One Step Further: Controlling How a Script Executes Against Active Directory


In this exercise, we will control the way we return data from Active Directory.

  1. To make it easier to keep up the number of users returned from our Active Directory queries, run the image from book DeleteMultipleUsers.ps1 script. This will delete the 60 users we created for the previous step-by-step exercise.

  2. Run the image from book Create2000Users.ps1 script. This script will create 2000 users for you to use in the MyTestOU OU.

  3. Open the image from book QueryUsersStepbyStep.ps1 script and save it as yournameimage from book OneStepFurtherQueryUsers.ps1.

  4. Because we are not interested in running finely crafted queries in this exercise (rather, we are interested in how to handle large amounts of objects that come back), delete all the $strFilter commands except for the one that filters out User objects. This line of code is shown here:

     $strFilter = "(objectCategory=User)"

  5. Save and run your script. You will see 1000 user names scroll by in your Windows PowerShell console window. After about 30 seconds (on my machine anyway), you will finally see MyLabUser997 show up. The reason it is MyLabUser997 instead of MyLabUser1000 is that this OU already had three users when we started (myBoss, myDirect1, and myDirect2). This is OK; it is easy to see that the query returned the system default of 1000 objects.

  6. We know, however, there are more than 2000 users in the MyTestOU, and we have only been able to retrieve 1000 of them. To get past the query limit that is set for Active Directory, we need to turn on paging. This is simple. We assign a value for the PageSize property to be less than the 1000 object limit. To do this, we use the Item method of the properties collection on the Command object and assign the value of 500 to the Page-Size property. This line of code is shown here. Place this code just above this line, which creates the RecordSet object: $objRecordSet = $objCommand.Execute().

     $objCommand.Properties.item("Page Size") = 500

  7. After you have made the change, save and run your script. You should see all 2000 user objects show up however, the results may be a little jumbled. Without using a SortObject or specifying the Sort property on the server, the values are not guaranteed to be in order. This script takes about a minute or so on my computer.

  8. To tell Active Directory we do not want any size limit, specify the SizeLimit property as 0. We can do this by using the Item method of the properties collection on the Command object. This line of code is shown here:

     $objCommand.Properties.item("Size Limit") = 0

  9. To make the script a bit more efficient, change the script to perform an asynchronous query (synchronous being the default). This will reduce the network bandwidth consumed and will even out the processor load on your server. To do this, declare a variable called $blnTrue and set it equal to the Boolean type. Assign the value –1 to it. Place this code just under the line that creates the $strQuery variable. This line of code is shown here:

     $blnTrue = [bool]-1

  10. Under the line of code that sets the size limit, use the Item method of the properties collection to assign the value true to the asynchronous property of the Command object. Use the Boolean value you created and stored in the $blnTrue variable. This line of code is shown here:

     $objCommand.Properties.item("Asynchronous") = $blnTrue

  11. Save and run your script. You should see the script run perhaps a little faster because it is doing an asynchronous query. If your script does not run properly, compare your script with the image from book OneStepFurtherQueryUsers.ps1 script.

  12. To clean up after this lab, run the image from book Delete2000Users.ps1 script. It will delete the 2000 users we created at the beginning of the exercise.

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