English 中文(简体)
Programmatically create a Distribution Group in Active Directory
原标题:

I have a Windows Domain here that runs Exchange 2007, and I need to programmatically create new Mailing Lists.

From what I could gather so far, Exchange mailing lists are just normal AD Groups, so I mainly have to worry about the interaction with AD. I used the System.DirectoryService namespace to query AD, but I m not sure what the correct way would be to create a Distribution Group here. Especially it has to be mail-enabled and show up in the Outlook address book, so I don t know if I need to invoke some magic to make sure Exchange picks up the new group?

Any pointers?

最佳回答

The magic you need to invoke to make a distribution list for Exchange is PowerShell so you ll need to dive into the wonderful world of cmdlets. ;-)

You can create a distribution group in Active Directory by using System.DirectoryServices (or more easily if you re on .NET 3.5 through System.DirectoryServices.AccountManagement), add members and so on and then use the Enable-DistributionGroup-cmdlet to mail-enable the group.

You could also create the groups and mail-enable them at the same time by using the New-DistributionGroup-cmdlet.

Basically what the PowerShell cmdlets do is to set a bunch of Exchange-attributes on the Active Directory group such as proxyAddresses and so on. You might get away with setting some of these "manually" (ie by using System.DirectoryServices) but chances are you get some of them wrong. The supported (as in Microsoft Support-supported) way is through calling the cmdlets.

You re probably best off Googling on how to call PowerShell from you .NET program (I haven t found a really good article on it but it s pretty straight-forward once you get the hang of it) - MSDN has a sample and a section to get you started.

问题回答

I ve created a PowerShell script with GUI for my colleagues to create a distribution group, add the manager, add up to 5 members and do the mail enable.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

Function DistributionGroup{

#Colecting the data
$Input1 = $InputBox1.Text   #GroupName
$Input2 = $InputBox2.Text   #Manager
$Input3 = $InputBox3.Text   #Alias
$Input4 = $InputBox4.Text   #Member1
$Input5 = $InputBox5.Text   #Member2
$Input6 = $InputBox6.Text   #Member3
$Input7 = $InputBox7.Text   #Member4
$Input8 = $InputBox8.Text   #Member5
$mailaddress = $Input1 +"@research.com"

#Checking if the group exists
   try {
    Get-ADGroup -Identity $Input1
     $GroupExists = $true
    Add-OutputBoxLine -Message "Fatal error!Distribution group already exists." -ForegroundColor Red

    exit
 }
catch [Microsoft.ActiveDirectory.Management.ADIdentityResolutionException] {
    $GroupExists = $false
Add-OutputBoxLine -Message "Distribution group does not exist, proceeding to create the group." -ForegroundColor Green

}

#Creating the Distribution Group
New-ADGroup -Name $Input1 -SamAccountName $Input1 -GroupCategory Distribution - 
GroupScope Universal -DisplayName $Input1 -Path "ou=Email, Ou=Groups, 
DC=group, Dc=local" -ManagedBy $Input2
Add-OutputBoxLine -Message "Distribution Group $Input1 has been created." -ForegroundColor Green
Add-OutputBoxLine -Message "Distribution Group manager has been set as $Input2." -ForegroundColor Green

 #Exporting the group members to the csv file

 if($Input4 -eq $null){
 Add-OutputBoxLine -Message "member field is empty, skiping to the next one"
 }
 else{
 $Report = New-Object psobject
 $Report | Add-Member -MemberType NoteProperty -name SamAccountName -Value $Input4 
 $Report | export-csv -Path "C:TempDistributionGroupMembers.csv" -NoTypeInformation -Append
 }

if($Input5 -eq $null) {
Add-OutputBoxLine -Message "member field is empty, skiping to the next one"
}
else{
$Report = New-Object psobject
$Report | Add-Member -MemberType NoteProperty -name SamAccountName -Value $Input5
$Report | export-csv -Path "C:TempDistributionGroupMembers.csv" -NoTypeInformation -Append
}

if($Input6 -eq $null) {
Add-OutputBoxLine -Message "member field is empty, skiping to the next one"
}
else{
$Report = New-Object psobject
$Report | Add-Member -MemberType NoteProperty -name SamAccountName -Value $Input6
$Report | export-csv -Path "C:TempDistributionGroupMembers.csv" -NoTypeInformation -Append
}

if($Input7 -eq $null) {
Add-OutputBoxLine -Message "member field is empty, skiping to the next one"
}
else{
$Report = New-Object psobject
$Report | Add-Member -MemberType NoteProperty -name SamAccountName -Value $Input7
$Report | export-csv -Path "C:TempDistributionGroupMembers.csv" -NoTypeInformation -Append
}

if($Input7 -eq $null) {
Add-OutputBoxLine -Message "member field is empty, skiping to the next one"
}
else{
$Report = New-Object psobject
$Report | Add-Member -MemberType NoteProperty -name SamAccountName -Value $Input8
$Report | export-csv -Path "C:TempDistributionGroupMembers.csv" -NoTypeInformation -Append
}

Add-OutputBoxLine -Message "Members have been exported to: C:TempDistributionGroupMembers.csv"

#Adding the members to the group
Import-Csv -Path “C:TempDistributionGroupMembers.csv” | ForEach-Object {Add-ADGroupMember -Identity $Input1 -Members $_.’SamAccountName’}
Add-OutputBoxLine -Message "Members have been added." -ForegroundColor Green

#Let the script breathe
   Add-OutputBoxLine -Message "Waiting 15 seconds for the servers to catch up, you will be promted for credentials soon." -ForegroundColor Green
   Start-Sleep -s 15

#Mail Enable of the group on HBR

    $SessionExchange= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://SUK1HBR01/PowerShell/ -Credential ($EXCred= Get-Credential -Message "input your email address and password. ex. jsnow@group.com") -Authentication Kerberos

#SUK1HBR01
Write-Output "Updating MailUser for HBR01"
Import-PSSession $SessionExchange -DisableNameChecking -AllowClobber

    try {
        Enable-DistributionGroup -Identity $Input1 -Alias $Input3 
        
    }
    catch {
        $errormessage= "Distribution group $Input1 not enabled"
        
    }

Remove-PSSession $SessionExchange

#Deleting the CSV file
Remove-Item -Path “C:TempDistributionGroupMembers.csv”
Add-OutputBoxLine -Message "And that s it, the distribution group $Input1 is ready and mail enabled. " -ForegroundColor Green

    }



