Solved

Creating EMS script to help create Distribution Group

Posted on 2013-01-14
11
858 Views
Last Modified: 2013-01-15
I'm trying to create a power shell script for EMS that will help in creating distribution groups.

I would like it to ask for the name of the group, email address and members. If i have multiple members I'm not sure how to pass that as a variable for each user name. this is what I have so far:

$distgroup = Read-Host "Enter new Distribution Group name"
$orgunit = Read-Host "Enter OU"
$emailaddress = Read-Host "Enter Email Address for the Group"
$groupmembers = Read-Host "Enter Member name"


New-DistributionGroup -Name "$distgroup" -OrganizationalUnit "$orgunit" -SAMAccountName "$distgroup" -Type "Distribution"
Set-DistributionGroup -EmailAddressPolicyEnabled:$False
Set-DistributionGroup -Identity $distgroup -EmailAddresses $emailaddress@domain.com
Set-DistributionGroup $distgroup -requiresenderauthenticationenabled:$False
Add-DistributionGroupMember -Identity "$distgroup" -Member

Open in new window



I'm not sure how to properly grab/pass multiple entries to the last entry. I'm still a novice at power shell but I'm trying to learn.
0
Comment
Question by:msidnam
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 13

Expert Comment

by:imkottees
ID: 38776345
Hi,

you can try like this..

Add-DistributionGroupMember -Idneity GROUP-NAME -Member Member1
Add-DistributionGroupMember -Idneity GROUP-NAME -Member Member2

or

Import-CSV FileName.csv | ForEach {Add-DistributionGroupMember -Identity "GROUP-NAME" -Member $_.Name}
0
 
LVL 18

Accepted Solution

by:
suriyaehnop earned 500 total points
ID: 38776775
May you try this

$distgroup = Read-Host "Enter new Distribution Group name"
$orgunit = Read-Host "Enter OU"
$emailaddress = Read-Host "Enter Email Address for the Group"
$groupmembers = Read-Host "Enter Member name"


New-DistributionGroup -Name "$distgroup" -OrganizationalUnit "$orgunit" -SAMAccountName "$distgroup" -Type "Distribution"
do
{
$DL = Get-DistributionGroup $distgroup -ErrorAction SilentlyContinue
} while ($DL -eq $null)

Set-DistributionGroup -EmailAddressPolicyEnabled:$False
Set-DistributionGroup -Identity $distgroup -EmailAddresses $emailaddress@domain.com
Set-DistributionGroup $distgroup -requiresenderauthenticationenabled:$False

#Add member. CSV file must have "Name" as header

$Members = Import-Csv C:\File.csv
Foreach ($Member in $Members) {

Add-DistributionGroupMember -Identity $distgrop -Member $Member.name

}

Open in new window


or

$distgroup = Read-Host "Enter new Distribution Group name"
$orgunit = Read-Host "Enter OU"
$emailaddress = Read-Host "Enter Email Address for the Group"
$groupmembers = Read-Host "Enter Member name"


New-DistributionGroup -Name "$distgroup" -OrganizationalUnit "$orgunit" -SAMAccountName "$distgroup" -Type "Distribution"
do
{
$DL = Get-DistributionGroup $distgroup -ErrorAction SilentlyContinue
} while ($DL -eq $null)

Set-DistributionGroup -EmailAddressPolicyEnabled:$False
Set-DistributionGroup -Identity $distgroup -EmailAddresses $emailaddress@domain.com
Set-DistributionGroup $distgroup -requiresenderauthenticationenabled:$False

#Add member. CSV file must have "Name" as header

$Members = "Name1","Name2","Name3"
Foreach ($Member in $Members) {

Add-DistributionGroupMember -Identity $distgrop -Member $Member

}

Open in new window

0
 
LVL 2

Author Comment

by:msidnam
ID: 38776823
thank you for the info. What is this piece of code doing?

do
{
$DL = Get-DistributionGroup $distgroup -ErrorAction SilentlyContinue
} while ($DL -eq $null)

Open in new window



I think i would like to try this piece of code:

$Members = "Name1","Name2","Name3"
Foreach ($Member in $Members) {

Add-DistributionGroupMember -Identity $distgrop -Member $Member

}

Open in new window


Could I make $Members a variable on top and let the user add the members using commas (user1, user2, user3) and then put in the ForEach code?

Sample:

$distgroup = Read-Host "Enter new Distribution Group name"
$orgunit = Read-Host "Enter OU"
$emailaddress = Read-Host "Enter Email Address for the Group"
$Members  = Read-Host "Enter Member names"


New-DistributionGroup -Name "$distgroup" -OrganizationalUnit "$orgunit" -SAMAccountName "$distgroup" -Type "Distribution"
do
{
$DL = Get-DistributionGroup $distgroup -ErrorAction SilentlyContinue
} while ($DL -eq $null)

Set-DistributionGroup -EmailAddressPolicyEnabled:$False
Set-DistributionGroup -Identity $distgroup -EmailAddresses $emailaddress@domain.com
Set-DistributionGroup $distgroup -requiresenderauthenticationenabled:$False

Foreach ($Member in $Members) {

Add-DistributionGroupMember -Identity $distgroup -Member $Member

}

Open in new window

0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 18

Expert Comment

by:suriyaehnop
ID: 38776872
do
{
$DL = Get-DistributionGroup $distgroup -ErrorAction SilentlyContinue
} while ($DL -eq $null) 

Open in new window


If you have mulitple domain controller, this code will checking if the DL already sync to all DC. Say if DL synced to DC1 not DC2, when you performed Set-DistributionGroup, you may encounter the DL isn't found.

$distgroup = Read-Host "Enter new Distribution Group name"
$orgunit = Read-Host "Enter OU"
$emailaddress = Read-Host "Enter Email Address for the Group"
$Members  = Read-Host "Enter Member names"



This will include one members only, you have to have some kind of loop to enter for $Members
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38777888
Could I make $Members a variable on top and let the user add the members using commas (user1, user2, user3) and then put in the ForEach code?
Yes you can.. Enter the users in following format and save it to a variable,
JohnEvans@Contoso.com,Phil@Contoso.com,James@Constoso.com
 
Then it should work with this script..

$Members  = Read-Host "Enter Member names"
$Member  -split "," | %{ Add-DistributionGroupMember -Identity $distgroup -Member $_ }

Open in new window

0
 
LVL 2

Author Closing Comment

by:msidnam
ID: 38778116
I'm trying to make the code as simple as possible an was hoping i could type in each name myself and have it add. i didnt want to add a csv file as that will add more components. however the ForEach loop with the members in $Members will work until i find a way. thank you

I tested this and it works great.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38778346
Did you try the method I suggested to add members? you can type users and add it to list..

$Members  = Read-Host "Enter Member names"
$Members  -split "," | %{ Add-DistributionGroupMember -Identity $distgroup -Member $_ }

Open in new window

There was a typo in my previous code..I have corrected it..
0
 
LVL 2

Author Comment

by:msidnam
ID: 38778365
Subsun, i will try this. im sorry, i didnt see your previous comment until just now. im not sure why i didnt get the email mentioning your post and i never hit refresh on my browser before accepting the answer.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38778415
It's ok. :-).. you can test it and let me know if it works for you..
0
 
LVL 2

Author Comment

by:msidnam
ID: 38779045
Subsun, I just tested this and it works great. I will have more power shell questions so please keep on the lookout and i'll be sure to refresh my browser :)

thank you.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38779760
You are welcome!..  It's not too late, If you like, you can click on request attention button to split the points.. I guess mod's can reopen the question to reassign the points as you like.. if not leave it like this.. not a problem.. ;-)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question