Modifying Computer Objects with Active Directory

In this conclusion to a four-part series on how Active Directory handles computers, you will learn how to modify the attributes of a computer object, change the default container for computers, and more. This article is excerpted from chapter eight of the Active Directory Cookbook, Second Edition, written by Robbie Allen and Laura E. Hunter (O'Reilly; ISBN: 059610202X). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
January 31, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Modifying the Attributes of a Computer Object

Problem

You want to modify one or more attributes of a computer object.

Solution

Using a graphical user interface

  1. Open ADSI Edit.

  2. If an entry for the naming context you want to browse is not already displayed, do the following: 

    a. Right-click on ADSI Edit in the right pane and click “Connect to....”

    b. Fill in the information for the naming context, container, or OU you want to add an object to. Click on the Advanced button if you need to enter alternate credentials.

    c. In the left pane, browse to the container or OU that contains the computer object you want to modify. Once you’ve found the object, right-click on it and select Properties.

  3. Right-click the attribute you want to modify and select Edit.

  4. Enter the new value and click OK.

  5. Click Apply, followed by OK.

Using a command-line interface

Create an LDIF file called modify_object.ldf with the following contents:

  dn: <ComputerDN>
  changetype: modify
  add: <AttributeName>
  <AttributeName>: <AttributeValue>
  -

Then run the following command:

  > ldifde -v -i -f modify_object.ldf

To modify an object using AdMod, you’ll use the following general syntax:

  > admod –b <ComputerDN> <attribute>:<operation>:<value>

For example, you can add a location to a computer object using the following syntax:

  > admod -b cn="Fin101,cn=Computers,dc=rallencorp,dc=com"
  location::"Berlin, Germany"

Using VBScript

  ' The following code will modify the location attribute
  ' of a computer object.
  Set objComputer = GetObject ("LDAP://<ComputerDN>")

  objComputer.Put "Location" , "<NewLocationValue>"
  objComputer.SetInfo

Discussion

Like all objects within Active Directory, computer objects have various attributes that can be queried, modified, and deleted during the day-to-day management of your domain. Because computer objects inherit from the user class, they include similar informational attributes to the user objects, as well as attributes that are specific to computer objects, including:

  1. Location
  2. Description
  3. operatingSystemVersion
  4. operatingSystemServicePack
  5. sAMAccountName
  6. pwdLastSet
  7. primaryGroupID

See Also

Recipe 8.10 for finding inactive or unused computers, Recipe 8.13 for finding computers with a particular OS, and MSDN: Computer System Hardware Classes [WMI]

Finding Computers with a Particular OS

Problem

You want to find computers that have a certain OS version, release, or service pack in a domain.

Solution

Using a graphical user interface

  1. Open LDP.
  2. From the menu, select Connection -> Connect.
  3. For Server, enter the name of a domain controller (or leave blank to do a serverless bind).
  4. For Port, enter 389.
  5. Click OK.
  6. From the menu, select Connection -> Bind.
  7. Enter credentials of a user to perform the search.
  8. Click OK.
  9. From the Menu, select Browse -> Search.
  10. For Base DN, enter the base of where you want your search to begin.
  11. For Filter, enter a filter that contains the OS attribute you want to search on. For example, a query for all computers that are running Windows XP would be the following:

      (&(objectclass=computer)(objectcategory=computer)(operatingSystem=Windows XP
      Professional))

  12. Select the appropriate Scope based on how deep you want to search.
  13. Click the Options button if you want to customize the list of attributes returned for each matching object.
  14. Click Run, and the results will be displayed in the right pane.

You can also perform this search using the Active Directory Users and Computers MMC snap-in (dsa.msc), as follows:

  1. Open the ADUC MMC snap-in.
  2. Right-click on the domain, OU, or container that you wish to search, and click Find.
  3. In the Find drop-down box, select Computers.
  4. Click on the Advanced tab. Click on Field and select Operating System.
  5. Select the Condition that you want to search on from one of the following:

    • Starts with

    • Ends with

    • Is (exactly)

    • Is not

    • Present

    • Not present

  6. In the Value field, enter the value that you want to search for, such as “Windows Server 2003.”

  7. Click Find Now.

Using a command-line interface

You can query for computer objects of a particular operating system using either DSQuery or AdFind. To perform the query with DSQuery, use the following syntax:

  > dsquery * <DomainDN> -scope subtree -attr "*" -filter "(&(objectclass=
  computer)(objectcategory=computer)(operatingSystem=Windows Server 2003))"

