question

EavenHuang avatar image
0 Votes"
EavenHuang asked EavenHuang commented

Powshell - how to understand such variables in scripts

Dear friends,

I was googling around trying to get a script to help automatically add different users into different groups and came into below script.

I didn't really understand how to use these
$VARIABLES = @("")
or
(@("UserName") + $faculties)
or
if ($user.$faculty)
or
$user.$faculty

Can anyone help to explain a bit more what these variables mean? Many thanks.

The .csv contains different headers like username, group1(FACULTY01), group2(FACULTY02), group3(FACULTY03), etc..


$faculties = @("FACULTY01", "FACULTY02", "FACULTY03", "FACULTY04")
$csv = Import-csv C:\temp\test.csv -Header (@("UserName") + $faculties)

foreach ($user in $csv | Select-Object -Skip 1)
{
foreach ($faculty in $faculties)
{
if ($user.$faculty)
{
Add-ADGroupMember -Identity $user.$faculty -Member $user.UserName
}
}
}

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered EavenHuang commented

The first just creates an array consisting of a single element that is an empty string.

 $VARIABLES = @("")
    
 $variables.gettype()
    
 IsPublic IsSerial Name                                     BaseType
 -------- -------- ----                                     --------
 True     True     Object[]                                 System.Array
    
 $variables.count
    
 1

The second adds whatever is in the variable "$faculties" to the unnamed array that has a single element having the value "UserName". The result is a new array (also unnamed in that example).

The "if ($user.$faculty)" tests whether the value of the property whose name in the "$faculty" variable of the object in the "$user" variable is "true" or "false". The "$user.$faculty" returns that same value.

Say you had a PSCustomObject stored in the $user variable and that PSCustomObject had, for example, two properties named "A" and "B" that held the values "John" and "Mary", respectively; and, in the $faculty variable you have a value of "B". The $user.$faculty expression would return the value "Mary".

 $user = [PSCustomObject]@{
     A = 'John'
     B = 'Mary'
 }
 $faculty = 'B'
 "The value of property $faculty is $($user.$faculty)"
 $faculty = 'A'
 "The value of property $faculty is $($user.$faculty)"

I've added comments to your script:

 # these are the names of the columns that hold the group names to
 # which the user will be added
 $faculties = @("FACULTY01", "FACULTY02", "FACULTY03", "FACULTY04")
 #Create an array of PSCustomObjects in $csv by importing the contents
 # of the CSV file. The -Header assigns the names of the columns.
 # The column names will be "UserName", "FACULTY01", "FACULTY02", etc.
 $csv = Import-Csv C:\temp\test.csv -Header (@("UserName") + $faculties)
    
 # Process each row of the CSV. Skip the 1st row because it holds the original
 # header from the CSV file, not any actual user
 foreach ($user in $csv | Select-Object -Skip 1) {
     # Get the group name from the column FACULTY01, then FACULTY02, etc.
     foreach ($faculty in $faculties) {
         # if there's a group name in this column . . .
         if ($user.$faculty) {
             # make the user in the UserName column a member of the group
             # in the $faculty column
             Add-ADGroupMember -Identity $user.$faculty -Member $user.UserName
         }
     }
 }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Dear Rich,

I would like to say thanks a lot for your help! it's very clear for me to understand in detail, and it's exactly what I'm looking for!

Thanks again for your great support!

0 Votes 0 ·