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()