One Step Further: Creating Multivalued Users


In this exercise, we create nine temporary user accounts using concatenation. We specify values for the users from a text file and populate attributes on both the Address tab and the Telephone tab.

  1. Open Notepad or your favorite Windows PowerShell script editor.

  2. Use the Get-Content cmdlet to open the image from book OneStepFurther.txt file. Use the path argument to point to the exact path to the file. Hold the array that is created in a variable called $aryText. This line of code is shown here:

     $aryText = Get-Content -Path "c:\labs\ch7\OneStepFurther.txt"

  3. Create a variable called $strUser. This will be used to determine the class of object to create in Active Directory. Assign the string “user” to this variable. This line of code is shown here:

     $strCLass = "User"

  4. Create a variable called $intUsers. This variable will be used to determine how many users to create. For this exercise, we will create nine users, so assign the integer 9 to the value of the variable. This code is shown here:

     $intUsers = 9

  5. Create a variable called $strName. This variable will be used to create the prefix for each user that is created. Because these will be temporary users, called the prefix cn=tempuser. This code is shown here:

     $strName = "cn=tempUser"

  6. Create a variable called $objADSI. This variable will be used to hold the object that is returned by using the [ADSI] accelerator that is used to make the connection into Active Directory. Specify the LDAP provider, and connect to the MyTestOU that resides in the NwTraders.msft domain. This line of code is shown here:

     $objADSI = [ADSI]"LDAP://ou=myTestOU,dc=nwtraders,dc=msft"

  7. Use a for loop to count from 1 to 9. Use the $i variable as the counter-variable. When the value of $i is less than or equal to the integer stored in the $intUsers variable, exit the loop. Use the $i++ operator to increment the value of $i. This code is shown here:

     for ($i=1; $i -le $intUsers; $i++)

  8. Open and close your code block by using curly brackets. This is shown here:

     { }

  9. Between the curly brackets, use the object contained in the $objADSI variable to create the class of object stored in the variable $strClass. The name of each object will be created by concatenating the $strName prefix with the number current in $i. Store the object returned by the Create method in the variable $objUser. This line of code is shown here:

     $objUser = $objADSI.create($strCLass, $StrName+$i)

  10. On the next line in the code block, write the New User object to Active Directory using the SetInfo() method. This line of code is shown here:

     $objUser.setInfo()

  11. Open the image from book OneStepFurther.txt file, and examine the contents. Note that each line corresponds to a property in Active Directory. The trick is to ensure that each line in the text file matches each position in the array. Beginning at element, use the array contained in the variable $aryText to write the streetaddress, postofficebox, l, st, postalcode, c, co, countrycode, facsimiletelephonenumber, and info attributes for each User object that is created. This section of code, shown here, is placed after the User object is created, and SetInfo() writes it to Active Directory.

     $objUser.put("streetAddress", $aryText[0]) $objUser.put("postOfficeBox", $aryText[1]) $objUser.put("l", $aryText[2]) $objUser.put("st", $aryText[3]) $objUser.put("postalCode" , $aryText[4]) $objUser.put("c", $aryText[5]) $objUser.put("co", $aryText[6]) $objUser.put("countryCode", $aryText[7]) $objUser.Put("facsimileTelephoneNumber", $aryText[8]) $objUser.Put("info", $aryText[9])

  12. Commit the changes to Active Directory by calling the SetInfo() method. This line of code is shown here:

     $objUser.setInfo()

  13. Save your script as yournameimage from book OneStepFurtherPt1.ps1. Run your script, and examine Active Directory Users and Computers. You should find the nine users with attributes on both the Address tab and the Telephones tab. If this is not the case, then compare your script with the image from book OneStepFurtherPt1.ps1 script. After the users are created, proceed to part 2.

  14. Save image from book OneStepFurtherPt1.ps1 as yournameimage from book OneStepFurtherPt2.ps1.

  15. Delete the $aryText = Get-Content -Path "c:\labs\ch7\OneStepFurther.txt" from the script.

  16. Delete everything from inside the code block except for the line of code that creates the User object. This line of code is: $objUser = $objADSI.create($strCLass, $StrName+$i). The code to delete is shown here:

     $objUser.setInfo() $objUser.put("streetAddress", $aryText[0]) $objUser.put("postOfficeBox", $aryText[1]) $objUser.put("l", $aryText[2]) $objUser.put("st", $aryText[3]) $objUser.put("postalCode" , $aryText[4]) $objUser.put("c", $aryText[5]) $objUser.put("co", $aryText[6]) $objUser.put("countryCode", $aryText[7]) $objUser.Put("facsimileTelephoneNumber", $aryText[8]) $objUser.Put("info", $aryText[9]) $objUser.setInfo()

  17. Inside the code block, change the create method in the $objADSI Create command to delete, as shown here:

     $objUser = $objADSI.Delete($strCLass, $StrName+$i)

  18. Save and run your script. You should see the nine users, created earlier, disappear. If this does not happen, compare your script with the image from book OneStepFurtherPt2.ps1 script.

  19. This concludes this one step further exercise.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net