Solved

Creating EMS script to help create Distribution Group

Posted on 2013-01-14
11
861 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In this video we show how to create an Address List 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 Organization >> Ad…
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…

828 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