To use AdFind, enter the following:

  > adfind –b <DomainDN> -s subtree –f
    "(&(objectclass=computer)(objectcategory=computer)
    (operatingSystem=Windows Server 2003))"

Using VBScript

  ' This code searches for computer objects that have Service Pack 1 installed.
  ' ------ SCRIPT CONFIGURATION ------
  strBase    = "<LDAP://" & "<DomainDN>" & ">;"
  ' ------ END CONFIGURATION ---------

  strFilter = "(&(objectclass=computer)(objectcategory=computer)" & _ 
        "(operatingSystemServicePack=Service Pack 1));"
  strAttrs  = "cn,operatingSystem,operatingSystemVersion," & _
        " operatingSystemServicePack;"
  strScope  = "subtree"

  set objConn = CreateObject("ADODB.Connection")
  objConn.Provider = "ADsDSOObject"
  objConn.Open "Active Directory Provider"
  Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
  objRS.MoveFirst
  while Not objRS.EOF
     Wscript.Echo objRS.Fields(0).Value
     Wscript.Echo objRS.Fields(1).Value
     Wscript.Echo objRS.Fields(2).Value
     Wscript.Echo objRS.Fields(3).Value
     Wscript.Echo objRS.Fields(4).Value
     WScript.Echo
     objRS.MoveNext
  wend

Discussion

When a computer joins an Active Directory domain, the operating system attributes are updated for the computer object. There are four of these attributes, which can be used in queries to find computers that match certain OS-specific criteria, like service pack level.

These attributes include the following:

operatingSystem
   Descriptive name of the installed Operating System—
   e.g., Windows Server 2003, Windows 2000 Server,
   and Windows XP Professional

operatingSystemVersion
   Numerical representation of the operating system—
   e.g., 5.0 (2195) and 5.2 (3757)

operatingSystemServicePack
   Current service pack level if one is installed—e.g.,
   Service Pack 2 and Service Pack 3

This recipe typically applies only to Windows-based machines. Other types of machines (e.g., Unix) that have accounts in Active Directory might not automatically update their OS attributes, though some newer Unix- or Linux-based NAS devices have been configured to do. Additionally, theoperatingSystem attribute does not distinguish between Windows NT 4 server and Windows NT 4 workstation.

Binding to the Default Container for Computers

This recipe requires the Windows Server 2003 domain functional level.

Problem

You want to bind to the default container that new computer objects are created in.

Solution

Using a graphical user interface

  1. Open LDP.
  2. From the menu, select Connection -> Connect.
  3. For Server, enter the name of a domain controller (or leave blank to do a serverless bind).
  4. For Port, enter 389.
  5. Click OK.
  6. From the menu, select Connection -> Bind.
  7. Enter credentials of a domain user.
  8. Click OK.
  9. From the menu, select View -> Tree.
  10. For the DN, enter: 

     <WKGUID=aa312825768811d1aded00c04fd8d5cd,
    <DomainDN>> 

    where <DomainDN> is the distinguished name of a domain .
  11. Click OK.
  12. In the lefthand menu, you can now browse the default computers container for the domain.

Using a command-line interface

With tools like netdom, if there is an option to specify only the name of the computer and not its DN or parent container, the computer object will be created in the default Computers container by default. You can use the redircmp utility to change this default location, as we will discuss in Recipe 8.15.

