Creating Multiple Organizational Units: Step-by-Step Exercises


In this exercise, we will explore the use of a text file to hold the name of multiple OUs we wish to create in Active Directory.

  1. Open Notepad or some other script editor.

  2. Locate the image from book StepByStep.txt file (this contains the list of OUs your script will create) in the Chapter 7 folder. Make sure you have the exact path to this file. On the first line of your script, create a variable called $aryText. Use this variable to hold the object that is returned by the Get-Content cmdlet. Specify the path to the image from book StepByStep.txt file as the value of the path argument. The line of code that does this is shown here:

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

  3. When the Get-Content cmdlet is used, it creates an array from a text file. To walk through each element of the array, we will use the ForEach cmdlet. Use a variable called $aryElement to hold the line from the $aryText array. This line of code is shown here:

     forEach ($aryElement in $aryText)

  4. Begin your code block section by opening a curly bracket. This is shown here:

     {

  5. Use the variable $strClass to hold the string “organizationalUnit” because this is the kind of object we will be creating in Active Directory. The line of code to do this is shown here:

     $strCLass = "organizationalUnit"

  6. The name of each OU we are going to create comes from each line of the image from book StepByStep.txt file. In our text file, to simplify the coding task, we included ou= as part of each OU name. The $strOUName that will be used in the Create command is a straight value assignment of one variable to another variable. This line of code is shown here:

     $StrOUName = $aryElement

  7. The next line of code in our code block is the one that connects into Active Directory by using the [ADSI] accelerator. We are going to use the LDAP provider and connect to the NwTraders.msft domain. We assign the object that is created to the $objADSI variable. This line of code is shown here:

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

  8. Now we are ready to actually create the OUs in Active Directory. To do this, we will use the Create method. We specify two properties to the Create method: the name of class to create and the name of the object to create. Here, the name of the class is stored in the variable $strClass. The name of the object to create is stored in the $strOUName variable. The object that is returned is stored in the $objOU variable. This line of code is shown here:

     $objOU = $objADSI.create($strCLass, $StrOUName)

  9. To write changes back to Active Directory, we use the SetInfo() method. This is shown here:

     $objOU.setInfo()

  10. Now we must close the code block. To do this, close it with a curly bracket, as shown here:

     }

  11. Save your script as yournameimage from book StepByStep.ps1. Run your script. You should see five OUs created off the root of your domain. If this is not the case, compare your script with the image from book StepByStep.ps1 script in the Chapter 7 folder.

  12. This concludes the creating multiple organizations units step-by-step 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