Solved

Creating EMS script to help create Distribution Group

Posted on 2013-01-14
11
866 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

729 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