Using VBScript

  ' This code illustrates how to bind to the default computers container.
  ' ------ SCRIPT CONFIGURATION ------
  strDomain = "<DomainDNSName>"  ' e.g. apac.rallencorp.com
  ' ------ END CONFIGURATION ---------

  ' Computer GUID as defined in ntdsapi.h
  Const ADS_GUID_COMPUTRS_CONTAINER = "aa312825768811d1aded00c04fd8d5cd"

  set objRootDSE = GetObject("LDAP://" & strDomain & "/RootDSE")
 
set objCompContainer = GetObject("LDAP://<WKGUID=" & _ 
                 ADS_GUID_COMPUTRS_CONTAINER & "," & _
                 objRootDSE.Get("defaultNamingContext") & ">" )

  WScript.Echo objCompContainer.Get("distinguishedName")

Discussion

In much the same way that the TCP/IP protocol defines a list of well-known ports that are commonly used by industry applications (TCP 20 and 21 for FTP, TCP port 80 for HTTP, etc.), Active Directory defines Well-Known GUIDs that map to container objects that are present in every AD installation. The Domain NC defines the following WKGUIDs:

  • Users
  • Computers
  • System
  • Domain Controllers
  • Infrastructure
  • Deleted Objects
  • Lost and Found

The Configuration NC also defines its ownDeleted Objects WKGUID.

For example, the default computers container has the following WKGUID:

  aa312825768811d1aded00c04fd8d5cd

You can use the GUID to bind to the default computers container in the domain using the following ADsPath:

 LDAP://<WKGUID=aa312825768811d1aded00c04fd8d5cd, dc=apac,dc=rallencorp,dc=com>

The list of well-known objects for a domain is contained in thewellKnownObjectsattribute of thedomainDNS object for the domain. ThewellKnownObjectsattribute is multivalued with DNWithBinary syntax. The following is an example of what that attribute looks like for the rallencorp.com domain:

  B:32:AA312825768811D1ADED00C04FD8D5CD:CN= Computers,DC=rallencorp,DC=com; 
  B:32:F4BE92A4C777485E878E9421D53087DB:CN= Microsoft,CN=Program
  Data,DC=rallencorp,DC=com; 
  B:32:09460C08AE1E4A4EA0F64AEE7DAA1E5A:CN= Program Data,DC=rallencorp,DC=com;
  B:32:22B70C67D56E4EFB91E9300FCA3DC1AA:
  CN=ForeignSecurityPrincipals,DC= rallencorp,DC=com;
  B:32:18E2EA80684F11D2B9AA00C04F79F805:CN= Deleted Objects,DC=rallencorp,DC=com;
  B:32:2FBAC1870ADE11D297C400C04FD8D5CD:CN= Infrastructure,DC=rallencorp,DC=com;
  B:32:AB8153B7768811D1ADED00C04FD8D5CD:CN= LostAndFound,DC=rallencorp,DC=com;
  B:32:AB1D30F3768811D1ADED00C04FD8D5CD:CN= System,DC=rallencorp,DC=com;
  B:32:A361B2FFFFD211D1AA4B00C04FD7D83A:OU= Domain Controllers,DC=rallencorp,DC=com;
  B:32:A9D1CA15768811D1ADED00C04FD8D5CD:CN= Users,DC=rallencorp,DC=com;

Each value has the format of:

  B:NumberofBytes:GUID:DistinguishedName

As you can see, the GUID for the first value is the same as the one we used in the ADsPath above to bind to the default computers container.

See Also

Recipe 8.15 for changing the default computers container, and MSDN: Binding to Well-Known Objects Using WKGUID

Changing the Default Container for Computers

Problem

You want to change the container that computers are created in by default.

Solution

