Difference between the scripts

Hello,

I have very less knowledge of Powershell Script. I want to know what does the commands in given script mean. I want to understand why this script is different from the one i raised a question on last script. Please explain in detail so that i can learn powershell better.
/////////////////////////////////

My old question was http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28652035.html#a40738008

//////////////////////////////
$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

}
//////////////////////////////
ITISEMEAAsked:
Who is Participating?
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:
This is the breakdown...
First Section
In the script you have posted lines 2/3 are using Variables for Organizational Unit and Distribution Group. The variable is now used in the script rather than the full OU path or Distribution Group name. This is useful when you have large scripts and you are using and have the same info in multiple places. When you are using a Variable you just need to change it at the top and it applies to all locations where the variable is present.

Second Section
ForEach is being used in both which tells Powershell to complete all commands in the {} and then move to the next one in the list (your list being the CSV file). This is required.

Third Section
in the script you have posted here you have 2 more variables DisplayName and Alias. This is basically providing the format of how the DisplayName and Alias are constructed based on the values you have in your CSV file. In the script I initially provided it only uses the values REQUIRED Name and Email Address to create the New-MailContact. All other parameters are options. I only added the ones that were required.

Forth Section
Provides another cmdlet Set-Mailcontact on a separate line. This entire process will happen for each user within the {}. This command is present due to not being able to modify those properties when using the new-mailcontact creation. The contact needs to be created first then you can run the Set-Mailcontact cmdlet to modify/update the attributes.

Fifth Section
The last section is adding the Users to a Distribution Group. In my first script I used another ForEach command which will do this after all of the contacts have been created rather than doing it while each contact is being created.


That is the complete breakdown of both of the scripts. Overview is that the second script had move Variables and additional info added for properties that needed to be set during the creation process.

Will.
0
ITISEMEAAuthor Commented:
ok thanks and i just need a help on first section:---"This is useful when you have large scripts and you are using and have the same info in multiple places." So i want to ask that why should i change if everything is in excel the script you told earlier should automatically pick it from there, then why this part of script is needed.
0
Will SzymkowskiSenior Solution ArchitectCommented:
So for example...

In my first script I did the following
-OrganizationalUnit "ou=test,dc=example,dc=com"

In the script you provided
$OrganizationalUnit = "ou=test,dc=example,dc=com"

-OrganizationalUnit $OrgnaizationalUnit

As you can see above I am using the Variable rather than typing the entire string out, like my first example.

So if your script contained multiple parameters for OrganizationalUnit when you are using a Variable you just need to type the full string once and use the variable $OrganizationalUnit multiple times where needed in the script.

Another thing that is good about variables is that the script looks cleaner as well.

Those are the reasons for using variables oppose to using the full OU path every time.

So i want to ask that why should i change if everything is in excel
The CSV file is still required. In the second script you are using you are formatting it differently that is all.

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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.