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
https://www.experts-exchange.com/questions/28652035/Powershell-Script.html?anchorAnswerId=40738008#a40738008
//////////////////////////
////
$UserList = Import-csv "c:\temp\book1.csv" -Delimiter ";"
$OrganizationalUnit = "OU=Contacts,OU=rvp,OU=rvp
_bharat,OU
=_Company,
DC=vaibhav
,DC=rajars
hi,DC=loca
l"
$DistributionGroup ="testgroup-lkofod"
ForEach ($User in $UserList) {
$Displayname = $user.LastName+", "+$user.FirstName+" "+"("+$User.Country+")"
$Alias = $user.FirstName+$user.Last
Name
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-DistributiongroupMembe
r -Identity $DistributionGroup -Member $Alias
}
//////////////////////////
////
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.