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 http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28329685.html.  Therefore please tell any script you know I am using exchange 2010.
ITISEMEAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
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.
0
ITISEMEAAuthor Commented:
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
0
Will SzymkowskiSenior Solution ArchitectCommented:
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.
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

ITISEMEAAuthor Commented:
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.
0
ITISEMEAAuthor Commented:
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".
0
ITISEMEAAuthor Commented:
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.
0
ITISEMEAAuthor Commented:
Request Attention Please.
0
Will SzymkowskiSenior Solution ArchitectCommented:
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.
0
ITISEMEAAuthor Commented:
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?
0
Will SzymkowskiSenior Solution ArchitectCommented:
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.
0
ITISEMEAAuthor Commented:
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.
0
Will SzymkowskiSenior Solution ArchitectCommented:
All you need to do for that is add the organizational parameter on line 5. I have modified the script below...
$UserList = Import-csv "c:\filename.csv"
ForEach ($User in $UserList) {
$User.Name
$User.ExternalEmailAddress
New-MailContact -Identity $User.Name -ExternalEmailAddress $User.ExternalEmailAddress -OrganizationalUnit "OU=contacts,DC=example,DC=com"
}

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

Open in new window


Will.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITISEMEAAuthor Commented:
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

}
0
ITISEMEAAuthor Commented:
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.
0
Will SzymkowskiSenior Solution ArchitectCommented:
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.
0
ITISEMEAAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.