Link to home
Start Free TrialLog in
Avatar of ITISEMEA
ITISEMEA

asked on

Powershell Script

Hello,

 I want to add contacts in DL.
I have been provided an excel and there are country vise sheets.  All the sheets carry following columns:-         COUNTRY, LAST NAME
, FIRST NAME, JOB TITLE, E-MAIL, "TELEPHONE (OFFICE)",  "TELEPHONE (CELL)",

There are lot of sheets in the excel so please tell me any powershell script and how to execute it.

Please tell me the script since i am not good in powershell. I see one answer but cannot understand it https://www.experts-exchange.com/questions/28329685/Add-existing-external-contacts-to-distribution-list-with-PowerShell.html.  Therefore please tell any script you know I am using exchange 2010.
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

Try the following script below...

You only need to reference the E-Mail column to do the import, if the users have already been created.
$UserList = Import-csv "c:\filename.csv"
ForEach ($User in $UserList) {
$User.E-mail
Add-DistributiongroupMember -Identity DistributionGroup1 -Member $User.E-mail
}

Open in new window


That should do it.

Will.
Avatar of ITISEMEA
ITISEMEA

ASKER

ok, that means i open the powershell administrator, then i type given command. Here I shall give the file name that I created. However $user and $userlist etc the command shall pick of its own from the excel sheet. I have attached the duplicate excel. Please help.
powershell-script.xlsx
You need to convert this spreadsheet to a CSV file first.

- You then need to copy the script to notepade
- save the file with a .PS1 file extention
- You run this from the Exchagne Mangement Shell (ESM)
- make sure that your session is set to RemoteSigned (Set-ExecutionPolicy RemoteSigned)
- in the shell go to the location where the script has been saved
- type .\scriptname.ps1
- press enter

The script will run and add all of the users in the CSV file to the distribution group you specified in the script.

Will.
I have to stay till monday or tuesday to run it on exchange and if any query will ask then since IT people are on leave because of easter.
Hello, I mentioned the script to my manager and he said that he is not satisfied with contact addition. He said " 
"you need to check how we create contacts first of all
make sure that your script matches that when it executes".
Hello the script does not tell how to create contact in exchange could that be helped.
I have  to write a script to create a contact from the Excel sheet and then add them to a DL.
Request Attention Please.
Sorry i must have missed this question alert. Use the script below (original script is modified)

You need to construct your CSV like below...
Name                 ExternalEmailAddress
John Smith             jsmith@externaldomain1.com
Mike Jones             mjones@externaldomain2.com
etc...

$UserList = Import-csv "c:\filename.csv"
ForEach ($User in $UserList) {
$User.Name
$User.ExternalEmailAddress
New-MailContact -Identity $User.Name -ExternalEmailAddress $User.ExternalEmailAddress
}

ForEach ($User in $UserList {
Add-DistributiongroupMember -Identity DistributionGroup1 -Member $User.Name
}

Open in new window


The Code above will first create all of the users you have in your CSV file to Mail Contacts and then add them all to the distribution group you define in the second part of the script.

If there are any other requirements let me know.

Will.
Last thing Will. I have to do it on a live environment so any ways i can test it. Someone told to put whatif. Suppose if i do it then where to put it. Also will it effect anything?
You can put -Whatif at the end of Line 5 and 9. This will just tell you on screen what will happen.

Also a good test to accomplish this would just add 1 user to your CSV, run the script and make sure that it creates the mail contact and puts it in the correct DL.

Will.
hello, i haven't heard anything from my manager however I guess there is one thing lacking. It does not contain the OU. E.G. If i manually create a contact I go to r.net/OU/OU1/Contacts. So i guess if that can be added to the script.
ASKER CERTIFIED SOLUTION
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Will:-- I have been told to use this script than yours please help me what was lacing in your script. It was much easy.

$UserList = Import-csv "c:\temp\book1.csv" -Delimiter ";"
$OrganizationalUnit = "OU=Contacts,OU=rvp,OU=rvp_bharat,OU=_Company,DC=vaibhav,DC=rajarshi,DC=local"
$DistributionGroup ="testgroup-lkofod"

ForEach ($User in $UserList) {

$Displayname = $user.LastName+", "+$user.FirstName+" "+"("+$User.Country+")"
$Alias = $user.FirstName+$user.LastName

New-MailContact -DisplayName $Displayname -LastName $user.lastname -FirstName $user.firstname -Alias $Alias -Name $Displayname -ExternalEmailAddress $user.ExternalEmailAddress -OrganizationalUnit $OrganizationalUnit | select name, alias, ExternalEmailAddress

Set-Contact -Identity $Alias -Phone $user.Phone -CountryOrRegion $User.Country

Add-DistributiongroupMember -Identity $DistributionGroup -Member $Alias

}
i do not have issue with your script i can close if you say. However the query happens if the given script should be used or the script you made shall pick automatically the data fro the excel.
The only difference is you are using more variables. Also I have 2 ForEach commands as well. As I have provided a solution for you it is your choice if you are going to use it or not.

I have tested this and it works as suggested. At this point if you have any other additional questions regarding your new script you have found I would suggest closing this one and opening a new question.

Will.