Function Add-OutputBoxLine {
    Param ($Message)
    $OutputBox.AppendText("`r`n$Message")
    $OutputBox.Refresh()
    $OutputBox.ScrollToCaret()
 }

#Creating the Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "New Distribution Group made by Victor G." 
$Form.size = New-Object System.Drawing.Size (900,700)

#Adding the first label for inputbox
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Text = "Input the distribution group name (ex. Test_group)"
$Label1.Location  = New-Object System.Drawing.Point(10,20)
$Label1.AutoSize = $true
$Form.Controls.Add($Label1)

#Adding the second label for inputbox
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "Input the group manager (ex. jsnow)"
$Label2.Location  = New-Object System.Drawing.Point(10,75)
$Label2.AutoSize = $true
$Form.Controls.Add($Label2)

#Adding the third label for inputbox
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Text = "Input the group Alias (it can t use special characters or space ex.TestGroup)"
$Label3.Location  = New-Object System.Drawing.Point(10,125)
$Label3.AutoSize = $true
$Form.Controls.Add($Label3)

#Adding the forth label for inputbox
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Text = "Input the first group member (ex.tnow)"
$Label4.Location  = New-Object System.Drawing.Point(10,175)
$Label4.AutoSize = $true
$Form.Controls.Add($Label4)

#Adding the fifth label for inputbox
$Label5 = New-Object System.Windows.Forms.Label
$Label5.Text = "Input the second group member (ex.cpopescu)"
$Label5.Location  = New-Object System.Drawing.Point(10,225)
$Label5.AutoSize = $true
$Form.Controls.Add($Label5)

#Adding the sixth label for inputbox
$Label6 = New-Object System.Windows.Forms.Label
$Label6.Text = "Input the third group member (ex.idascal)"
$Label6.Location  = New-Object System.Drawing.Point(10,275)
$Label6.AutoSize = $true
$Form.Controls.Add($Label6)

#Adding the seventh label for inputbox
$Label7 = New-Object System.Windows.Forms.Label
$Label7.Text = "Input the forth group member (ex.rnicolescu)"
$Label7.Location  = New-Object System.Drawing.Point(10,325)
$Label7.AutoSize = $true
$Form.Controls.Add($Label7)