Using a graphical user interface

  1. Open LDP.

  2. From the menu, select Connection -> Connect.
  3. For Server, enter the name of a domain controller (or leave blank to do a serverless bind).
  4. For Port, enter 389.
  5. Click OK.
  6. From the menu, select Connection -> Bind.
  7. Enter credentials of a domain user.
  8. Click OK.
  9. From the menu, select Browse -> Modify.
  10. For DN, enter the distinguished name of the domainDNS object of the domain you want to modify.
  11. For Attribute, enterwellKnownObjects.
  12. For Values, enter the following: 

      B:32:AA312825768811D1ADED00C04FD8D5 CD:CN=Computers,<DomainDN >

    where<DomainDN>is the same as the DN you enter for the DN field.
  13. Select Delete for the Operation and click the Enter button.
  14. Go back to the Values field and enter the following: 
      
     B:32:AA312825768811D1ADED00C04FD8D5CD: <NewComputersParent>,
    <DomainDN>

    where <NewComputersParent>is the new parent container for new computer objects (e.g.,ou=RAllenCorp Computers).
  15. Select Add for the Operation and click the Enter button.
  16. Click the Run button.

    The result of the operations will be displayed in the right pane of the main LDP window.

Using a command-line interface

  > redircmp "<NewParentDN>"

 Using VBScript

  ' This code changes the default computers container.
  ' ------ SCRIPT CONFIGURATION ------
  strNewComputersParent = "<NewComputersParent>" ' e.g. OU=RAllenCorp Computers
  strDomain             = "<DomainDNSName>" ' e.g. rallencorp.com
  ' ------ END CONFIGURATION ----------

  Const COMPUTER_WKGUID = "B:32:AA312825768811D1ADED00C04FD8D5CD:"
  ' ADS_PROPERTY_OPERATION_ENUM
  Const ADS_PROPERTY_APPEND = 3
  Const ADS_PROPERTY_DELETE = 4

  set objRootDSE = GetObject("LDAP://" & strDomain & "/RootDSE")
  set objDomain = GetObject("LDAP://" & objRootDSE.Get("defaultNamingContext"))
  set objCompWK = GetObject("LDAP://" & _
 "<WKGUID=AA312825768811D1ADED00C04FD8D5CD," & _
objRootDSE.Get("defaultNamingContext") & ">")

  objDomain.PutEx ADS_PROPERTY_DELETE, "wellKnownObjects", _
      Array( COMPUTER_WKGUID & objCompWK.Get("distinguishedName"))
  objDomain.PutEx ADS_PROPERTY_APPEND, "wellKnownObjects", _
                    Array( COMPUTER_WKGUID & strNewComputersParent & "," &
   
objRootDSE.Get("defaultNamingContext") )
  objDomain.SetInfo
  WScript.Echo "New default Computers container set to " & _
                strNewComputersParent

Discussion

Most Active Directory administrators do not use the Computers container within the Domain naming context as their primary computer repository. One reason is that since it is a container and not an OU, you cannot apply Group Policy Objects to it. If you have another location where you store computer objects, you might want to consider changing the default container used to bind to the computers container by changing the well-known objects attribute, as shown in this recipe. This can be beneficial if you want to ensure computers cannot sneak into Active Directory without having the appropriate group policies applied to them. While you can also apply GPOs at the site or the domain level, forcing new computers into a particular Organizational Unit ensures that those computers receive the Group Policy settings that you want them to receive through GPOs linked at the OU level. However, this does not protect you from an administrator (whether intentionally or accidentally) explicitly creating a computer object in the incorrect OU; this only protects you from applications or utilities that do not allow or do not require you to specify an OU when creating the computer.

See Recipe 8.14 for more information on how well-known objects are specified in Active Directory.

See Also

MS KB 324949 (Redirecting the Users and Computers Containers in Windows Server 2003 Domains)

Listing All the Computer Accounts in a Domain

Problem

You want to obtain a list of all computer accounts in an Active Directory domain.

Solution

Using a graphical user interface

  1. Open the Active Directory Users and Computers MMC snap-in.
  2. Right-click on the domain node and select Find.
  3. In the Find drop-down box, select Computers and click Find Now.

    All computer objects in the domain will be displayed in the Search Results window.

