Solved

Creating EMS script to help create Distribution Group

Posted on 2013-01-14
11
865 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 19

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

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

Office 365 Advanced Training for Admins

Special Offer:  Buy 1 course, get 2nd free!  Buy the 'Managing Office 365 Identities & Requirements' course w/ Accelerated TestPrep, and automatically receive the 'Enabling Office 365 Services' course FREE!

Question has a verified solution.

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

Suggested Solutions

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
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

734 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