#Adding the 9th label for inputbox
$Label8 = New-Object System.Windows.Forms.Label
$Label8.Text = "Input the fifth group member (ex.opinter)" 
$Label8.Location  = New-Object System.Drawing.Point(10,375)
$Label8.AutoSize = $true
$Form.Controls.Add($Label8)

#Adding the eighth label for inputbox
$Label9 = New-Object System.Windows.Forms.Label
$Label9.Text = "System response here"
$Label9.Location  = New-Object System.Drawing.Point(10,440)
$Label9.AutoSize = $true
$Form.Controls.Add($Label9)

#Adding the first imput box
$InputBox1 = New-Object System.Windows.Forms.TextBox
$InputBox1.Location = New-Object System.Drawing.Size (20,50)
$InputBox1.Size = New-Object System.Drawing.Size (300,20)
$InputBox1.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
$Form.Controls.Add($InputBox1)

#Adding the second imput box
$InputBox2 = New-Object System.Windows.Forms.TextBox
$InputBox2.Location = New-Object System.Drawing.Size (20,100)
$InputBox2.Size = New-Object System.Drawing.Size (300,20)
$InputBox2.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
$Form.Controls.Add($InputBox2)

#Adding the third imput box
$InputBox3 = New-Object System.Windows.Forms.TextBox
$InputBox3.Location = New-Object System.Drawing.Size (20,150)
$InputBox3.Size = New-Object System.Drawing.Size (300,20)
$InputBox3.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
$Form.Controls.Add($InputBox3)

#Adding the forth imput box
$InputBox4 = New-Object System.Windows.Forms.TextBox
$InputBox4.Location = New-Object System.Drawing.Size (20,200)
$InputBox4.Size = New-Object System.Drawing.Size (300,20)
$InputBox4.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
$Form.Controls.Add($InputBox4)

#Adding the fifth imput box
$InputBox5 = New-Object System.Windows.Forms.TextBox
$InputBox5.Location = New-Object System.Drawing.Size (20,250)
$InputBox5.Size = New-Object System.Drawing.Size (300,20)
$InputBox5.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
$Form.Controls.Add($InputBox5)

#Adding the sixth imput box
$InputBox6 = New-Object System.Windows.Forms.TextBox
$InputBox6.Location = New-Object System.Drawing.Size (20,300)
$InputBox6.Size = New-Object System.Drawing.Size (300,20)
$InputBox6.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
$Form.Controls.Add($InputBox6)

#Adding the seventh imput box
$InputBox7 = New-Object System.Windows.Forms.TextBox
$InputBox7.Location = New-Object System.Drawing.Size (20,350)
$InputBox7.Size = New-Object System.Drawing.Size (300,20)
$InputBox7.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
$Form.Controls.Add($InputBox7)

#Adding the eight imput box
$InputBox8 = New-Object System.Windows.Forms.TextBox
$InputBox8.Location = New-Object System.Drawing.Size (20,400)
$InputBox8.Size = New-Object System.Drawing.Size (300,20)
$InputBox8.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
$Form.Controls.Add($InputBox8)


#Adding an outbox
$OutputBox = New-Object System.Windows.Forms.TextBox
$OutputBox.Location = New-Object System.Drawing.Size (10,470)
$OutputBox.Size = New-Object System.Drawing.Size(400,150)
$OutputBox.Font = New-Object System.Drawing.Font("Arial",12,[System.Drawing.FontStyle]::Regular)
$OutputBox.Multiline = $true
$OutputBox.ScrollBars = "Vertical"
$OutputBox.ReadOnly = $True
$Form.Controls.Add($OutputBox)

#Adding the button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size (500,400)
$Button.Size = New-Object System.Drawing.Size (200,100)
$Button.Text = "Create New Distribution Group"
$Button.Add_click({DistributionGroup})
$Form.Controls.Add($Button)

$ButtonCloseSkript = New-Object System.Windows.Forms.Button 
$ButtonCloseSkript.Location = New-Object System.Drawing.Size(750,600) 
$ButtonCloseSkript.Size = New-Object System.Drawing.Size(92,30) 
$ButtonCloseSkript.Text = "EXIT" 
$ButtonCloseSkript.Add_Click({$Form.Close()})#On click do function
$ButtonCloseSkript.Cursor = [System.Windows.Forms.Cursors]::Hand 
$Form.Controls.Add($ButtonCloseSkript)

$Form.ShowDialog()




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签