Solved

Creating EMS script to help create Distribution Group

Posted on 2013-01-14
11
853 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now