create email distribution list in exchange 2007 via powershell scirp
-
Monday, June 04, 2012 7:33 AM
create Email distribution list in exchange 2007 via powershell scirp
and set manager name and check in AD Manager can update membership list
- Edited by MyTechnet.Me Monday, June 04, 2012 7:34 AM
All Replies
-
Monday, June 04, 2012 8:37 AM
You can create a distribution group using the New-DistributionGroup Cmdlet:
http://technet.microsoft.com/en-us/library/aa998856(v=exchg.80)
To set the ManagedBy attribute you can use the -ManagedBy parameter for the Cmdlet.
Jaap Brasser
http://www.jaapbrasser.com- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:49 AM
-
Monday, June 04, 2012 9:06 AM
Thx, I did followed and its work but its not check option in AD "Manager can update membership list"
could u please assist for this
Ilyas Mohammed
-
Monday, June 04, 2012 9:44 AM
Hi,
Try
Add-ADPermission -Identity 'YourDistributionGroup' -User 'SamOfManager' -AccessRights WriteProperty -Properties "Member"
according to http://theblownlightbulb.com/2011/03/22/useful-cmdlet-commands-for-exchange/
if it doesn't work, you might want to check out this link http://iconraja.wordpress.com/2011/02/25/managing-distribution-groups-changed-in-exchange-2010/
good luck!
- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:49 AM
-
Monday, June 04, 2012 9:48 AM
In the example above we gave permissions to SamOrManager to update the membership list of the "YourDistributionGroup" distribution group (we need to declare the manager at the same time, just like you're doing it in the GUI).Hi,
Try
Add-ADPermission -Identity 'YourDistributionGroup' -User 'SamOfManager' -AccessRights WriteProperty -Properties "Member"
according to http://theblownlightbulb.com/2011/03/22/useful-cmdlet-commands-for-exchange/
if it doesn't work, you might want to check out this link http://iconraja.wordpress.com/2011/02/25/managing-distribution-groups-changed-in-exchange-2010/
good luck!
- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:49 AM
-
Monday, June 04, 2012 10:25 AM
Unfortunately receive this error
[PS] C:\>Add-AdPermission -Identity "_Dlname" -User 'SamofManager' -AccessRights WriteProperty -Properties "Member"
Add-ADPermission : Failed to resolve the specified user or group "SamofManager.
" If the user or group is a foreign forest principal, you must have either a tw
o-way trust or an outgoing trust."
At line:1 char:17
+ Add-AdPermission <<<< -Identity "_DLname" -User 'SamofManager' -Acc
essRights WriteProperty -Properties "Member"
+ CategoryInfo : InvalidOperation: (:) [Add-ADPermission], Locali
zedException
+ FullyQualifiedErrorId : CB15E391,Microsoft.Exchange.Management.Recipient
Tasks.AddADPermission
Ilyas Mohammed
- Edited by MyTechnet.Me Monday, June 04, 2012 10:25 AM
-
Monday, June 04, 2012 10:51 AM
you have to replace the string SamofManager with the actual Samaccountname of the User u want this to manage.
So if you p.x. have a User Peter Smith with the Samaccountname PSmith your Powershell Input should look like this
Add-AdPermission -Identity "_Dlname" -User 'PSmith' -AccessRights WriteProperty -Properties "Member"
Good luck- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 4:05 AM
- Unmarked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:49 AM
- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:50 AM
-
Monday, June 04, 2012 12:53 PM
Thx its work but one by one i have to run
could you assist me to make it as script file, i had tried following but fail;
new-DistributionGroup -Name '_Dltest' -Type 'Distribution' -OrganizationalUnit 'Mytechnet.me/Mytechnet
/Distributionlist' -SamAccountName '_DLtest' -Alias 'DLtest' -Managedby "usert9"Do {Start-Sleep -m 5} Until (Get-Distributiongroup $'DLtest')
Import-Module ActiveDirectory
Add-AdPermission -Identity "Dltest" -User 'usert9' -AccessRights WriteProperty -Properties "Member"
Ilyas Mohammed
- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 4:05 AM
-
Monday, June 04, 2012 1:35 PM
Sorry but it's really hard to figure out what you actually trying to do.
If you want to write a script and manually set the DistributionGroup and User try this
$User = Read-Host "Type Samaccountname of User" $DGroup = Read-Host "Type Distribution Group Name" Add-AdPermission -Identity $DGroup -User $User -AccessRights WriteProperty -Properties "Member"
You may want to use a csv file where you declare the Manager of each Distribution Group
Content of DistributionList.csv
Group;User Dltest;usert9 DlHi;usert10 DlHello;usert11
Powershell Script
# Get Content of Csv File
$Csv = Import-Csv -Path DistributionList.csv -Delimiter ";"
# Place settings $Csv | foreach-object {Add-AdPermission -Identity $_.Group -User $_.User -AccessRights WriteProperty -Properties "Member"}
If it's not what you're looking for, please specify what you want to do exactly. You can also Vote My Answer as helpfull if it helped you.
thx
- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 4:05 AM
- Unmarked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:49 AM
- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:49 AM
-
Monday, June 04, 2012 7:33 PM
can u assist for this for organization unit
$DGroup = Read-Host "Type Distribution Group Name"
$User = Read-Host "Type Samaccountname of User"
$DLlocation = Read-Host "Type Distribution List Location"
new-DistributionGroup -Name "$DGroup" -Type 'Distribution' -OrganizationalUnit 'Mytechnet.me/mytechnet/'$DLlocation'' -SamAccountName "$DGroup" -Alias "$DGroup" -Managedby "$User"
Add-AdPermission -Identity $DGroup -User $User -AccessRights WriteProperty -Properties "Member"Ilyas Mohammed
-
Tuesday, June 05, 2012 4:02 AM
Thx i made it, its working fine now
thank u very much for your support.
another thing i need to try setting description for DL in script
$DGroup = Read-Host "Type Distribution Group Name"
$User = Read-Host "Type Samaccountname of User"
$DLlocation = Read-Host "Type Distribution List Location"
new-DistributionGroup -Name "$DGroup" -Type 'Distribution' -OrganizationalUnit Mytechnet.me/mytechnet/"$DLlocation'' -SamAccountName "$DGroup" -Alias "$DGroup" -Managedby "$User"
Add-AdPermission -Identity $DGroup -User $User -AccessRights WriteProperty -Properties "Member"Ilyas Mohammed
-
Tuesday, June 05, 2012 6:39 AM
1. you need to install the active directory module Install AD Module
2. import the ad cmdlets to the powershell
import-module ActiveDirectory
3. place the settings using the Set-ADUser Cmdlet
# add this to your script
$Description = Read-Host "Type Description" set-adgroup $DGroup -description $Description
I appreciate it very much if you vote my Replies as Helpful or Mark as Answer if I could help you out. Thanks!
- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:50 AM
-
Tuesday, June 05, 2012 7:05 AM
Thanks is did work!!! fine
how do i loop back to start again or quit option
$DGroup = Read-Host "Type Distribution Group Name"
$Alias = Read-Host "Type DL Alias name with out any space"
$DLlocation = Read-Host "Type Distribution List Location"
$User = Read-Host "Type Samaccountname of User"
$Description = Read-Host "Type Description "Request Number""
$strResponce = "Quit"
new-DistributionGroup -Name "$DGroup" -Type 'Distribution' -OrganizationalUnit Mytechnet.me/mytechnet/"$DLlocation" -SamAccountName "$DGroup" -Alias "$Alias" -Managedby "$User"Import-Module ActiveDirectory
Set-adgroup $DGroup -description $Description
Add-AdPermission -Identity $DGroup -User $User -AccessRights WriteProperty -Properties "Member"
do {$strResponce = Read-Host "Do you want to continue for creation another DL?(Y/N)"}
until (strResponce -eq "Y")Ilyas Mohammed
-
Tuesday, June 05, 2012 7:40 AM
You should make the do {} command encompass the entire script, so this will work. It will continue looping until n or no is typed:
do { $DGroup = Read-Host "Type Distribution Group Name" $Alias = Read-Host "Type DL Alias name with out any space" $DLlocation = Read-Host "Type Distribution List Location" $User = Read-Host "Type Samaccountname of User" $Description = Read-Host "Type Description "Request Number"" $strResponce = "Quit" new-DistributionGroup -Name "$DGroup" -Type 'Distribution' -OrganizationalUnit Mytechnet.me/mytechnet/"$DLlocation" -SamAccountName "$DGroup" -Alias "$Alias" -Managedby "$User" Import-Module ActiveDirectory Set-adgroup $DGroup -description $Description Add-AdPermission -Identity $DGroup -User $User -AccessRights WriteProperty -Properties "Member" $strResponce = Read-Host "Do you want to continue for creation another DL?([Y]es/[N]o)" } until ($strResponce -eq "n" -or $strresponse -eq "no")
Jaap Brasser
http://www.jaapbrasser.com- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:50 AM
-
Tuesday, June 05, 2012 8:24 AM
Thx for driving me ferpect!!!!!
:)
if possible i want to ask further question.
Ilyas Mohammed
-
Tuesday, June 05, 2012 8:38 AMSure, what would you like to ask?
Jaap Brasser
http://www.jaapbrasser.com- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:50 AM
- Unmarked As Answer by Yan Li_Microsoft Contingent Staff, Moderator Thursday, June 07, 2012 2:57 AM
-
Tuesday, June 05, 2012 8:45 AMhow do i run this script with out exchange powershell managment in gui mode
Ilyas Mohammed
- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:50 AM
- Unmarked As Answer by Yan Li_Microsoft Contingent Staff, Moderator Thursday, June 07, 2012 2:57 AM
-
Tuesday, June 05, 2012 8:59 AM
What do you mean exactly by in GUI mode, do you want PowerShell to prompt you with popup dialogs instead of have to type into the console?
Also in order to be able to execute you either need to have the following:
- Exchange Management pack installed
- A Remote PowerShell connection to a server that has the pack installed
If you want to load the snapin manually in a normal PowerShell session, this is unsupported, you can run the following command:
Add-PSSnapin -Name "Microsoft.Exchange.Management.PowerShell.Admin"
Jaap Brasser
http://www.jaapbrasser.com- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:50 AM
-
Tuesday, June 05, 2012 9:34 AM
I have gone throw snapin, what i mean to say instead of powershell command develop script can be used as back ground process and front hand gui mode application like VB or portal.
Ilyas Mohammed
- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:50 AM
-
Tuesday, June 05, 2012 11:03 AM
Again I'm not sure about what you really what.
How is this supposed to run as a back ground proccess?
If you want to create a GUI u can use the System.Windows.Forms class: http://blogs.technet.com/b/csps/archive/2011/12/07/guiapp.aspx
Good luck
I appreciate it very much if you vote my Replies as Helpful or Mark as Answer if I could help you out. Thanks!
- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 11:51 AM
-
Tuesday, June 05, 2012 12:54 PM
how do i use cmdlet (Start-Sleep -m 50} Until (Get-distributiongroup -Identity $DGroup)) in below script because below script run fine in my trail server but in production its delayed to create DL due to this following command not finding object.
do {
$DGroup = Read-Host "Type Distribution Group Name"
$Alias = Read-Host "Type DL Alias name with out any space"
$DLlocation = Read-Host "Type Distribution List Location"
$User = Read-Host "Type Samaccountname of User"
$Description = Read-Host "Type Description "Request Number""
$strResponce = "Quit"
new-DistributionGroup -Name "$DGroup" -Type 'Distribution' -OrganizationalUnit sabiccorp.sabic.com/Sabicgroups/MEA/"$DLlocation" -SamAccountName "$DGroup" -Alias "$Alias" -Managedby "$User"
Import-Module ActiveDirectoryStart-Sleep -m 50} Until (Get-distributiongroup -Identity $DGroup)
Set-adgroup $DGroup -description $Description
Add-AdPermission -Identity $DGroup -User $User -AccessRights WriteProperty -Properties "Member"
$strResponce = Read-Host "Do you want to continue for creation another DL?([Y]es/[N]o)"
}
until ($strResponce -eq "n" -or $strresponse -eq "no")Ilyas Mohammed
-
Tuesday, June 05, 2012 1:06 PM
Hello, here is what you do:
do { $DGroup = Read-Host "Type Distribution Group Name" $Alias = Read-Host "Type DL Alias name with out any space" $DLlocation = Read-Host "Type Distribution List Location" $User = Read-Host "Type Samaccountname of User" $Description = Read-Host "Type Description "Request Number"" $strResponce = "Quit" new-DistributionGroup -Name "$DGroup" -Type 'Distribution' -OrganizationalUnit sabiccorp.sabic.com/Sabicgroups/MEA/"$DLlocation" -SamAccountName "$DGroup" -Alias "$Alias" -Managedby "$User" Import-Module ActiveDirectory Start-Sleep -Seconds 2 Get-distributiongroup -Identity $DGroup Set-adgroup $DGroup -description $Description Add-AdPermission -Identity $DGroup -User $User -AccessRights WriteProperty -Properties "Member" $strResponce = Read-Host "Do you want to continue for creation another DL?([Y]es/[N]o)" } until ($strResponce -eq "n" -or $strresponse -eq "no")
You just place Start-Sleep on a separate line and you can use either the -Seconds of -MilliSeconds parameter to specify the amount of time it should wait.
Also if you are keen on developing PowerShell GUI's for your scripts you should check out PowerShell Studio 2012, it allows you to easily create graphical interfaces for your PowerShell scripts. Check it out here:
http://www.sapien.com/software/powershell_studio
Jaap Brasser
http://www.jaapbrasser.com- Marked As Answer by MyTechnet.Me Tuesday, June 05, 2012 1:38 PM
-
Tuesday, June 05, 2012 1:39 PM
done work perfect!
further, what will be the script for send email after process DL creation.
Ilyas Mohammed
-
Tuesday, June 05, 2012 3:32 PMI am not sure what you mean by that, do you want the script to send an email, if so what kind of information would you like it to send?
Jaap Brasser
http://www.jaapbrasser.com -
Tuesday, June 05, 2012 5:22 PM
after completion DL creation process will like to send email to requester and DL creator;
following info can be contain
DL name, DL Email, managedby and description.
Ilyas Mohammed
- Edited by MyTechnet.Me Tuesday, June 05, 2012 5:24 PM
-
Wednesday, June 06, 2012 7:14 AM
It depends how you want to send the email, here is an example of how to send an email utilizing Outlook through PowerShell. I made an assumption all email address are username@sabic.com, if this is not correct we can also implement an AD query to gather the correct email addresses. Here is the code:
$OutlookCOM = New-Object -comObject Outlook.Application $Email = $OutlookCOM.CreateItem(0) $EMail.Recipients.Add("$($env:username)@sabic.com") $EMail.Recipients.Add("$($User)@sabic.com") $EMail.Subject = "New Distribution Group created: $DGroup" $user = $env:username $EMail.Body = "$DGroup $($DGroup)@Sabic.com $User $Description" $EMail.Send()
Jaap Brasser
http://www.jaapbrasser.com -
Wednesday, June 06, 2012 5:15 PM
i tried by following method not work.
DO {
$DGroup = Read-Host "Type Distribution Group Name"
$Alias = Read-Host "Type DL Alias name with out any space"
$DLlocation = Read-Host "Type Distribution List Location"
$Email = Read-Host "Type Samaccountname of User or Email Address"
$Description = Read-Host "Type Description "Request Number""
$strResponce = "quit"
new-DistributionGroup -Name "$DGroup" -Type 'Distribution' -OrganizationalUnit Mytechnet.me/mytechnet/"$DLlocation" -SamAccountName "$DGroup" -Alias "$Alias" -Managedby "$Email"Import-Module ActiveDirectory
Start-Sleep -Seconds 120Set-adgroup $DGroup -description $Description
Add-AdPermission -Identity $DGroup -User $Email -AccessRights WriteProperty -Properties "Member"
$OutlookCOM = New-Object -comObject Outlook.Application
$Email = $OutlookCOM.CreateItem(0)
$EMail.Recipients.Add("$($env:usert9)@Mytechnet.me")
$EMail.Recipients.Add("$($Email)@mytechnet.me")
$EMail.Subject = "New Distribution Group created: $DGroup"
$user = $env:usert9
$EMail.Body = "$DGroup
$($DGroup)@mytechnet.me
$User
$Description"
$EMail.Send()$strResponce = Read-Host "Do you want to continue for creation another DL?(Y/N)"
}
until ($strResponce -eq "no")Ilyas Mohammed
- Marked As Answer by MyTechnet.Me Thursday, June 07, 2012 11:16 AM
-
Thursday, June 07, 2012 5:50 AMSo where does it go wrong, does PowerShell present you with an error? Also do you have Outlook installed on the workstation or server you are executing the script from, otherwise we need a different piece of code. I also noticed that you changed the $env:username to $env:usert9, why was that? I used $env:username to grab the username of the current user, or the user that is executing the script as to establish who to mail it to.
Jaap Brasser
http://www.jaapbrasser.com -
Thursday, June 07, 2012 10:26 AMException calling "Send" with "0" argument(s): "Outlook does not recognize one or more names. "
At E:\script\creatingDL-2.ps1:27 char:12
+ $EMail.Send <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocationIlyas Mohammed
-
Thursday, June 07, 2012 10:33 AMThat seems like a clear error, the formatting in your email addresses does not seem to work. You can test this by manually setting $EMail.Recipients.Add() to an email address you know exists. Can you try that and let us know the results?
Jaap Brasser
http://www.jaapbrasser.com -
Thursday, June 07, 2012 11:16 AM
some how with quit change it did work but dint receive any email.
Ilyas Mohammed
-
Thursday, June 07, 2012 12:47 PM
Okay an alternative would be to use SMTP to send the email. Here is an excerpt of code that you can use for that, please modify the email addresses and the smtp.sabic.com to the correct smtp server:
$Smtp = New-Object Net.Mail.SmtpClient("smtp.sabic.com") $Email = New-Object Net.Mail.MailMessage $Email.From = "Automation@sabic.com" $Email.ReplyTo = "Automation@sabic.com" $EMail.To.Add("$($env:username)@sabic.com") $EMail.To.Add("$($User)@sabic.com") $EMail.Subject = "New Distribution Group created: $DGroup" $EMail.Body = "$DGroup $($DGroup)@Sabic.com $($env:username) $Description" $Smtp.Send($Email)
Jaap Brasser
http://www.jaapbrasser.com -
Thursday, June 07, 2012 3:43 PM
Exception calling "Add" with "1" argument(s): "The specified string is not in the form required for an e-mail address."
At E:\script\creatingDL-2.ps1:21 char:14
+ $EMail.To.Add <<<< ("$($env:username)mytechnet.me")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodExceptionException calling "Send" with "1" argument(s): "Failure sending mail."
At E:\script\creatingDL-2.ps1:28 char:11
+ $Smtp.Send <<<< ($Email)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodExceptionIlyas Mohammed
-
Friday, June 08, 2012 10:14 AM
The error here is quite clear, you are not inserting a correct email address. usernamemytechnet.me is not an email address, just manually set a correct email address to verify if the script works for you.Jaap Brasser
http://www.jaapbrasser.com- Marked As Answer by MyTechnet.Me Friday, June 08, 2012 8:32 PM
-
Friday, June 08, 2012 8:33 PMThx its WORK!
Ilyas Mohammed

