Enumerating Computer Account Attributes

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

By retrieving and reviewing computer account attributes you can do such things as identify the computers owned by each department, or compile a list of the computers that are located in a particular location (city, building, floor, room). This information is very useful in a number of situations, from planning new equipment allocations to making preparations to move a group of users from one building to another.

You can use the ADSI IADs Get method to retrieve the attributes of any computer account in Active Directory. You simply bind to the appropriate computer account and then echo the value of the desired attributes.

Creating a script to return computer account attributes is easy, but there is one caveat you must be aware of. Although you can retrieve any attribute by using ADSI, a problem can occur if any of these attributes have a null value (that is, if no value has been assigned to the attribute). For example, suppose you create a computer account and assign values only to the mandatory attributes. In that instance, all the optional attributes, including such properties as Location and Description, will have a null value, which means no value has been assigned to the attribute.

Although a script can retrieve attributes with null values, an error will occur if you attempt to do things such as assign that value to a variable or echo the value to the screen. For example, if you attempt to report the value, you will receive the following error message:

The property could not be found in the cache. 

To avoid getting this error, your script must do two things:

  1. Include the On Error Resume Next statement at the beginning of the script. This will prevent the script from failing if an attribute has a null value.
  2. Check each retrieved attribute to ensure that the value is not null before attempting to do anything else with it. For example, before echoing the value of the computer account Location attribute, check to ensure that the computer actually has a location.

You can check for null values by using the function IsNull in Microsoft® Visual Basic® Scripting Edition (VBScript). The following code sample tests to see if the value assigned to the variable strLocation is null. If it is, a message is echoed.

If IsNull(strLocation) Then     Wscript.Echo "The location has not been configured for this computer." Else     Wscript.Echo "Location: " & strLocation End If 

Scripting Steps

Listing 9.5 contains a script that enumerates the values of two attributes of a computer account. To carry out this task, the script must perform the following steps:

  1. Include the On Error Resume Next statement. This will prevent the script from failing if the value of either the Location or Description attribute is null.
  2. Use a GetObject call to bind to the computer account.
  3. Use the Get method to retrieve the computer location.
  4. Use the IsNull function to check whether a value has been configured for the Location attribute, and do one of the following:
    • If no location has been configured, echo a message stating that the Location attribute has not been configured.
    • If a location has been configured, echo the value of the Location attribute.
  5. To retrieve the computer description, repeat steps 2 and 3, using the Get method to retrieve the Description attribute and then echoing the value of that attribute. If the value is null, echo the message "The description has not been configured for this computer."

This same approach can be used to retrieve additional computer account attributes.

Listing 9.5   Enumerating Computer Account Attributes

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 
On Error Resume Next Set objComputer = GetObject _     ("LDAP://CN=atl-dc-01, CN=Computers, DC=fabrikam, DC=com") objProperty = objComputer.Get("Location") If IsNull(objProperty) Then     Wscript.Echo "The location has not been set for this computer." Else     Wscript.Echo "Location: " & objProperty     objProperty = Null End If objProperty = objComputer.Get("Description") If IsNull(objProperty) Then     Wscript.Echo "The description has not been set for this computer." Else     Wscript.Echo "Description: " & objProperty     objProperty = Null End If

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

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