Using a command-line interface

  > adfind –default –f (objectCategory=computer)

Using VBScript

  ' The following script will enumerate all computer accounts
  ' within an Active Directory domain.

  Const ADS_SCOPE_SUBTREE = 2
  strDomain = "<DomainDN>"

  Set objConnection = CreateObject("ADODB.Connection")
  Set objCommand = CreateObject("ADODB.Command")
  objConnection.Provider = "ADsDSOObject"
  objConnection.Open "Active Directory Provider"

  Set objCOmmand.ActiveConnection = objConnection
  objCommand.CommandText = _
      "Select Name, Location from 'LDAP://" & strDomain & "' " _
        
& "Where objectCategory='computer'"
  objCommand.Properties("Page Size") = 1000
  objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
  Set objRecordSet = objCommand.Execute
  objRecordSet.MoveFirst

  Do Until objRecordSet.EOF
     
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
     
Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
     
objRecordSet.MoveNext
  Loop

Discussion

Using VBScript

To obtain a list of domain controllers, rather than just computer objects, you should query the Configuration NC rather than the domain NC, and replace "where objectCategory=computer" with "where objectCategory=ntDSDSA".

See Also

MSDN: Object Class and Object Category [Active Directory] and MSDN: Object-Class Attribute [AD-Schema]

Identifying a Computer Role

Problem

You want to identify the role that a particular computer serves in an Active Directory domain. 

Solution

Using a graphical user interface

  1. Open the Active Directory Users and Computers MMC snap-in.
  2. Right-click on the domain node and select Find.
  3. In the Find drop-down box, select Computers and click Find Now.

    The role of each computer will be displayed in the Machine Role column in the Search Results window.

Using a command-line interface

  > wmic computersystem get domainrole

For a domain controller that holds the PDC Emulator FSMO role, this will return the following output:

  DomainRole
  5

For a DC that doesn’t hold the PDCe FSMO, this command will return a value of 4.

Using VBScript

  ' The following code will return the domain role of the
  ' local computer.
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!" _
      & strComputer & "\root\cimv2")
  Set colComputers = objWMIService.ExecQuery _
      ("Select DomainRole from Win32_ComputerSystem")
  For Each objComputer in colComputers
      Select Case objComputer.DomainRole
          Case 0
              strComputerRole = "Standalone Workstation"
          Case 1
              strComputerRole = "Member Workstation"
          Case 2
              strComputerRole = "Standalone Server"
          Case 3
              strComputerRole = "Member Server"
          Case 4
              strComputerRole = "Backup Domain Controller"
          Case 5
             
strComputerRole = "Primary Domain Controller"
      End Select
      Wscript.Echo strComputerRole
  Next

Discussion

Using a command-line interface

WMIC is the command-line component of the Windows Management Instrumentation that uses aliases to enable you to easily access WMI namespaces from the command line. To run wmic against a remote computer, specify the /node:"<ComputerFQDN> " switch.

Using VBScript

Rather than relying on an if...else construct to produce output, this script uses Select Case. In situations where there are numerous possible outcomes for a conditional statement, Select Case can produce far more elegant code than using numerous if...else statements.

blog comments powered by Disqus
WINDOWS SCRIPTING ARTICLES

- More Windows Scripting Workarounds from Nilpo
- Overloading Methods and More in VBScript
- Improving MFC for Windows Vista
- Regular Expressions in VBScript
- Working with Dates in WMI
- Completing Calendars with VBScript Date Func...
- Building Calendars with VBScript Date Functi...
- Working With Dates and Times in VBScript
- Designing WCF DataContract Classes Using the...
- Understanding Dates and Times in VBScript
- Working With Arrays in VBScript
- Compressed Folders in WSH
- Using .NET Interops in VBScript
- Nilpo`s Scripting Secrets, Vol I
- Database operations using Silverlight 2.0 WC...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials