index
int64
0
4.08k
text
stringlengths
34
4.9k
url
stringclasses
38 values
96
3.10. Removing an Unsuccessfully Demoted Domain Controller Problem You want to manually remove a domain controller from Active Directory if the demo‐ tion process was unsuccessful or you are unable to bring a domain controller back online after a hardware or software failure. Solution Use the following steps to remove a domain controller: 1. Go to the Windows command line and type ntdsutil. 2. From the ntdsutil menu, type metadata cleanup. 3. Type remove selected server cn=<ServerName>,cn=Servers,cn=<SiteName>, cn=Sites,cn=Configuration,dc=<ForestRootDomain> to remove the server met‐ adata associated with dc1.adatum.com. If successful, a message will state that the removal was complete. However, if you receive an error message, check to see if the server’s nTDSDSA object (e.g., cn=NTDSSet tings,cn=DC5,cn=Servers,cn=MySite1,cn=Sites,cn=Configuration,dc=ada tum,dc=com) is present. If so, the demotion process may have already removed it, and it will take time for the change to replicate. If it is still present, try the ntdsutil procedure again, and if that doesn’t work, manually remove that object and the parent object (e.g., cn=DC5) using ADSI Edit or another tool. (Deleting Active Directory objects is discussed in Recipe 4.24.) Follow these additional steps to remove all traces of the domain controller: 1. Delete the CNAME record from DNS for <GUID>._msdcs.<RootDomainDNSName>, where <GUID> is the objectGUID for the server’s nTDSDSA object as obtained via ADSI Edit or a command-line tool such as AdFind. You’ll need to manually check and delete any associated SRV records. Delete any A and PTR records that exist for the server. When using Microsoft DNS, you can use the DNS MMC snap-in to accom‐ plish these tasks. 2. Delete the computer object for the server under ou=DomainControl lers,<DomainDN>. This can be done using the Active Directory Users and Com‐ puters snap-in or PowerShell. (Deleting objects is described in Chapter 4.) 3. Delete the FRS Member object for the computer contained under cn=DomainSys temVolume (SYSVOL share),cn=file replication service,cn=sys tem,<DomainDN>. This can be done using the Active Directory Users and Computers 3.10. Removing an Unsuccessfully Demoted Domain Controller | 71
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
97
snap-in when Advanced Features has been selected from the View menu (so the System container will be displayed), or with the AdMod tool. 4. Delete the server object associated with the failed domain controller in the Active Directory Sites and Services MMC. Discussion If the domain controller that you are forcibly removing from Active Directory is the last one in an Active Directory domain, you’ll need to manually remove the domain from the forest as well. See Recipe 2.5 for more information on removing orphaned domains. Here are some additional issues to consider when you forcibly remove a domain controller: • Seize any FSMO roles the DC may have had to another domain controller. (Man‐ aging FSMO roles is discussed later in this chapter.) • If the DC was a global catalog server, ensure there is another global catalog server configured in the site that can handle the increased workload. • If the DC was a DNS server, ensure that there is another DNS server that can handle the additional name resolution queries, and be sure that your clients are configured to use the correct name server. • If the DC was the RID FSMO master, check to make sure duplicate SIDs have not been issued (see Recipe 2.25). • Check to see if the DC hosted any application partitions, and if so, consider making another server a replica server for those application partitions (see Recipe 16.8). If the (former) domain controller that you forcibly removed is still active or otherwise returns to your network, you should strongly consider reinstalling the operating system to avoid potential conflicts from the server trying to reinsert itself back into Active Directory. See Also Recipe 2.5 for removing an orphaned domain; Recipe 2.25; Recipe 3.31 for seizing FSMO roles; Recipe 4.24; Recipe 16.8 72 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
98
3.11. Renaming a Domain Controller Problem You want to rename a domain controller. Solution Your first step in renaming a domain controller is as follows, where <NewName> is a fully qualified domain name (FQDN): > netdom computername <CurrentName> /Add:<NewName> The new name will be automatically replicated throughout Active Directory and DNS. Once you’ve verified that the new name has replicated (which may take some time depending on your replication topology), you can designate it as the domain controller’s primary name as follows, and then reboot the domain controller: > netdom computername <CurrentName> /MakePrimary:<NewName> See Chapter 12 for information on verifying Active Directory replication. Once you’re satisfied that your clients are accessing the domain controller using its new name, you can remove the old computer name using the following syntax: > netdom computername <NewName> /remove:<OldName> Discussion An option in the netdom utility allows an alternate computer name to be associated with a computer in Active Directory. Once you’ve added a new name, you can then set that name to be the primary name, thereby renaming the computer. The old name effectively remains with the domain controller until you remove it, which can be done using the netdom computername /Remove:<Name> command. You should reboot the server before removing the old name. The old names are stored in the msDS￾AdditionalDnsHostName and msDS-AdditionalSamAccountName attributes on the do‐ main controller’s computer object. If the domain controller has any version of Microsoft Exchange installed on it, renaming the domain controller is unsupported. 3.11. Renaming a Domain Controller | 73
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
99
3.12. Finding the Domain Controllers for a Domain Problem You want to find the domain controllers in a domain. Solution Using a graphical user interface 1. Open the Active Directory Users and Computers snap-in (dsa.msc). 2. Right-click on the target domain and select Find. 3. In the Find drop-down box, select Computers. 4. In the Role drop-down box, select Writable Domain Controllers or Read-Only Domain Controllers. 5. Click Find Now. The list of domain controllers for the domain will be present in the search results pane. Using PowerShell To find all of the domain controllers in the adatum.com domain, use the following command: Get-ADDomainController -Filter { domain -eq "adatum.com" } | select Name Discussion There are several ways to get a list of domain controllers for a domain. The GUI solution simply uses the built-in “Find” functionality of the Active Directory Users and Com‐ puters MMC. The PowerShell solution uses a dedicated cmdlet for getting information about domain controllers. For yet another solution, see Recipe 3.26 to find out how to query DNS to get the list of domain controllers for a domain. See Also Recipe 3.26 for finding domain controllers via DNS 74 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
100
3.13. Finding the Closest Domain Controller Problem You want to find the closest domain controller for a particular domain. Solution Using a command-line interface The following command finds the closest domain controller in the specified domain (<DomainDNSName>); that is, a domain controller that is located in the same site or in the closest site if a local DC is not available. By default, it will return the closest DC for the computer from which nltest is being run, but you can optionally use the /server option to target a remote host. If you are interested in finding a DC within a particular site regardless of whether it is the closest DC to you, you can also optionally specify the /site option to find a domain controller that belongs to a particular site: > nltest/dsgetdc:<DomainDNSName> [/site:<SiteName>] [/server:<ClientName>] Using PowerShell Get-ADDomainController -Discover The preceding command will discover the closest domain controller from the computer where the command is run. Discussion The DC locator process defines how clients find the closest domain controller. The process uses the site topology stored in Active Directory to calculate the site a particular client is in. After the client site has been identified, it is a matter of finding a domain controller that is a member of that same site or that is covering for that site. The Microsoft DsGetDcName Directory Services API method implements the DC Lo‐ cator process, but unfortunately it cannot be used directly from a scripting language, such as VBScript. The nltest /dsgetdc command is also a wrapper around the DsGetDcName method, and it is a handy tool when troubleshooting client issues related to finding an optimal domain controller. Using a command-line interface You can use nltest to return the closest domain controller that is serving a particular function. Some of the available functions include a global catalog server (/GC switch), time server (/TIMESERV switch), KDC (/KDC switch), and PDC (/PDC switch). Run nltest /? from a command line for the complete list. 3.13. Finding the Closest Domain Controller | 75
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
101
Using PowerShell Similar to nltest, you can specify additional criteria for finding a domain controller by using the -Filter parameter. The following are some of the most used filters: IsGlobalCatalog IsReadOnly Site Service 3.14. Finding a Domain Controller’s Site Problem You need to determine the site of which a domain controller is a member. Solution Using a command-line interface To retrieve the site for a particular DC, use the following command syntax: > nltest /dsgetsite /server:<DomainControllerName> The nltest /dsgetsite command is a wrapper around the DsGetSi teName method. Using PowerShell Get-ADDomainController -Server <DomainControllerName> | FL Name,Site Discussion Domain controllers are represented in the site topology by a server object and a child nTDSDSA object. Actually, any type of server can conceivably have a server object; it is the nTDSDSA object that differentiates domain controllers from other types of servers. You’ll often see the nTDSDSA object of a domain controller used to refer to that domain controller elsewhere in Active Directory. For example, the fSMORoleOwner attribute that represents the FSMO owners contains the distinguished name of the nTDSDSA object of the domain controller that is holding the role. Finding a domain controller’s site using a GUI solution is time-consuming but can be accomplished by using LDP or Active Directory Sites and Services. 76 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
102
See Also Recipe 3.13 3.15. Moving a Domain Controller to a Different Site Problem You want to move a domain controller to a different site. Solution Using a graphical user interface 1. Open the Active Directory Sites and Services snap-in (dssite.msc). 2. In the left pane, expand the site that contains the domain controller. 3. Expand the Servers container. 4. Right-click on the domain controller you want to move and select Move. 5. In the Move Server box, select the site to which the domain controller will be moved and click OK. Using a command-line interface When using DSMove, you must specify the DN of the object you want to move. In this case, it needs to be the distinguished name of the server object for the domain con‐ troller. The value for the -newparent option is the distinguished name of the Servers container you want to move the domain controller to: > dsmove "<ServerDN>" -newparent "<NewServersContainerDN>" For example, the following command would move dc2 from the Default-First-Site￾Name site to the Raleigh site: > dsmove "cn=dc2,cn=servers,cn=Default-First-Site-Name,cn=sites,↵ cn=configuration,cn=adatum,dc=com" -newparent↵ "cn=servers,cn=Raleigh,cn=sites,cn=configuration,cn=adatum,dc=com" You can also move an object using AdMod, as follows: > admod -b cn=<ServerName>,cn=servers,cn=<OldSite>,cn=sites,↵ cn=configuration,<ForestRootDN> -move cn=servers,cn=<NewSite>,↵ cn=sites,cn=configuration,<ForestRootDN> Using PowerShell Move-ADDirectoryServer -Identity <DomainControllerName> -Site <NewSite> 3.15. Moving a Domain Controller to a Different Site | 77
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
103
Discussion When you install a new domain controller, a server object and nTDSDSA object for the domain controller get added to the site topology. The Knowledge Consistency Checker (KCC) and Intersite Topology Generator (ISTG) use these objects to determine whom the domain controller should replicate with. A domain controller is assigned to the site that has been mapped to the subnet it is located on. If there is no subnet object that has an address range that contains the domain controller’s IP address, the server object is added to the Default-First-Site￾Name site. If the domain controller should be in a different site, you’ll then need to manually move it. It is a good practice to ensure that a subnet object that matches the domain controller’s subnet is already in Active Directory before promoting the server into the forest. That way you do not need to worry about moving it after the fact. When moving a server object, remember that it has to be moved to a Servers container within a site, not directly under the site itself. Using a command-line interface In the solution provided, you need to know the current site of the domain controller you want to move. If you do not know the site it is currently in, you can use DSQuery to find it. In fact, you can use DSQuery in combination with DSMove in a single com‐ mand line: > for /F "usebackq" %i in ('dsquery server↵ -name"<DomainControllerName>"') do dsmove -newparent "cn=servers,↵ cn=Default-First-Site,cn=sites, cn=configuration,<ForestDN>" %i This command is long, so we’ll break it up into three parts to clarify it. The first part contains the for command extension that is built into the cmd.exe shell. When the /F "usebackq" syntax is specified, it is typically used to iterate over output from a command and perform certain functions on the output. for /F "usebackq" %i in The next part of the for loop contains the data to iterate over. In this case, we use DSQuery to return the distinguished name of the server object for dc2: ('dsquery server -name "<DomainControllerName>"') The last part executes a command for each result returned from DSQuery. In this case, there should only be one result, so this command will run only once: do dsmove -newparent "cn=servers,cn=Default-First-↵ Site,cn=sites,cn=configuration,<ForestDN>" %i 78 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
104
See Also Recipe 3.14 for finding a domain controller’s site; Recipe 4.20 for moving objects to different containers 3.16. Finding the Services a Domain Controller Is Advertising Problem You want to find the services that a domain controller is advertising. Solution The following command will display the list of services a domain controller is advertising: > dcdiag /v /s:<DomainControllerName> /test:advertising Running this command on a typical domain controller will produce the following output: Starting test: Advertising The DC dc1 is advertising itself as a DC and having a DS. The DC dc1 is advertising as an LDAP server The DC dc1 is advertising as having a writable directory The DC dc1 is advertising as a Key Distribution Center The DC dc1 is advertising as a time server The DS dc1 is advertising as a GC. You can also use nltest to get similar information: > nltest /server:<DomainControllerName> /dsgetdc:<DomainName> Running this command on a domain controller in the adatum.com domain will produce the following output: DC: \\dc1.adatum.com Address: \\10.0.0.1 Dom Guid: ac0e4884-cf79-4c9d-8cd9-817e3bfdab54 Dom Name: adatum.com Forest Name: adatum.com Dc Site Name: Raleigh Our Site Name: Raleigh Flags: PDC GC DS LDAP KDC TIMESERV GTIMESERV WRITABLE DNS_DC DNS_DOMAIN DNS_FOREST CLOSE_SITE 3.16. Finding the Services a Domain Controller Is Advertising | 79
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
105
In the previous example, GTIMESERV denotes a DC that is a master time server. WRITABLE denotes a DC that holds a writable copy of the Active Directory database. Prior to Windows Server 2008, only NT 4.0 BDCs would not possess this flag. Since 2008, Read-Only Domain Controllers will also lack the WRITABLE flag. Discussion The dcdiag /test:advertising command is a wrapper around the DsGetDcName method. DsGetDcName returns a structure called DOMAIN_CONTROLLER_INFO that con‐ tains the list of services a domain controller provides. Table 3-4 contains the possible values returned from this call. Table 3-4. DOMAIN_CONTROLLER_INFO flags Value Description DS_DS_FLAG Directory server for the domain. DS_GC_FLAG Global catalog server for the forest. DS_KDC_FLAG Kerberos Key Distribution Center for the domain. DS_PDC_FLAG Primary domain controller of the domain. DS_TIMESERV_FLAG Time server for the domain. DS_WRITABLE_FLAG Hosts a writable directory service. See Also MSDN: DsGetDcName; MSDN: DOMAIN_CONTROLLER_INFO 3.17. Restoring a Deleted Domain Controller in Windows Server 2012 Problem You want to restore the computer account of a domain controller that has been acci‐ dentally deleted. Solution Using a graphical user interface The following solution requires that the Active Directory Recycle Bin feature has been enabled and that the feature was enabled prior to the deletion of the domain controller computer object. 80 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
106
1. Launch the Active Directory Administrative Center. 2. In the left pane, select the domain and then double-click the Deleted Objects container in the right pane. 3. In the Filter box near the top of the Active Directory Administrative Center, enter the domain controller name to narrow down the displayed objects to the domain controller object. 4. Right-click the domain controller object and then click Restore. Using a command-line interface This command-line solution uses a traditional restoration approach by performing an authoritative restore. To restore the computer account, use the following sequence of commands in Windows Server 2012: > ntdsutil > activate instance ntds > authoritative restore> restore object <DomainControllerDN> > quit > exit Restart the domain controller after running these commands. Using PowerShell The PowerShell solution requires that the Active Directory Recycle Bin feature be en‐ abled and that the feature was enabled prior to the deletion of the domain controller computer object. To restore the computer account for a domain controller named DC1, use the following PowerShell command: Get-ADObject -Filter {Name -eq "dc1" -and ObjectClass -eq "computer"}↵ -IncludeDeletedObjects | Restore-ADObject Discussion The Active Directory Recycle Bin has greatly simplified the restoration of AD objects. Now, deleted objects can be restored in their entirety without rebooting a domain con‐ troller or recovering data from backup media. In addition, the restore process is much faster, literally just minutes. Without the use of the Active Directory Recycle Bin, when you restore a deleted object within Active Directory you have the option of performing an authoritative or a non‐ authoritative restore. In both cases, any changes that have been made to the AD database subsequent to the time that the backup was taken will be replicated back to the restored DC. With an authoritative restore, the version number of the object(s) being restored is incremented so that the restored objects will “win” in the case of any replication 3.17. Restoring a Deleted Domain Controller in Windows Server 2012 | 81
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
107
collisions. In a case where you want to restore an object that has been inadvertently deleted, you need to perform an authoritative restore to prevent the deletion from re‐ propagating to the restored domain controller. You can mark an entire restore as au‐ thoritative, or any subtree of your AD environment down to a single object (in this case, the computer object for the DC that was deleted). Using PowerShell The PowerShell solution uses two filters: one for Name and one for ObjectClass. Al‐ though filtering by just the name will find and restore the object, it may also restore noncomputer objects as well (e.g., an object in the msDFSR-Member object class will have the same name). Instead of using two filters, you can also find the specific object that you want to recover and then recover it directly by specifying the object GUID. See Also Chapter 16 for more on recovering and restoring Active Directory 3.18. Resetting the TCP/IP Stack on a Domain Controller Problem You want to uninstall and reinstall the TCP/IP stack on a domain controller as part of a disaster recovery or troubleshooting operation. Solution Using a command-line interface > netsh int tcp reset <Log_File_Name> Discussion Resetting the TCP/IP stack using netsh will remove all configuration information, in‐ cluding the default gateway and any configured DNS and WINS servers. This procedure might be necessary during a disaster recovery situation where you’re restoring System State data to a server with a dissimilar hardware configuration, for example, as the restore process might corrupt the TCP/IP stack on the destination computer. Using a command-line interface In addition to resetting the TCP/IP stack, you can also reset Winsock using the following command: > netsh winsock reset 82 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
108
Use this command with care, though, as resetting Winsock can cause network applica‐ tions such as antivirus scanners to malfunction and require reinstallation. A reboot is required to complete the Winsock reset. See Also MS KB 299357 (How to reset Internet Protocol (TCP/IP)) 3.19. Configuring a Domain Controller to Use an External Time Source Problem You want to set the reliable time source for a domain controller. Solution Using the Registry To configure a domain controller to sync to an external time provider, set the following Registry keys: [HKLM\System\CurrentControlSet\Services\W32Time\Parameters\] Type: REG_SZ - "NTP" [HKLM\System\CurrentControlSet\Services\W32Time\Config\] AnnounceFlags: REG_DWORD - 10 [HKLM\System\CurrentControlSet\Services\W32Time\TimeProviders\] NTPServer: REG_DWORD - 1 [HKLM SYSTEM\CurrentControlSet\Services\W32Time\Parameters\] NTPServer: REG_SZ -<Peers> [HKLM \SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\ NtpClient\] SpecialPollInterval: REG_DWORD -<TimeBetweenPollsInSeconds> [HKLM \SYSTEM\CurrentControlSet\Services\W32Time\Config\] MaxPosPhaseCorrection: REG_DWORD -<MaximumForwardOffsetInSeconds> [HKLM \SYSTEM\CurrentControlSet\Services\W32Time\Config\] MaxNegPhaseCorrection: REG_DWORD -<MaximumBackwardOffsetInSeconds> 3.19. Configuring a Domain Controller to Use an External Time Source | 83
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
109
<Peers> in the preceding code refers to a space-separated list of FQDNs of external time servers. Each DNS name must be followed by ,0x1 for the rest of these settings to take effect. Once you have made these changes to the Registry, stop and restart the W32time service by issuing the following commands: > net stop w32time > net start w32time Using a command line w32tm /config /syncfromflags:manual /manualpeerlist:<FQDNofTimeServer> w32tm /config /update Discussion You should set a reliable time source on the PDC Emulator FSMO for only the forest root domain. All other domain controllers sync their time either from that server, from a PDC within their own domain, or from a designated time server on another domain controller. The list of external time servers is stored in the Registry under the W32Time Service Registry key: HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Parame ters\ntpserver. If you want a domain controller such as the PDC to use an external time source, you have to set the ntpserver Registry value along with the type value. The default value for type on a domain controller is Nt5DS, which means that the domain controller will use the Active Directory domain hierarchy to find a time source. You can override this behavior and have a domain controller contact a non-DC time source by setting type to NTP. After setting the time server, the W32Time service should be restarted for the change to take effect. You can check that the server was set properly by running the following command: > w32tm /query /computer:localhost /configuration Since the PDC Emulator is the time source for the other domain controllers, you should also make sure that it is advertising the time service, which you can do with the following command: > nltest /server:<DomainControllerName> /dsgetdc:<DomainDNSName> /TIMESERV 84 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
110
To configure the PDC Emulator to use its own internal clock as a time source instead of relying on an external clock, modify the HKLM\SYSTEM \CurrentControlSet\Services\W32Time\Config\AnnounceFlags DWORD value to contain a value of 0x0A. The algorithm used by domain controllers to sync time gets quite complex. See the next section for links to additional details on how the Windows time service works. See Also “Configure the Time Source for the Forest”; “How Windows Time Service Works” 3.20. Finding the Number of Logon Attempts Made Against a Domain Controller Problem You want to find the number of logon requests a domain controller has processed. Solution The following query returns the number of logon requests processed: > nltest /server:<DomainControllerName> /LOGON_QUERY This will produce output similar to the following: Number of attempted logons: 10542526 Discussion The nltest /LOGON_QUERY command is a wrapper around the I_NetLogonControl2 method, and it can be useful to determine how many logon requests are being processed by a server. Viewing the results of the command over a period of time and comparing them against another DC in the same domain can also tell you if one domain controller is being used significantly more or less than the others. See Also MSDN: I_NetLogonControl2 3.20. Finding the Number of Logon Attempts Made Against a Domain Controller | 85
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
111
3.21. Enabling the /3GB Switch to Increase the LSASS Cache Problem You have installed more than 1 GB of memory on your 32-bit domain controllers and want to enable the /3GB switch so that the LSASS process can use more memory. Solution Using a command-line interface On a 32-bit Windows Server 2008 server, run the following command: > bcdedit /set IncreaseUserVA 3072 Restart the computer. Discussion When computers are referred to as 32- or 64-bit computers, it means they support memory addresses that are 32 or 64 bits long. This is the total available memory (virtual and real) that can be processed by the system. Since the days of Windows NT, Microsoft has split memory allocation in half by giving applications up to 2 GB and the Windows kernel 2 GB of memory to use (32 bits of address space = 2^32 = 4 GB). In many cases, administrators would rather allocate more memory to applications than to the kernel. For this reason, Microsoft developed the /3GB switch to allow applications running on 32-bit versions of Windows to use up to 3 GB of memory, leaving the kernel with 1 GB. This configuration is not necessary for 64-bit versions of Windows. 3.22. Enabling and Disabling the Global Catalog Problem You want to enable or disable the global catalog (GC) on a particular server. 86 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
112
Solution Using a graphical user interface 1. Open the Active Directory Sites and Services snap-in (dssite.msc). 2. Browse to the nTDSDSA object (NTDS Settings) underneath the server object for the domain controller for which you want to enable or disable the global catalog. 3. Right-click on NTDS Settings and select Properties. 4. Under the General tab, check (to enable) or uncheck (to disable) the box beside Global Catalog. 5. Click OK. Using a command-line interface In the following command, <ServerObjectDN> should be the server object DN, not the DN of the nTDSDSA object: > dsmod server "<ServerObjectDN>" -isgc yes|no For example, the following command will enable the global catalog on DC1 in the Ra‐ leigh site: > dsmod server "cn=DC1,cn=servers,cn=Raleigh,cn=sites,cn=configuration,dc=adatum,dc=com"↵ -isgc Yes You can also use AdMod with the following syntax and output to disable the GC; to enable it, use options::{{.:CLR:1}}: > adfind -b "cn=NTDS Settings,cn=dc1,cn=Servers,cn=Raleigh,cn=Sites,cn=Configuration,dc=adatum,↵ dc=com" options -adcsv | admod options::{{.:SET:1}} See Chapter 4 for information on safely modifying bitwise operators. Using PowerShell Set-ADObject "cn=NTDS Settings,cn="<DomainControllerName>,cn=Servers,↵ cn=<SiteName>,;cn=Sites,cn=Configuration,dc="<DomainName>,dc=<TopLevelDomain>"↵ -Replace @{Options='1'} 3.22. Enabling and Disabling the Global Catalog | 87
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
113
Discussion The first domain controller promoted into a forest is also made a global catalog (GC) server by default. In a single-domain environment, the global catalog server incurs no memory or bandwidth overhead beyond that of a domain controller, so you could con‐ figure each DC in a single-domain forest as a GC without any ill effects. In a multidomain environment, however, each global catalog server will require additional disk space to store a partial replica of other domains in the forest, and will require additional network bandwidth to replicate with other GCs. For more details on DC and GC placement planning, see Active Directory, Fifth Edition, by Brian Desmond et al. (O’Reilly). The global catalog on a domain controller becomes enabled when the low-order bit on the options attribute on the nTDSDSA object under the server object for the domain controller is set to 1; it becomes disabled when it is set to 0. The DN of this object for DC1 in the Default-First-Site-Name site looks like this: cn=NTDSSettings,cn=DC1,cn=Default-First-Site-Name,cn=Sites,cn=Configuration,↵ dc=adatum,dc=com After enabling the global catalog, it can take some time before the domain controller can start serving as a global catalog server. The length of time is based on the amount of data that needs to replicate and the type of connectivity between the domain con‐ troller’s replication partners. This is also dependent on the Global Catalog Partition Occupancy setting, which is set in the HKLM\System\CurrentControlSet\Services \NTDS\Parameters key on the GC itself, which specifies how many directory partitions must be fully replicated to the GC before it is considered ready; this can range from no occupancy requirement whatsoever, to requiring that all partitions be fully synchron‐ ized before the GC can begin servicing requests. After replication is complete, you should see Event 1119 in the Directory Services log stating the server is advertising itself as a global catalog. At that point you should also be able to perform LDAP queries against port 3268 on that server. See Recipe 3.23 for more information on how to determine whether global catalog promotion is complete. See Also Recipe 3.23 for determining whether global catalog promotion is complete; “Under‐ standing the Global Catalog” 88 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
114
3.23. Determining Whether Global Catalog Promotion Is Complete Problem You want to determine whether a domain controller is a global catalog server. After you initially enable the global catalog on a domain controller, it can take some time for all of the read-only naming contexts to replicate to it, depending on the number of domains, the volume of directory data, and the underlying network topology. Solution Query the isGlobalCatalogReady attribute on the RootDSE for the domain controller. A TRUE value means the server is a global catalog, and a FALSE value indicates it is not. For more information on how to query the RootDSE, see Recipe 4.1. You can also check the Directory Services Event Log in the Event Viewer MMC for the presence of Event ID 1119, whose text reads as follows: "This Windows Domain Controller is now a Global Catalog Server" Using a command-line interface To confirm that a domain controller in the adatum.com domain named dc1 is func‐ tioning as a global catalog server, use nltest with the following syntax: > nltest /server:dc1.adatum.com /dsgetdc:adatum.com If the DC in question is functioning as a GC, you’ll see output similar to the following: > C:\>nltest /dsgetdc:adatum.com > DC: \\dc1.adatum.com > Address: \\10.0.0.1 > Dom Guid: ac0e4884-cf79-4c9d-8cd9-817e3bfdab54 > Dom Name: adatum.com > Forest Name: adatum.com > Dc Site Name: Raleigh > Our Site Name: Raleigh > Flags: PDC GC DS LDAP KDC TIMESERV GTIMESERV WRITABLE DNS_DC DNS_DOMAIN > DNS_FOREST CLOSE_SITE > The command completed successfully Using PowerShell Get-ADDomainController -Server <DomainControllerName> |↵ FT Name,IsGlobalCatalog -AutoSize 3.23. Determining Whether Global Catalog Promotion Is Complete | 89
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
115
Discussion Once a server has completed initial replication of the global catalog, the attribute is GlobalCatalogReady in the RootDSE will be set to TRUE. Another way to determine if a domain controller has been at least flagged to become a global catalog is by checking whether the options attribute on the nTDSDSA object for the server has been set to 1. (Note that this does not necessarily mean the server is accepting requests as a global catalog.) An additional query to the RootDSE as described in the “Solution” on page 89 or directly to port 3268 (the global catalog port) could also confirm that the appropriate flag has been set. See Also Recipe 4.1 for viewing the RootDSE 3.24. Finding the Global Catalog Servers in a Forest Problem You want a list of the global catalog servers in a forest. Solution Using a command-line interface To enumerate all GCs in a forest using DSQuery, use the following syntax: > dsquery server -forest -isgc Using PowerShell To find all global catalogs in the current domain, use the following syntax: Get-ADDomainController -Filter { IsGlobalCatalog -eq $true } | Select Name To find all global catalogs in the forest, use the following two PowerShell commands: $GCs = Get-ADForest $GCs.GlobalCatalogs Discussion To find the global catalog servers in a forest, you need to query for NTDS Settings objects that have the low-order bit of the options attribute equal to 1 under the Sites container in the Configuration naming context. That attribute determines whether a domain controller should be a global catalog server, but it does not necessarily mean it 90 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
116
is a global catalog server yet. See Recipe 3.23 for more information on how to tell whether a server marked as a global catalog is ready to accept requests as one. See Also Recipe 3.23 for determining whether global catalog promotion is complete 3.25. Finding the Domain Controllers or Global Catalog Servers in a Site Problem You want a list of the domain controllers or global catalog servers in a specific site. Solution Using a graphical user interface 1. Open the Active Directory Administrative Center. 2. In the left pane, double-click the domain name. 3. Double-click the Domain Controllers OU. 4. In the right pane, you can view the Domain Controller Type column to see if a domain controller is a global catalog server. Global catalog servers will have the appropriate box checked beside Global Catalog. Using PowerShell To find all of the domain controllers in SiteA for the current domain, use the following syntax: Get-ADDomainController -Filter { Site -eq "SiteA" } | FL Name To find all GCs in SiteA for the current domain, use the following syntax: Get-ADDomainController -Filter { Site -eq "SiteA" -and↵ ( IsGlobalCatalog -eq "True" )} | FL Name To find all global catalogs in SiteA in the entire forest, use the following syntax: $for = [System.DirectoryServices.ActiveDirectory.Forest]::getCurrentForest() $dom.FindAllGlobalCatalogs("SiteA") 3.25. Finding the Domain Controllers or Global Catalog Servers in a Site | 91
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
117
Discussion Each domain controller has a server object within the Servers container for the site it is a member of (e.g., cn=DC1,cn=Servers,cn=MySite,cn=site,cn=configura tion,dc=adatum,dc=com). Since other types of servers can have server objects in a site’s Servers container, domain controllers are differentiated by the nTDSDSA object that is a child of the server object (e.g., cn=NTDSSettings,cn=DC1,cn=Servers,cn=My Site,cn=site,cn=configuration,dc=adatum,dc=com). Querying for this nTDSDSA object will return a list of domain controllers in the site. Locating global catalog servers consists of the same query, except where the low-order bit of the options attribute of the nTDSDSA object is equal to 1. Note that this may not be available if replication has not completed after enabling the GC. 3.26. Finding Domain Controllers and Global Catalogs via DNS Problem You want to find domain controllers or global catalogs using DNS lookups. Solution Domain controllers and global catalog servers are represented in DNS as SRV records. You can query SRV records using nslookup by setting type=SRV, such as in the following: > nslookup Default Server: dns01.adatum.com Address: 10.1.2.3 > set type=SRV You then need to issue the following query to retrieve all writable domain controllers for the specified domain: > _ldap._tcp.<DomainDNSName> You can issue a similar query to retrieve global catalogs: > _gc._tcp You can even find the domain controllers or global catalogs that are in a particular site or that cover a particular site by querying the following: > _ldap._tcp.<SiteName>._sites > _gc._tcp.<SiteName>._sites See Recipe 11.20 for more information on site coverage. 92 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
118
Discussion One of the benefits of Active Directory over its predecessor, Windows NT, is that it relies on DNS for name resolution, which is the standard for name resolution on the Internet and on most TCP/IP-based networks. Active Directory uses DNS to locate servers that serve a particular function, such as a domain controller for a domain, global catalog server, PDC Emulator, or KDC. It also uses the site topology information stored in Active Directory to populate site-specific records for domain controllers. The DC locator process relies on this information in DNS to direct clients to the most optimal server when logging in. Reliance on DNS makes it easy to troubleshoot prob‐ lems related to clients finding domain controllers. If you know the site a client is in, you can make a few DNS queries to determine which domain controller they should be using to authenticate. The resource records that a domain controller registers in DNS can be restricted, if you have a lag site configured, for example, so querying DNS may return only a subset of the actual domain controllers that are available. See Recipes 12.13 and 12.14 for more information. See Also Recipe 12.13; Recipe 12.14; Recipe 3.32 for finding the PDC Emulator via DNS; MS KB 267855 (Problems with Many Domain Controllers with Active Directory Integrated DNS Zones); RFC 2782, “A DNS RR for Specifying the Location of Services (DNS SRV)” 3.27. Changing the Preference for a Domain Controller Problem You want a particular domain controller to be used less frequently for client requests, or not at all. This may be necessary if a particular domain controller is overloaded, perhaps due to numerous application requests. Solution You can modify the Priority or Weight field in SRV resource records by modifying the registry on the domain controller. Open regedit or regedit32 on the domain controller and browse to the following key: HKLM\SYSTEM\CurrentControlSet\Services\Netlo gon\Parameters. To configure the priority, add a REG_DWORD with the name LdapSrv Priority. To configure the weight, add a REG_DWORD with the name LdapSrvWeight. After you make the change, the %SystemRoot%\System32\Config\netlogon.dns file should be updated and the DDNS updates sent to the DNS server within an hour. You can also restart the NetLogon service to expedite the process. 3.27. Changing the Preference for a Domain Controller | 93
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
119
Discussion Each domain controller registers several SRV records that clients use as part of the DC locator process to find the closest domain controller. Two fields of the SRV record let clients determine which server to use when multiple possibilities are returned. The Priority field is used to dictate whether a specific server or set of servers should always be contacted over others unless otherwise unavailable. A server with a higher priority (i.e., lower Priority field value) will always be contacted before a server with a lower priority. For example, if DC1 has an SRV priority of 5 and DC2 has an SRV priority of 10, DC1 will always be used unless it is unavailable. The Weight field, on the other hand, determines the percentage of time clients should use a particular server. You can easily calculate the percentage by dividing the weight by the sum of all weights for servers with the same priority. If servers DC1, DC2, and DC3 have weights of 1, 2, and 3, respectively, then DC1 will be contacted one out of six times or (1 / (3 + 2 + 1)), DC2 will be contacted two out of every six times or 1/3 (2 / (3 + 2 + 1)), and DC3 will be contacted three out of every six times or 1/2 (3 / (3 + 2 + 1)). Here is an example of how the SRV records look with these weights: C:\> nslookup -type=SRV _ldap._tcp.dc._msdcs.adatum.com Server: dns01.adatum.com Address: 172.16.168.183 _ldap._tcp.dc._msdcs.adatum.com SRV service location: priority = 0 weight = 1 port = 389 svr hostname = dc1.adatum.com _ldap._tcp.dc._msdcs.adatum.com SRV service location: priority = 0 weight = 2 port = 389 svr hostname = dc2.adatum.com _ldap._tcp.dc._msdcs.datum.com SRV service location: priority = 0 weight = 3 port = 389 svr hostname = dc3.datum.com In certain situations, having this capability can come in handy. For example, the server acting as the PDC FSMO role owner typically receives more traffic from clients simply because of the nature of tasks that the PDC FSMO has to handle. If you find a certain server, like the PDC FSMO, has considerably higher load than the rest of the servers, you could change the priority or weight of the SRV records so that the server is used less often during the DC locator process. You can increase the Priority to eliminate its 94 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
120
use unless all other domain controllers fail, or modify the Weight to reduce how often it will be used. You can modify this information manually within the DNS Management Console, or for multiple DCs using Group Policy Objects in the Computer Configuration\Admin istrative Templates\System\Net Logon\DC Locator DNS Records GPO node. See Also MS KB 232025 (Description of the DNS SRV Resource Record Type) 3.28. Disabling the Global Catalog Requirement for User Logon Problem You want to disable the requirement for a global catalog server to be reachable when a user logs in to a Windows domain. Solution See Recipe 7.12 for information on enabling universal group caching, which can reduce the need to contact a global catalog server during logon for universal group expansion. 3.29. Finding the FSMO Role Holders Problem You want to find the domain controllers that are acting as one of the FSMO roles. Solution Using a graphical user interface For the Schema Master: 1. Open the Active Directory Schema snap-in. 2. Right-click on Active Directory Schema in the left pane and select Operations Master. For the Domain Naming Master: 1. Open the Active Directory Domains and Trusts snap-in (domain.msc). 3.28. Disabling the Global Catalog Requirement for User Logon | 95
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
121
2. Right-click on Active Directory Domains and Trusts in the left pane and select Operations Master. For the PDC Emulator, RID Master, and Infrastructure Master: 1. Open the Active Directory Users and Computers snap-in (dsa.msc). 2. Make sure you’ve targeted the correct domain. 3. Right-click on Active Directory Users and Computers in the left pane and select Operations Masters. 4. Work your way through the individual tabs for the PDC, RID, and Infrastructure roles. Using a command-line interface In the following command, you can leave out the /Domain <DomainDNSName> option to query the domain you are currently logged in to: > netdom query fsmo /Domain:<DomainDNSName> To query the owner of an individual FSMO role, you can use the dsquery server command shown here, where <Role> can be schema, name, infr, pdc, or rid: > dsquery server -hasfsmo <Role> Using PowerShell The following command will query the forest-level FSMO roles: Get-ADForest | FL DomainNamingMaster,SchemaMaster The following command will query the domain-level FSMO roles for the specified domain: Get-ADDomain -Identity <DomainDNSName> |↵ FL InfrastructureMaster,PDCEmulator,RIDMaster Discussion Several Active Directory operations are sensitive, such as updating the schema, and therefore need to be restricted to a single domain controller to prevent corruption of the AD database. This is because Active Directory cannot guarantee the proper evaluation of these functions in a situation where they may be invoked from more than one DC. The FSMO mechanism is used to limit these functions to a single DC. Five designated FSMO roles correspond to these sensitive functions. A FSMO role can apply either to an entire forest or to a specific domain. Each role is stored in the fSMOR 96 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
122
oleOwner attribute on various objects in Active Directory depending on the role. Table 3-5 contains a list of FSMO roles. Table 3-5. FSMO roles Role Description fSMORoleOwner location Domain- or forest-wide? Schema Processes schema updates. cn=Schema,cn=Configura tion,<ForestDN> Forest Domain Naming Processes the addition, removal, and renaming of domains. cn=Partitions cn=Configu ration,<ForestDN> Forest Infrastructure Maintains references to objects in other domains. cn=Infrastructure,<Do mainDN> Domain RID Handles RID pool allocation for the domain controllers in a domain. cn=RidManager$,cn=Sys tem,<DomainDN> Domain PDC Emulator Receives preferential password replication, handles user authentication after another DC reports bad password, handles account lockouts. <DomainDN> Domain Using PowerShell For a quick method of retrieving the FSMO role holders in a forest or domain, simply retrieve the properties of the forest or domain object, as follows: Get-ADForest or: Get-ADDomain See Also Recipe 3.13; “Using Ntdsutil.exe to transfer or seize FSMO roles to a domain controller” 3.30. Transferring a FSMO Role Problem You want to transfer a FSMO role to a different domain controller. This may be necessary if you need to take a current FSMO role holder down for maintenance. 3.30. Transferring a FSMO Role | 97
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
123
Solution Using a graphical user interface 1. Use the same directions as described in Recipe 3.29 for viewing a specific FSMO, except target (i.e., right-click and select Connect to Domain Controller) the domain controller you want to transfer the FSMO to before selecting Operations Master. 2. Click the Change button. 3. Click OK twice. You should then see a message stating whether the transfer was successful. Using a command-line interface The following will transfer the PDC Emulator role to <NewRoleOwner> (see “Discus‐ sion” on page 96 section for information on transferring the other roles): > ntdsutil roles conn "co t s <NewRoleOwner>" q "transfer PDC" q q Using PowerShell The following command will transfer the PDC Emulator role to a DC named DC1: Move-ADDirectoryServerOperationMasterRole DC1 -PDCEmulator The following will transfer the RID Master role to another DC named DC1; this syntax can be used for all FSMO role holders except for the PDC Emulator: Move-ADDirectoryServerOperationMasterRole DC1 -RIDMaster Discussion The first domain controller in a new forest is assigned the two forest-wide FSMO roles (schema and domain naming). The first domain controller in a new domain gets the other three domain-wide roles. It is very likely you’ll need to move the roles around to different domain controllers at some point. Also, when you need to decommission a domain controller that is currently a FSMO role owner (either permanently or for a significant period of time), you’ll want to transfer the role beforehand. If you plan to install a hotfix or do some other type of maintenance that only necessitates a quick reboot, you may not want to go to the trouble of transferring the FSMO role. This is because some FSMO roles are more time-critical than others, and some come into use on a far more frequent basis. For example, the PDC Emulator role is used extensively (and therefore should be transferred to a domain controller of equal or better capacity as a best practice), but the Schema Master is needed only when you are ex‐ tending the schema by installing a new software package, such as Microsoft Exchange. 98 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
124
If a FSMO role owner becomes unavailable before you can transfer it, you’ll need to seize the role (see Recipe 3.31). Using a command-line interface Any role can be transferred using ntdsutil by replacing "transfer PDC" in the solution with one of the following: • "transfer domain naming master" • "transfer infrastructure master" • "transfer RID master" • "transfer schema master" Using PowerShell The FSMO roles can be shortened to simplify the commands to move the roles. Because each role starts with a unique letter, you can transfer them by just referring to them by the first letter of the role name. In addition, you can transfer multiple roles in one command. The following command will transfer all five roles to DC1: Move-ADDirectoryServerOperationMasterRole DC1 -S,D,I,P,R See Also Recipe 3.29 for finding FSMO role holders; Recipe 3.31 for seizing a FSMO role; “Using Ntdsutil.exe to transfer or seize FSMO roles to a domain controller” 3.31. Seizing a FSMO Role Problem You need to seize a FSMO role because the current role holder is down and will not be restored. Solution Using a command-line interface The following will seize the PDC Emulator role to <NewRoleOwner>: > ntdsutil roles conn "co t s <NewRoleOwner>" q "seize PDC" q q Any of the other roles can be transferred as well using ntdsutil by replacing "seize PDC" in the previous solution with one of the following: 3.31. Seizing a FSMO Role | 99
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
125
• "seize domain naming master" • "seize infrastructure master" • "seize RID master" • "seize schema master" Using PowerShell The following will seize the PDC Emulator role to <NewRoleOwner>: > Move-ADDirectoryServerOperationMasterRole <NewRoleOwner> -PDCEmulator -Force Discussion Seizing a FSMO role should not be taken lightly. The general recommendation is to seize a FSMO role only when you cannot possibly bring the previous role holder back online. One reason that seizing a role is problematic is that you could possibly lose data. For example, let’s say that you extended the schema and immediately after it was ex‐ tended the Schema FSMO went down. If you could not bring that server back online, those extensions may not have replicated before the server went down. You would need to determine whether any of the schema extensions replicated and, if not, reextend the schema. Other issues can result from losing the RID FSMO, where duplicate RID pools may be allocated. See Recipe 3.29 for more information. See Also Recipe 3.29 for finding FSMO role holders; Recipe 3.30 for transferring a FSMO role; “Using Ntdsutil.exe to transfer or seize FSMO roles to a domain controller” 3.32. Finding the PDC Emulator FSMO Role Owner via DNS Problem You want to find the PDC Emulator for a domain using DNS. Solution Using a command-line interface > nslookup -type=SRV _ldap._tcp.pdc._msdcs.<DomainDNSName> 100 | Chapter 3: Domain Controllers, Global Catalogs, and FSMOs
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
126
Discussion The PDC Emulator FSMO role is the only FSMO role that is stored in DNS. Like many of the other Active Directory−related DNS records, the PDC record is stored as an SRV record under _ldap._tcp.pdc._msdcs.<DomainDNSName> where <DomainDNSName> is the domain the PDC is in. This allows your Active Directory clients to use normal DNS name resolution to locate the PDC Emulator for their domain. See Also Recipe 3.26 for finding domain controllers via DNS 3.32. Finding the PDC Emulator FSMO Role Owner via DNS | 101
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
127
null
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
128
CHAPTER 4 Searching and Manipulating Objects 4.0. Introduction Active Directory is based on Lightweight Directory Access Protocol (LDAP) and sup‐ ports the LDAP version 3 specification defined in RFC 2251. And while many of the AD tools and interfaces, such as ADSI, abstract and streamline LDAP operations to make things easier, any good AD administrator or developer must have a thorough understanding of LDAP to fully utilize Active Directory. This chapter will cover some of the LDAP-related tasks you may need to perform when working with Active Direc‐ tory, along with other tasks related to searching and manipulating objects within the directory. The Anatomy of an Object The Active Directory schema is composed of a hierarchy of classes that define the types of objects that can be created within Active Directory, as well as the different attributes they can possess. These classes support inheritance, which enables developers to reuse existing class definitions for more than one type of object; for example, the descrip tion attribute is available with every type of AD object, but the attribute itself is defined only once within the schema. At the top of the inheritance tree is the top class, from which every class in the schema is derived. Table 4-1 contains a list of some of the attributes that are available from the top class, and subsequently are defined on every object that is created in Active Directory. 103
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
129
Table 4-1. Common attributes of objects Attribute Description cn RDN attribute for most object classes, also referred to as the common name. whenCreated Timestamp when the object was created. See Recipe 4.26 for more information. description Multivalued attribute that can be used as a generic field for storing a description of the object. Although this attribute is multivalued, objects such as users and groups can have only one value populated due to legacy support requirements. displayName Name of the object displayed in administrative interfaces. distinguishedName Distinguished name of the object. whenChanged Timestamp when the object was last changed by the local server. See Recipe 4.26 for more information. name RDN of the object. The value of this attribute will mirror the naming attribute (e.g., cn, ou, dc). nTSecurityDescriptor Security descriptor assigned to the object. objectCategory Used as a grouping mechanism for objects with a similar purpose (e.g., Person). objectClass List of classes from which the object’s class was derived. objectGUID Globally unique identifier for the object. uSNChanged Update sequence number (USN) assigned by the local server after the last change to the object (can include creation). uSNCreated USN assigned by the local server when the object was created. 4.1. Viewing the RootDSE Problem You want to view attributes of the RootDSE, which can be useful for discovering basic information about a forest, domain, or domain controller without hardcoding the name of a particular naming context into a query. Solution Using a graphical user interface 1. Open LDP. 2. From the menu, select Connection→Connect. 3. For Server, enter a domain controller or domain name, or leave it blank to do a serverless bind. 4. For Port, enter 389. 5. Click OK. 104 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
130
6. The contents of the RootDSE will be shown in the right pane. Using a command-line interface To display the RootDSE of a domain controller using AdFind, use the following syntax: > adfind -rootdse Using PowerShell Get-ADRootDSE You’ll see results similar to the following (truncated for readability): configurationNamingContext: cn=Configuration,dc= adatum,dc=com currentTime: 10/9/2012 9:27:38 AM defaultNamingContext: dc=adatum,dc=com dnsHostName: dc1.adatum.com domainControllerFunctionality: Windows2012 domainFunctionality: Windows2012Domain dsServiceName: cn=NTDS Settings,cn=DC1,cn=Servers,cn=Default-First-Site-Name,↵ cn=Sites,cn=Configuration,dc=adatum,dc=com forestFunctionality: Windows2012Forest highestCommittedUSN: 28013 isGlobalCatalogReady: {TRUE} isSynchronized: {TRUE} ldapServiceName: adatum.com:dc1$@ADATUM.COM namingContexts: {dc=adatum,dc=com, cn=Configuration,dc=adatum,dc=com, cn=Schema,cn=Configuration,dc=adatum,dc=com,dc=DomainDnsZones,dc=adatum,↵ dc=com...} rootDomainNamingContext: dc=adatum,dc=com schemaNamingContext: cn=Schema,cn=Configuration,dc=adatum,dc=com serverName: cn=dc1,cn=Servers,cn=Default-First-Site-Name,cn=Sites,↵ cn=Configuration,dc=adatum,dc=com subschemaSubentry: cn=Aggregate,cn=Schema,cn=Configuration,dc=adatum,dc=com supportedCapabilities: {1.2.840.113556.1.4.800 (LDAP_CAP_ACTIVE_DIRECTORY_OID),↵ ;1.2.840.113556.1.4.1670(LDAP_CAP_ACTIVE_DIRECTORY_V51_OID),↵ ;1.2.840.113556.1.4.1791(LDAP_CAP_ACTIVE_DIRECTORY_LDAP_INTEG_OID),↵ ;1.2.840.113556.1.4.1935(LDAP_CAP_ACTIVE_DIRECTORY_V61_OID)...} supportedControl: {1.2.840.113556.1.4.319(LDAP_PAGED_RESULT_OID_STRING),↵ ;1.2.840.113556.1.4.801(LDAP_SERVER_SD_FLAGS_OID),↵ ;1.2.840.113556.1.4.473(LDAP_SERVER_SORT_OID),↵ ;1.2.840.113556.1.4.528(LDAP_SERVER_NOTIFICATION_OID)...} supportedLDAPPolicies: {MaxPoolThreads, MaxDatagramRecv, MaxReceiveBuffer,↵ ; InitRecvTimeout...} supportedLDAPVersion: {3, 2} supportedSASLMechanisms: {GSSAPI, GSS-SPNEGO, EXTERNAL, DIGEST-MD5} 4.1. Viewing the RootDSE | 105
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
131
Discussion The RootDSE was originally defined in RFC 2251 as part of the LDAPv3 specification. It is not part of the Active Directory namespace per se. It is a synthetic object that is maintained separately by each domain controller. The RootDSE can be accessed anonymously using LDP; the command-line and Pow‐ erShell solutions use the credentials of the currently logged-on user unless you specify an alternate username and password. In the CLI and PowerShell solutions, serverless binds were used against the RootDSE. In that case, the DC Locator process is used to find a domain controller in the domain you authenticate against. This can also be ac‐ complished with LDP by not entering a server name from the Connect dialog box. The RootDSE is key to writing portable AD-enabled applications. It provides a mech‐ anism to programmatically determine the distinguished names of the various naming contexts (among other things), which means that you do not need to hardcode that information in scripts and programs. See Also RFC 2251; MS KB 219005 (Windows 2000: LDAPv3 RootDSE); MSDN: IADsProper‐ tyEntry; MSDN: IADsProperty Value; MSDN: IADs::Get MSDN: IADs::GetEx 4.2. Viewing the Attributes of an Object Problem You want to view one or more attributes of an object. Solution Using a graphical user interface 1. Open LDP. 2. From the menu, select Connection→Connect. 3. For Server, enter the name or IP address of a domain controller or domain that contains the object. 4. For Port, enter 389. 5. Click OK. 6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 106 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
132
8. Click OK. 9. From the menu, select View→Tree. 10. For BaseDN, type the DN of the object you want to view. 11. For Scope, select Base. 12. Click OK. Using a command-line interface To obtain a list of attributes for a particular object using DSQuery, use the following syntax: > dsquery * "<ObjectDN>" -scope base -attr * To query for an object using AdFind, use the following syntax: > adfind -b <Parent Container DN> -f cn=<Object CN> -tdcgt Using PowerShell To get the attributes of the built-in Administrator account, run the following PowerShell command: Get-ADUser -Identity Administrator -Properties * An example of the output is shown here: AccountExpirationDate : accountExpires : 0 AccountLockoutTime : AccountNotDelegated : False adminCount : 1 AllowReversiblePasswordEncryption : False BadLogonCount : 0 badPasswordTime : 129934507947486667 badPwdCount : 0 CannotChangePassword : False CanonicalName : adatum.com/Users/Administrator Certificates : {} City : CN : Administrator codePage : 0 Company : CompoundIdentitySupported : {} Country : countryCode : 0 Created : 9/27/2012 11:16:45 AM createTimeStamp : 9/27/2012 11:16:45 AM Deleted : Department : 4.2. Viewing the Attributes of an Object | 107
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
133
Description : Built-in account for administering the computer/domain DisplayName : DistinguishedName : cn=Administrator,cn=Users,dc=adatum,dc=com Division : DoesNotRequirePreAuth : False dSCorePropagationData : {9/27/2012 11:34:42 AM,↵ 9/27/2012 11:34:42 AM,↵ 9/27/2012 11:19:31 AM,↵ 1/1/1601 10:12:16 AM} EmailAddress : EmployeeID : EmployeeNumber : Enabled : True Fax : GivenName : HomeDirectory : HomedirRequired : False HomeDrive : HomePage : HomePhone : Initials : instanceType : 4 isCriticalSystemObject : True isDeleted : KerberosEncryptionType : {} LastBadPasswordAttempt : 9/29/2012 8:53:14 PM LastKnownParent : lastLogoff : 0 lastLogon : 129942736428265971 LastLogonDate : 10/9/2012 9:27:17 AM lastLogonTimestamp : 129942736379503412 LockedOut : False logonCount : 29 logonHours : {255, 255, 255, 255...} LogonWorkstations : Manager : MemberOf : {cn=Group Policy Creator Owners,cn=Users,dc=adatum,dc=com, cn=Domain Admins,cn=Users,dc=adatum,dc=com, cn=Enterprise Admins,cn=Users,dc=adatum,dc=com, cn=Schema Admins,cn=Users,dc=adatum,dc=com...} MNSLogonAccount : False MobilePhone : Modified : 10/9/2012 9:27:17 AM modifyTimeStamp : 10/9/2012 9:27:17 AM msDS-AuthenticatedAtDC : {cn=2012-DC05,OU=Domain Controllers,dc=adatum,dc=com} msDS-User-Account-Control-Computed : 0 Name : Administrator nTSecurityDescriptor : System.DirectoryServices.↵ ActiveDirectorySecurity 108 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
134
ObjectCategory : cn=Person,cn=Schema,cn=Configuration,↵ dc=adatum,dc=com ObjectClass : user ObjectGUID : c897bc0a-b5d4-4025-8c10-b696e45ce780 objectSid : S-1-5-21-3384837461-4027165227-↵ 453512602-500 Office : OfficePhone : Organization : OtherName : PasswordExpired : False PasswordLastSet : 9/27/2012 10:18:17 AM PasswordNeverExpires : False PasswordNotRequired : False POBox : PostalCode : PrimaryGroup : cn=Domain Users,cn=Users,dc=adatum,dc=com primaryGroupID : 513 PrincipalsAllowedToDelegateToAccount : {} ProfilePath : ProtectedFromAccidentalDeletion : False pwdLastSet : 129932398979898472 SamAccountName : Administrator sAMAccountType : 805306368 ScriptPath : sDRightsEffective : 15 ServicePrincipalNames : {} SID : S-1-5-21-3384837461-4027165227-↵ 453512602-500 SIDHistory : {} SmartcardLogonRequired : False State : StreetAddress : Surname : Title : TrustedForDelegation : False TrustedToAuthForDelegation : False UseDESKeyOnly : False userAccountControl : 512 userCertificate : {} UserPrincipalName : uSNChanged : 28008 uSNCreated : 8196 whenChanged : 10/9/2012 9:27:17 AM whenCreated : 9/27/2012 11:16:45 AM Discussion Objects in Active Directory are made up of a collection of attributes. Attributes can be single- or multivalued. Each attribute also has an associated syntax that is defined in the schema. See Recipe 10.6 for a complete list of syntaxes. 4.2. Viewing the Attributes of an Object | 109
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
135
Using a graphical user interface You can customize the list of attributes returned from a search with LDP by modifying the Attributes: field under Options→Search. To include all attributes, enter an asterisk (*). To modify the default subset of attributes that are returned, enter a semicolon-separated list of attributes. You can also use the numeric attribute ID instead of the attribute name, such as using 1.1 in place of distinguishedName. Using a command-line interface The -attr option for the dsquery command accepts a whitespace-separated list of at‐ tributes to display. Using an asterisk (*) will return all default attributes. When using AdFind, you have several shortcut switches to reduce the amount of typing you need to do. If you are searching for an object in the default container, you can use the –default switch rather than something like –b dc=contoso,dc=com. Likewise, if you are querying the Configuration NC, you can use the –config switch, -root for the root partition, or –schema for the Schema partition. If you want to query a subcontainer of one of these partitions, you can add the –rb switch, which stands for Relative Base. See Also Recipe 10.6; MSDN: IADsPropertyEntry; MSDN: IADsPropertyList; MSDN: ADSTY‐ PEENUM; MSDN: IADs::GetInfo; Active Directory, Fifth Edition, by Brian Desmond et al. (O’Reilly) 4.3. Counting Objects in Active Directory Problem You want to retrieve the number of directory objects that meet the result of an LDAP query. Solution Using a graphical user interface 1. Open LDP. 2. From the menu, select Connection→Connect. 3. For Server, enter the name or IP address of a domain controller or the domain that contains the object. 4. For Port, enter 389. 5. Click OK. 110 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
136
6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 8. Click OK. 9. From the menu, select Browse→Search. 10. Enter the base DN, scope, and LDAP filter of the objects that you’re looking for. 11. Click Options and remove the checkmark next to Display Results. This will display the number of objects returned by the query without displaying the details of the items that are returned. 12. Click OK and then click Run to perform the query. Using a command-line interface To retrieve a count of objects that match a particular query, use the following syntax: > adfind -b <Search Base> -s <Scope> -f <Search Filter> -c For example, retrieving the number of user objects in the adatum.com domain would use the following syntax: > adfind -default -f "(&(objectclass=user)(objectcategory=person))" -c > > AdFind V01.46.00cpp Joe Richards (joe@joeware.net) March 2012 > > Using server: dc1.adatum.com:389 > Directory: Windows Server 8 > Base DN: dc=adatum,dc=com > > 5 Objects returned Using PowerShell The following example will query Active Directory for a list of user objects in the current domain and return the count: Get-ADObject -Filter {(objectClass -eq "user") -and ↵ (objectCategory -eq "person")} | Measure-Object | FL Count To retrieve a count of all of the computer objects in the current domain, run the following command: Get-ADObject -Filter {(objectClass -eq "user") -and ↵ (objectCategory -eq "computer")} | Measure-Object | FL Count 4.3. Counting Objects in Active Directory | 111
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
137
Discussion Using PowerShell The PowerShell commands can target a specific container or OU, as follows: Get-ADObject -SearchBase "OU=Branches,dc=adatum,dc=com"↵ -Filter {(objectClass -eq "user") -and (objectCategory -eq "person")} |↵ Measure-Object | FL Count Be careful with the filtering. The class and category of the object must be combined to accurately target user objects. There are other ways to filter, too. In this recipe, we’ve used an efficient filtering method while keeping the commands easy to use. However, in large environments, using LDAP filtering can be a bit faster than standard filtering. The following command counts all of the user objects in the current domain and uses LDAP filtering: Get-ADObject -LDAPFilter "(&(objectCategory=Person)(objectClass=User)) " |↵ Measure-Object | FL Count 4.4. Using LDAP Controls Problem You want to use an LDAP control as part of an LDAP operation. Solution Using a graphical user interface 1. Open LDP. 2. From the menu, select Options→Controls. 3. Under Load Predefined, select the control you want to use. The control should automatically be added to the list of Active Controls. 4. Under Value, enter the value for the control. 5. Under Control Type, select whether the control is server-side or client-side. 6. Check the box beside Critical if the control is critical. 7. Click OK. 8. At this point, you will need to invoke the LDAP operation (such as Search) that will use the control. In the dialog box for any operation, click Options and ensure that the Extended option is checked before initiating the operation. 112 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
138
Using a command-line interface The AdFind and AdMod utilities will enable a number of LDAP controls, either by default or through the use of various command-line switches. For example, the –show del switch will invoke the Show Deleted Objects LDAP control, and -stats will invoke the Show Stats control. Using PowerShell PowerShell leverages LDAP controls in some built-in parameters of various cmdlets. For example, the Get-ADObject cmdlet has a parameter to include deleted objects in searches. The –IncludeDeletedObjects parameter uses LDAP controls as part of the command. Discussion LDAP controls were defined in the LDAPv3 specification as a way to extend LDAP and its operations without breaking the protocol. Many controls have been implemented, some of which are used when searching the directory (e.g., paged searching, Virtual List View [VLV], finding deleted objects, and attribute scoped query), and some are needed to do certain modifications to the directory (e.g., cross-domain object moves, tree delete, and permissive modify). Controls can be marked as critical, which means they must be processed with the request or an error is returned. If an unsupported control is not flagged as critical, the server can continue to process the request and ignore the control. The complete list of controls supported by Active Directory is included in Table 4-2. Table 4-2. LDAP controls supported by Active Directory Name OID Description Permit No-Opt Modify 1.2.840.113556.1.4.1413 Allows duplicate adds of the same value for an attribute or deletion of an attribute that has no values to succeed (normally, it would fail in that situation). Return Deleted Objects 1.2.840.113556.1.4.417 Used to inform the server to return any deleted objects that matched the search criteria. Cross Domain Move 1.2.840.113556.1.4.521 Used to move objects between domains. Set change notifications 1.2.840.113556.1.4.528 Used by clients to register for notification of when changes occur in the directory. Delayed Write 1.2.840.113556.1.4.619 Used to inform the server to return after directory modifications have been written to memory, but before they have been written to disk. This can speed up processing of a lot of modifications. Security Descriptor Flags 1.2.840.113556.1.4.801 Used to pass flags to the server to control certain security descriptor options. Subtree Delete 1.2.840.113556.1.4.805 Used to delete portions of the directory tree, including any child objects. Verify Name Existence 1.2.840.113556.1.4.1338 Used to target a specific GC server that is used to verify DN-valued attributes that are processed during addition or modification operations. 4.4. Using LDAP Controls | 113
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
139
Name OID Description No referrals generated 1.2.840.113556.1.4.1339 Informs the server not to generate any referrals in a search response. Domain or phantom scope 1.2.840.113556.1.4.1340 Used to pass flags to the server to control search options. Search Stats 1.2.840.113556.1.4.970 Used to return statistics about an LDAP query. See Recipe 15.8 for an example. Attribute Scoped Query 1.2.840.113556.1.4.1504 Used to force a query to be based on a specific DN-valued attribute. This control is new to Windows Server 2003. See Recipe 4.8 for an example. Extended DN 1.2.840.113556.1.4.529 Used to return an object’s GUID and SID (for security principals) as part of its distinguished name. Quota SID 1.2.840.113566.1.4.1852 Used to pass the SID of a security principal in order to query constructed attributes such as ms-DS-Quota-Effective and ms-DS-Quota￾Used. Paged Results 1.2.840.113556.1.4.319 Instructs the server to return search results in “pages.” DIRSYNC 1.2.840.113556.1.4.841 Used to find objects that have changed over a period of time. Server-side Sort Request 1.2.840.113556.1.4.473 Used to inform the server to sort the results of a search. Server-side Sort Response 1.2.840.113556.1.4.474 Returned by the server in response to a sort request. Show deactivated links 1.2.840.113556.1.4.2065 Used to request deactivated links to be included in a search. Show deleted objects 1.2.840.113556.1.4.417 Used to request deleted objects to be included in a search. VLV Request 2.16.840.1.113730.3.4.9 Used to request a virtual list view of results from a search. This control is new to Windows Server 2003. VLV Response 2.16.840.1.113730.3.4.10 Response from a server returning a virtual list view of results from a search. This control is new to Windows Server 2003. See Also Recipe 4.8; Recipe 15.8; RFC 2251 (Lightweight Directory Access Protocol [v3]) for a description of LDAP controls; 3.1.1.3.4.1 LDAP Extended Controls; MSDN: Using Controls 4.5. Using a Fast or Concurrent Bind Problem You want to perform an LDAP bind using a concurrent bind, also known as a fast bind. Concurrent binds are typically used in situations where you need to authenticate a lot of users, and either those users do not need to directly access the directory or the di‐ rectory access is done with another account. 114 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
140
Solution Using a graphical user interface 1. Open LDP. 2. From the menu, select Connection→Connect. 3. For Server, enter the name of a DC. 4. For Port, enter 389. 5. Click OK. 6. From the menu, select Options→Connection Options. 7. Under Option Name:, select LDAP_OPT_FAST_CONCURRENT_BIND. 8. Click the Set button. Then click the Close button. 9. From the menu, select Connection→Bind. 10. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 11. Click OK. Discussion Unlike simple binding, concurrent binding does not generate a security token or de‐ termine a user’s group memberships during the authentication process. It determines only whether the authenticating user has a valid enabled account and password, which makes it much faster than a typical bind. This is usually used programmatically for AD￾enabled applications to improve the speed of AD authentication; it’s not something that you’ll typically do on the fly. Concurrent binding is implemented as a session option that is set after you establish a connection to a domain controller, but before any bind attempts are made. After the option has been set, any bind attempt made with the con‐ nection will be a concurrent bind. There are a couple of caveats when using concurrent binds. First, you cannot enable signing or encryption, which means that all data for concurrent binds will be sent over the network in clear text. Second, because the user’s security token is not generated, access to the directory is done anonymously and access restrictions are based on the ANONYMOUS LOGON principal. It is worth mentioning that there is another type of fast bind that is completely different from the procedure just described. This fast bind is implemented within ADSI, and it simply means that when you fast-bind to an object, the objectClass attribute for the object is not retrieved; therefore, the object-specific IADs class interfaces are not 4.5. Using a Fast or Concurrent Bind | 115
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
141
available. For example, if you bind to a user object using an ADSI fast bind, then only the basic IADs interfaces will be available, not the IADsUser interfaces. This is the complete list of interfaces that are available for objects retrieved with fast binds: • IADs • IADsContainer • IDirectoryObject • IDirectorySearch • IADsPropertyList • IADsObjectOptions • ISupportErrorInfo • IADsDeleteOps You must use the IADsOpenDSObject::OpenDSObject interface to enable fast binds. If you call IADsContainer::GetObject on a child object of a parent you used a fast bind with, the same fast bind behavior applies. Unlike concurrent binds, ADSI fast binds do not impose any restrictions on the authenticating user. This means that the object￾specific IADs interfaces will not be available. Also, no check is done to verify the object exists when you call OpenDSObject. ADSI fast binds are useful when you need to make a lot of updates to objects that you know exist (perhaps from an ADO query that returned a list of DNs) and you do not need any IADs-specific interfaces. Instead of two trips over the network per object binding, there would be only one. See Also MSDN: Using Concurrent Binding; MSDN: ADS_AUTHENTICATION_ENUM 4.6. Connecting to an Object GUID Problem You want to bind to a container using its globally unique identifier (GUID). Solution Using a graphical user interface 1. Open LDP. 116 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
142
2. From the menu, select Connection→Connect. 3. For Server, enter the name of a domain controller (or leave it blank to do a serverless bind). 4. For Port, enter 389. 5. Click OK. 6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 8. Click OK. 9. From the menu, select Browse→Search. 10. For BaseDN, enter the GUID of the object that you’re searching for in the following format: <GUID=758A39F4A44A0C48A16016457C1AE9E9> 11. For Scope, select the appropriate scope. 12. For Filter, enter an LDAP filter. 13. Click Run. Discussion Each object in Active Directory has a GUID associated with it, stored in the object GUID attribute. The GUID is, for most purposes, a unique identifier that retains its value even if an object is updated, renamed, or moved. This makes the GUID the preferable means of binding to an object, rather than hardcoding a reference to an object name that might change or by using a potentially complex LDAP query. See Also “‘GUIDs’ or ‘Having unique in the name doesn’t make it so...’” for a more in-depth discussion of the objectGUID attribute; MSDN: IADs.GUID; MSDN: Using object‐ GUID to Bind to an Object; Recipe 4.7 4.7. Connecting to a Well-Known GUID Problem You want to connect to LDAP using one of the well-known GUIDs in Active Directory. 4.7. Connecting to a Well-Known GUID | 117
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
143
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 it blank to do a serverless bind). 4. For Port, enter 389. 5. Click OK. 6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 8. Click OK. 9. From the menu, select View→Tree. 10. For the DN, enter: <WKGUID=<WKGUID>,<DomainDN>> where <WKGUID> is the well-known GUID that you want to connect to, and <DomainDN> is the distinguished name of a domain. 11. Click OK. In the lefthand menu, you can now browse the container corresponding to the well-known GUID that you specified. Using a command-line interface To enumerate the well-known GUIDs in the Domain NC, use the following syntax: > adfind -default -s base wellknownObjects To display the WKGUIDs in the Configuration NC, replace –default with –config in the previous syntax. To connect to a well-known GUID in the Domain NC using AdFind, use the following syntax: > adfind -b "<WKGUID=<WKGUID>,<DomainDN>>" -s base -dn Because of additional security settings attached to the Deleted Objects container, if you specify the GUID you must also use the –showdel switch in AdFind. 118 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
144
Using PowerShell Get-ADObject (Get-ADRootDSE).DefaultNamingContext -Properties wellKnownObjects↵ | Select wellKnownObjects -ExpandProperty wellknownobjects Discussion The Domain NC in Active Directory contains a number of well-known GUIDs that correspond to containers that exist in every AD implementation. These GUIDs are stored as wellKnownObjects attributes within the <DomainDN> object, and they allow administrators and developers to consistently connect to critical containers even if they are moved or renamed. The <DomainDN> container possesses the following objects that correspond to well-known GUIDs: • cn=NTDS Quotas,<DomainDN> • cn=Microsoft,cn=Program Data,<DomainDN> • cn=Program Data,<DomainDN> • cn=ForeignSecurityPrincipals,<DomainDN> • cn=Deleted Objects,<DomainDN> • cn=Infrastructure,<DomainDN> • cn=LostAndFound,<DomainDN> • cn=System,<DomainDN> • OU=Domain Controllers,<DomainDN> • cn=Computers,<DomainDN> • cn=Users,<DomainDN> The Configuration NC adds these additional WKGUIDs: • cn=NTDS Quotas,cn=Configuration,<ForestRootDN> • cn=LostAndFoundConfig,cn=Configuration,<ForestRootDN> • cn=Deleted Objects,cn=Configuration,<ForestRootDN> See Also MSDN: Binding to Well-Known Objects Using WKGUID 4.7. Connecting to a Well-Known GUID | 119
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
145
4.8. Searching for Objects in a Domain Problem You want to find objects in a domain that match certain criteria. 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 it blank to do a serverless bind). 4. For Port, enter 389. 5. Click OK. 6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 8. Click OK. 9. From the menu, select Browse→Search. 10. For BaseDN, type the base distinguished name where the search will start. (You can leave this blank if you wish to connect to the Domain NC as the base DN.) 11. For Scope, select the appropriate scope. 12. For Filter, enter an LDAP filter. 13. Click Run. Using a command-line interface To run a query using the built-in DSQuery tool, use the following syntax: > dsquery * <BaseDN> -scope <Scope> -filter "<Filter>" -attr "<AttrList>" To retrieve the SAM account name for all user objects within the adatum.com domain, for example, use the following syntax: > dsquery * dc=adatum,dc=com -filter "(&(objectclass=user)(objectcategory=person))" -attr sAMAccountName To run a query using adfind, use the following syntax: > adfind -b <BaseDN> -s <Scope> -f <Filter> <Attributes> 120 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
146
Querying for SAM account names of user objects with adfind takes the following syntax: > adfind -b dc=adatum,dc=com -f↵ "(&(objectclass=user)(objectcategory=person))" sAMAccountName Both DSQuery and AdFind assume a default search scope of subtrees; you need only to specify the search scope if you want to use a different one. Using PowerShell The following example will search for user objects within the current Active Directory domain: Get-ADObject -Filter {(objectClass -eq "user") -and ↵ (objectCategory -eq "person")} Discussion Most tools that can be used to search Active Directory require a basic understanding of how to perform LDAP searches using a base DN, search scope, and search filter, as described in RFC 2251 and RFC 2254. The base DN is where the search begins in the directory tree. The search scope defines how far down in the tree to search from the base DN. The search filter is a prefix notation string that contains equality comparisons of attribute and value pairs. The scope can be base, onelevel (or one), or subtree (or sub). A base scope will match only the base DN, onelevel will match only objects that are contained directly under the base DN, and subtree will match everything from the base DN and any objects beneath it. There are no LDAP query scopes that will walk backward “up” the tree. The search filter syntax is a powerful way to represent simple and complex queries. For example, a filter that matches all of the user objects would be (&(objectclass=user) (objectcategory=Person)). For more information on filters, see RFC 2254. Using a graphical user interface To customize the list of attributes returned for each matching object, look at the GUI discussion in Recipe 4.2. 4.8. Searching for Objects in a Domain | 121
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
147
Using a command-line interface <AttrList> should be a space-separated list of attributes to return. To return all at‐ tributes that have been populated with a value, leave this field blank or use an asterisk (*). See Also Recipe 4.2 for viewing attributes of objects; Recipe 4.9 for setting advanced ADO op‐ tions; RFC 2251 (Lightweight Directory Access Protocol [v3]); RFC 2254 (Lightweight Directory Access Protocol [v3]); “LDAP Query Basics” 4.9. Searching the Global Catalog Problem You want to perform a forest-wide search using the global catalog. Solution Using a graphical user interface 1. Open LDP. 2. From the menu, select Connection→Connect. 3. For Server, enter the name of a global catalog server. 4. For Port, enter 3268. 5. Click OK. 6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 8. Click OK. 9. From the menu, select Browse→Search. 10. For BaseDN, type the base distinguished name of where to start the search. 11. For Scope, select the appropriate scope. 12. For Filter, enter an LDAP filter. 13. Click Run. 122 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
148
Using a command-line interface To query the global catalog using DSQuery, use the following syntax: > dsquery * <BaseDN> -gc -scope <Scope> -filter "<Filter>" -attr "<AttrList>" To run a query using AdFind, use the following syntax: > adfind -gc -b <BaseDN> -s <Scope> -f <Filter> <Attributes> Using PowerShell To query the global catalog server named dc1.adatum.com for all computer objects in the adatum.com domain, run the following command: Get-ADObject -Filter {ObjectClass -eq "computer"} -Server dc1.adatum.com:3268↵ -SearchBase "dc=adatum,dc=com" -Properties Name | FT Name Discussion The global catalog facilitates forest-wide searches. When you perform a normal LDAP search over port 389, you are searching against a particular partition within Active Directory, whether that is the Domain naming context, Configuration naming context, Schema naming context, or an application partition. If you have multiple domains in your forest, this type of search will not search against all domains; it will search only the domain that you specify. The global catalog, by contrast, contains a subset of the attributes for all objects in the forest (excluding objects in application partitions). Think of it as a subset of all the naming contexts combined. Every object in the directory will be contained in the global catalog (except for objects contained within application partitions), but only some of the attributes of those objects will be available. For that reason, if you perform a global catalog search and do not get values for attributes you were expecting to, make sure those attributes are included in the global catalog, also known as the partial attribute set (PAS). See Recipe 10.13 for more information on adding information to the PAS. As an alternative, you can query a DC within the domain containing the object to return a list of all attributes configured for that object. Note that the Active Directory Admin‐ istrative Center provides a method to easily change the scope of a search to be a global catalog search. Using a graphical user interface The only difference between this solution and the one in Recipe 4.8 is that the port has changed to 3268, which is the standard GC port. 4.9. Searching the Global Catalog | 123
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
149
Using a command-line interface The only difference between this solution and the one in Recipe 4.8, both for DSQuery and AdFind, is the addition of the -gc flag. See Also Recipe 4.8 for searching for objects; Recipe 10.13 4.10. Searching for a Large Number of Objects Problem Your search is returning exactly 1,000 objects, which is only a subset of the objects you expected, and you want it to return all matching objects. Solution You might notice, when using some tools, that searches with large numbers of matches stop displaying after 1,000. By default, domain controllers return a maximum of 1,000 entries from a search unless paging is enabled. This is done to prevent queries from consuming excessive resources on domain controllers by retrieving the results all at once instead of in pages or batches. The following examples are variations of Recipe 4.8, which will show how to enable paging and return all matching entries. 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 it blank to do a serverless bind). 4. For Port, enter 389. 5. Click OK. 6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 8. Click OK. 9. From the menu, select Browse→Search. 10. For BaseDN, type the base distinguished name of where the search will start. (You can leave this blank if you wish to connect to the domain NC as the base DN.) 11. For Scope, select the appropriate scope. 124 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
150
12. For Filter, enter an LDAP filter. 13. Click Options to customize the options for this query. 14. For “Time limit,” enter a value such as 120. 15. For “Size limit,” enter the number of objects to be returned with each page (e.g., 1,000). 16. Under Search Call Type, select Paged. 17. Click OK and then Run to perform the query. A page of results (i.e., 1,000 entries) will be displayed each time you click Run until all results have been returned. Using a command-line interface > dsquery * <BaseDN> -limit 0 -scope <Scope> -filter "<Filter>" ↵ -attr "<AttrList>" Using PowerShell The following PowerShell command will search the default Users container and return all objects while limiting results to 12,000 objects: Get-ADObject -SearchBase "cn=users,dc=adatum,dc=com" -Filter * -ResultSetSize ↵ 12000 Discussion Paged searching support is implemented via an LDAP control. LDAP controls were defined in RFC 2251 and the Paged control in RFC 2696. Controls are extensions to LDAP that were not built into the protocol, so not all directory vendors support the same ones. In Active Directory, you can change the default maximum page size of 1,000 by modifying the LDAP query policy. See Recipe 4.27 for more information. Active Directory will return a maximum of 256 KB of data even when paged searching is enabled. This value is defined in the LDAP query policy and can be modified like the maximum page size (see Recipe 4.27). Using a graphical user interface A word of caution when using LDP to display a large number of entries: by default, only 2,048 lines will be displayed in the right pane. To change that value, go to Op‐ tions→General and change the number of lines under Buffer Size to a larger number. 4.10. Searching for a Large Number of Objects | 125
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
151
Note that newer tools aren’t as limited as some of the tools that have been around a long time. For example, the Active Directory Administrative Center displays up to 20,000 objects in a search by default. Using a command-line interface The only difference between this solution and the one in Recipe 4.8 is the addition of the -limit 0 flag. With -limit set to 0, paging will be enabled according to the default LDAP query policy; matching objects will be returned within those parameters. If -limit is not specified, a maximum of 100 entries will be returned. AdFind enables paged searches by default; it will return any number of objects from a query without any modification. See Also Recipe 4.8 for searching for objects; Recipe 4.27 for viewing the default LDAP policy; RFC 2251 (Lightweight Directory Access Protocol [v3]); RFC 2696 (LDAP Control Extension for Simple Paged Results Manipulation) 4.11. Searching with an Attribute-Scoped Query Problem You want to perform a search using an individual value within a multivalued attribute as part of the search criteria. An attribute-scoped query can do this in a single query, instead of the previous method, which required multiple queries. Solution Using a graphical user interface 1. Follow the steps in Recipe 4.4 to enable an LDAP control. 2. Select the Attribute Scoped Query control. 3. For Value, enter the multivalued attribute name (e.g., member). 4. Click the “Check in” button. 5. Click OK. 6. From the menu, select Browse→Search. 7. For BaseDN, type the DN of the object that contains the multivalued attributes. 126 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
152
8. For Scope, select Base. 9. For Filter, enter an LDAP filter to match against the objects that are part of the multivalued DN attribute. 10. Click Run. Attribute-scoped queries can only be performed using a Base scope. Using a command-line interface AdFind allows attribute-scoped queries by using the -asq switch. For example: adfind -b cn=somegroup,cn=users,dc=domain,dc=group -asq member ↵ -f objectclass=user samaccountname Using PowerShell The PowerShell pipeline will allow you to perform an attribute-scoped query as follows: Get-ADGroup "Domain Admins" | Get-ADGroupMember |↵ Get-ADUser -Properties sAMAccountName | Select samaccountname Discussion When dealing with group objects, you may have encountered the problem where you wanted to search against the members of a group to find a subset or to retrieve certain attributes about each member. This normally involved performing a query to retrieve all of the members, and additional queries to retrieve whatever attributes you needed for each member. This was less than ideal, so an alternative was developed. With an attribute-scoped query, you can perform a single query against the group object and return whatever properties you need from the member’s object, or return only a subset of the members based on certain criteria. Let’s look at the LDAP search param‐ eters for an attribute-scoped query: Attribute-scoped query control value The value to set for this control should be the DN attribute that you want to iterate over (for example, member). Base DN This must be the DN of the object that contains the DN attribute (e.g., cn=Domain Admins,cn=users,dc=adatum,dc=com). Scope This must be set to Base to query only the group object itself. 4.11. Searching with an Attribute-Scoped Query | 127
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
153
Filter The filter will match against objects defined in the Control Value. For example, a filter of (objectClass=computer) would match computer objects only. You can also use any other attributes that are available with those objects. The following filter would match all computer objects that have a Description attribute equal to “Sales”: (&(objectclass=computer)(Description=Sales)) Attributes This should contain the list of attributes to return for the objects matched in the DN attribute. When performing an attribute-scoped query against a member attribute, it’s important to remember that primary group membership is handled as a special case; as such you may experience unpredictable results in this situation. See Also Recipe 4.4; MSDN: Performing an Attribute Scoped Query 4.12. Searching with a Bitwise Filter Problem You want to search against an attribute that contains a bit flag, which requires you to use a bitwise filter to perform the search. 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 it blank to do a serverless bind). 4. For Port, enter 389. 5. Click OK. 6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 8. Click OK. 128 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
154
9. From the menu, select Browse→Search. 10. For Base DN, type the base distinguished name of where the search will start. (You can leave this blank if you wish to connect to the domain NC as the base DN.) 11. For Scope, select the appropriate scope. 12. For the Filter, enter the bitwise expression, such as the following, which will find all universal groups: (&(objectCategory=group)(groupType:1.2.840.113556.1.4.804:=8)) 13. Click Run. Using a command-line interface The following query finds universal groups in the adatum.com domain by using a bit‐ wise AND filter: > dsquery * dc=adatum,dc=com -scope subtree -attr "name" -filter↵ "(&(objectclass=group)(objectCategory=group)↵ (groupType:1.2.840.113556.1.4.804:=8) )" The following query finds disabled user accounts in the adatum.com domain by using a bitwise AND filter: > dsquery * dc=adatum,dc=com -attr name -scope subtree -filter "(&(objectclass=user)(objectcategory=person)↵ (useraccountcontrol:1.2.840.113556.1.4. 803:=2))" You can also perform queries that use bitwise filters using AdFind. The following will find all disabled user accounts in the adatum.com domain: > adfind -default -bit -f useraccountcontrol:AND:=2 Similarly, the following will return all universal groups in the adatum.com domain using a bitwise filter: > adfind -default -bit -f groupType:AND:=8 Using PowerShell The following command will find all universal groups in the current domain: Get-ADObject -LDAPFilter {(groupType:1.2.840.113556.1.4.803:=8)} | Select Name Discussion Many attributes in Active Directory are composed of bit flags. A bit flag is often used to encode properties about an object into a single attribute. For example, the group Type attribute on group objects is a bit flag that is used to determine the group scope and type. 4.12. Searching with a Bitwise Filter | 129
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
155
The userAccountControl attribute on user and computer objects is used to describe a whole series of properties, including account status (i.e., enabled or disabled), account lockout, password not required, smartcard authentication required, and so on. The searchFlags and systemFlags attributes on attributeSchema objects define, among other things, whether an attribute is constructed, indexed, and included as part of Ambiguous Name Resolution (ANR). To search against these types of attributes, you need to use bitwise search filters. There are two types of bitwise search filters you can use, one that represents a logical OR and one that represents a logical AND. This is implemented within a search filter as a matching rule. A matching rule is simply a way to inform the LDAP server (in this case, a domain controller) to treat part of the filter differently. Here is an example of what a matching rule looks like: (userAccountControl:1.2.840.113556.1.4.803:=514) The format is (attributename:MatchingRuleOID:=value), though AdFind allows you to use an easier syntax for bitwise queries. As mentioned, there are two bitwise matching rules, which are defined by OIDs. The logical AND matching rule OID is 1.2.840.113556.1.4.803, and the logical OR matching rule OID is 1.2.840.113556.1.4.804. These OIDs instruct the server to perform special processing on the filter. A logical OR filter will return success if any bit specified by value is stored in attributename. Al‐ ternatively, the logical AND filter will return success if all bits specified by value match the value of attributename. Perhaps an example will help clarify this. To create a normal user account, you have to set userAccountControl to 514. The number 514 was calculated by adding the normal user account flag of 512 together with the disabled account flag of 2 (512 + 2 = 514). If you use the following logical OR matching rule against the 514 value, as shown here: (useraccountcontrol:1.2.840.113556.1.4.804:=514) then all normal user accounts (flag 512) OR disabled accounts (flag 2) would be re‐ turned. This would include enabled user accounts (from flag 512), disabled computer accounts (from flag 2), and disabled user accounts (from flag 2). In the case of userAc countControl, flag 2 can apply to both user and computer accounts, which is why both would be included in the returned entries. One of the benefits of bitwise matching rules is that they allow you to combine a bunch of comparisons into a single filter. In fact, it may help to think that the OR filter could also be written using two expressions: (|(useraccountcontrol:1.2.840.113556.1.4.804:=2) (useraccountcontrol:1.2.840.113556.1.4.804:=512)) 130 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
156
Just as before, this will match userAccountControl attributes that contain either the 2 or 512 flag; we’re performing two OR operations against the same value, first ORing the value against 2, then against 512. For the logical AND operator, similar principles apply. Instead of any of the bits in the flag being a possible match, all of the bits in the flag must match for it to return a success. If the userAccountControl example was changed to use logical AND, it would look like this: (useraccountcontrol:1.2.840.113556.1.4.803:=514) In this case, only normal user accounts that are also disabled would be returned. The same filter could be rewritten using the & operator instead of | as in the following: (&(useraccountcontrol:1.2.840.113556.1.4.803:=2) (useraccountcontrol:1.2.840.113556.1.4.803:=512)) An important subtlety to note is that when you are comparing only a single bit-flag value, the logical OR and logical AND matching rules would return the same result. So, if you wanted to find any normal user accounts, you could search on the single bit flag of 512 using either of the following: (useraccountcontrol:1.2.840.113556.1.4.803:=512) (useraccountcontrol:1.2.840.113556.1.4.804:=512) Using PowerShell Searching on a bitwise operator in PowerShell is done using the appropriate LDAP filter, as you can see. In other chapters we will look at individual AD cmdlets that mask the bitwise search into a more human-readable operation, such as the Enable-ADAccount and Disable-ADAccount cmdlets. See Also MSDN: Enumerating Groups by Scope or Type in a Domain; MSDN: Determining Which Properties Are Non-Replicated, Constructed, Global Catalog, and Indexed; MS KB 305144 (How to Use the UserAccountControl Flags to Manipulate User Account Properties) 4.13. Creating an Object Problem You want to create an object. 4.13. Creating an Object | 131
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
157
Solution In each solution in this recipe, an example of adding a user object is shown. Modify the examples as needed to include whatever class and attributes you need to create. 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. 3. In the left pane, browse to the container or OU you want to add the object to. Once you’ve found the parent container, right-click on it and select New→Object. 4. Under Select a Class, select user. 5. For the cn, enter jsmith and click Next. 6. For sAMAccountName, enter jsmith and click Next. 7. Click the More Attributes button to enter additional attributes. 8. Click Finish. Using a command-line interface Create an LDAP Data Interchange Format (LDIF) file called create_object.ldf with the following contents: dn: cn=jsmith,cn=users,dc=adatum,dc=com changetype: add objectClass: user samaccountname: jsmith Then run the following command: > ldifde -v -i -f create_object.ldf It is also worth noting that you can add a limited number of object types with the dsadd command. Run dsadd /? from a command line for more details. You can also create objects using AdMod; to create a new user object in the ada‐ tum.com domain use the following syntax: > admod -b "cn=Joe Smith,cn=users,dc=adatum,dc=com"↵ objectclass::user samaccountname::jsmith -add 132 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
158
Using PowerShell To create a new computer object named Kiosk2 in the Workstations OU, use the fol‐ lowing PowerShell command: New-ADObject -Path "OU=Workstations,dc=adatum,dc=com" -Type Computer ↵ -Name "Kiosk2" -Description "Computer in lobby" -OtherAttributes ↵ @{SamAccountName="Kiosk2"} Discussion To create an object in Active Directory, you have to specify the objectClass, RDN value, and any other mandatory attributes that are not automatically set by Active Directory. Some of the automatically generated attributes include objectGUID, instanceType, and objectCategory. In the jsmith example, the object class was user, the RDN value was jsmith, and the only other attribute set was sAMAccountName. Admittedly, this user object is unusable in its current state because it will be disabled by default and no password was set, but it should give you an idea of how to create an object. In the case of a user object, you’ll need to configure a password that meets any existing password complexity requirements before enabling the user. Using a graphical user interface Other tools, such as Active Directory Administrative Center and AD Users and Com‐ puters, could be used to do the same thing, but ADSI Edit is useful as a generic object editor. One attribute that you will not be able to set via ADSI Edit is the password (unico dePwd attribute). It is stored in binary form and needs to be edited using a secure con‐ nection. If you want to set the password for a user through a GUI, you can do it with the AD Users and Computers snap-in or Active Directory Administrative Center. Using a command-line interface For more on ldifde, see Recipe 4.28. With DSAdd, you can set numerous attributes when creating an object. The downside is that you can create only the following object types: computer, contact, group, OU, quota, and user. See Also Recipe 4.28; Recipe 4.29 for importing objects with LDIF; MSDN: IADsContainer:: GetObject; MSDN: IADsContainer::Create; MSDN: IADs::Put; MSDN: IADs::SetInfo 4.13. Creating an Object | 133
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
159
4.14. Modifying an Object Problem You want to modify one or more attributes of an object. Solution The following example sets the employeeID attribute for a user object. Using a graphical user interface 1. Open Active Directory Administrative Center. 2. In the bottom-right pane, enter the search criteria of the desired object and then click the Search button. 3. In the search results, locate the object, right-click it, and then click Properties. 4. Scroll down to the Extensions section and then click the Attribute Editor tab. 5. Scroll down and click the employeeID attribute and then click the Edit button. 6. Enter the value and then click OK. 7. Click OK again to close the object properties. Using a command-line interface Create an LDIF file called modify_object.ldf with the following contents: dn: cn=jsmith,cn=users,dc=adatum,dc=com changetype: modify replace: employeeID employeeID: 17320 - 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 <ObjectDN> <attribute>:<operation>:<value> For example, you can add a description to a user object using the following syntax: > admod -b cn="Joe Smith,cn=Users,dc=adatum,dc=com" description::Consultant You can modify a limited number of object types with the dsmod command. Run dsmod /? from a command line for more details. 134 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
160
Using PowerShell To modify an object using PowerShell, use the following syntax: Set-ADObject -Identity <"ObjectDN"> -Replace @{<property>=<"value">} Discussion Using a command-line interface For more on ldifde, see Recipe 4.28. As of the publication of this book, the only types of objects you can modify with DSMod are the following: computer, contact, group, OU, server, quota, partition, and user. As you saw in this recipe, the basic format of the AdMod command when used to modify an attribute is as follows: > admod -b <ObjectDN> <attribute>:<operation>:<value> The value used for <operation> can be any one of the following: <blank> Updates the attribute with the new value. (In practical terms, this leads to a syntax of <attribute>::<value>, with nothing included between the two colons.) + Adds a value to an attribute. − Clears an attribute. ++ Adds multiple values to an attribute. −− Removes multiple values from an attribute. To change a user’s password via AdMod, encrypt the connection by using the -kerbenc switch and then modify the unicodepwd attribute. See Also MSDN: IADs::Put; MSDN: IADs::PutEx; MSDN: IADs::SetInfo; MSDN: ADS_ PROP‐ ERTY_OPERATION_ENUM 4.14. Modifying an Object | 135
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
161
4.15. Modifying a Bit-Flag Attribute Problem You want to safely modify an attribute that contains a bit flag, without blindly over‐ writing its existing contents. The solutions in this recipe modify a new attribute named adatum-UserProperties that was previously added to the schema. Solution Using VBScript ' This code safely modifies a bit flag attribute ' ------ SCRIPT CONFIGURATION ------ strObject = "<ObjectDN>" ' e.g. cn=jsmith,cn=users,dc=adatum,dc=com strAttr = "<AttrName>" ' e.g. adatum-UserProperties boolEnableBit = <TRUEorFALSE> ' e.g. FALSE intBit = <BitValue> ' e.g. 16 ' ------ END CONFIGURATION -------- set objObject = GetObject("LDAP://" & strObject) intBitsOrig = objObject.Get(strAttr) intBitsCalc = CalcBit(intBitsOrig, intBit, boolEnableBit) if intBitsOrig <> intBitsCalc then objObject.Put strAttr, intBitsCalc objObject.SetInfo WScript.Echo "Changed " & strAttr & " from " & intBitsOrig &↵ " to " & intBitsCalc else WScript.Echo "Did not need to change " & strAttr & " (" & intBitsOrig & ")" end if Function CalcBit(intValue, intBit, boolEnable) CalcBit = intValue if boolEnable = TRUE then CalcBit = intValue Or intBit else if intValue And intBit then CalcBit = intValue Xor intBit end if end if End Function 136 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
162
Using PowerShell To set the adatum-UserProperties value using PowerShell, use the following example script: $user = Get-ADObject "cn=jsmith,cn=users,dc=adatum,dc=com"↵ -Properties adatum-UserProperties if (!$user.adatum-UserProperties) { Set-ADObject $user -Add @{"adatum-UserProperties"=4} Write-Host "Changed from NULL to 4." } else { $value = $user.adatum-UserProperties + 4 Set-ADObject $user -Replace @{"adatum-UserProperties"=$value} Write-Host "Changed value from" $user.adatum-UserProperties "to" $value"." } Discussion Recipe 4.12 described how to search against attributes that contain a bit flag, which is used to encode various settings about an object in a single attribute. As a quick recap, you need to use a logical OR operation to match any bits being searched against, and a logical AND to match a specific set of bits. If you want to set an attribute that is a bit flag, you need to take special precautions to ensure that you don’t overwrite an existing bit. Let’s consider an example. Adatum wants to secretly store some politically incorrect information about their users, such as whether the user is really old or has big feet. They don’t want to create attributes such as adatum-UserHasBigFeet, so they decide to encode the properties in a single bit-flag attribute. They decide to call the attribute adatum￾UserProperties with the possible bit values shown in Table 4-3. Table 4-3. Sample bit-flag attribute values Value Description 1 User is overweight. 2 User is very tall. 4 User has big feet. 8 User is very old. After they extend the schema to include the new attribute, Adatum needs to initially populate the attribute for all their users. To do so they can simply logically OR the values together that apply to each user. So, if settings 4 and 8 apply to the jsmith user, his adatum-UserProperties would be set to 12 (4 OR 8). No big deal so far. The issue comes in when they need to modify the attribute in the future. 4.15. Modifying a Bit-Flag Attribute | 137
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
163
You will, however, find that searching for information based on a bit￾flag attribute is not terribly efficient. This is because bit flags cannot be indexed; you need to calculate the value for every object populated with the bit-flag attribute in question. They later find out that jsmith was a former basketball player and is 6′8″. They need to set the 2 bit (for being tall) in his adatum-UserProperties attribute. To set the 2 bit they need to first determine whether it has already been set. If it has already been set, then there is nothing to do. If the 2 bit hasn’t been set, they need to logical OR 2 with the existing value of jsmith’s adatum-UserProperties attribute. If they simply set the at‐ tribute to 2, it would overwrite the 4 and 8 bits that had been set previously. In the VBScript solution, they could use the CalcBit function to determine the new value: intBitsCalc = CalcBit(intBitsOrig, 2, TRUE) The result would be 14 (12 OR 2). The same logic applies if they want to remove a bit, except the XOR logical operator is used. Active Directory contains numerous bit-flag attributes, most notably options (which is used on several different object classes) and userAc countControl (which is used on user objects). We do not recommend blindly setting those attributes unless you know what you are doing. It is preferable to use a script from this recipe so that it calculates the new value based on the existing value. You should note that it’s certainly possible to modify bitwise attributes using a GUI tool like ADSI Edit or a command-line tool like DSMod. However, it will require a certain amount of manual effort, as you’ll first need to make note of the existing attribute value and then calculate the new value using a calculator or some other method. The VBScript solution presented here simply automates that process by performing the lookup and calculations for you. Using PowerShell The PowerShell solution looks at the existing adatum-UserProperties attribute value. If it is null, it changes it to 4. If it contains a value already, it takes that value and adds 4 to it, and then replaces the existing value with the new value. See Also Recipe 4.12 for searching with a bitwise filter 138 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
164
4.16. Dynamically Linking an Auxiliary Class Problem You want to dynamically link an auxiliary class to an existing object instance. Solution In each solution in this recipe, an example of adding the custom adatum-SalesUser auxiliary class to the jsmith user object will be described. 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 the Advanced button if you need to enter alternate cre‐ dentials. 3. In the left pane, browse to the container or OU that contains the object you want to modify. Once you’ve found the object, right-click on it and select Properties. 4. Edit the values for the objectClass attribute. 5. For “Value to add,” enter adatum-SalesUser. 6. Click Add. 7. Click OK twice. Using a command-line interface Create an LDIF file called dynamically_link_class.ldf with the following contents: dn: cn=jsmith,cn=users,dc=adatum,dc=com changetype: modify add: objectClass objectClass: adatum-SalesUser - Then run the following command: > ldifde -v -i -f dynamically_link_class.ldf Alternatively, you can use AdMod as follows: > admod -b <ObjectDN> objectClass:+:<Dynamic Object Class> 4.16. Dynamically Linking an Auxiliary Class | 139
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
165
Using PowerShell Set-ADObject -Identity "cn=jsmith,cn=users,dc=adatum,dc=com"↵ -Add @{ObjectClass="adatum-SalesUser"} Discussion Dynamically linking an auxiliary class to an object is an easy way to use new attributes without modifying the existing object class definition in the schema. A situation in which it makes more sense to dynamically link auxiliary classes than to link them statically is when several organizations or divisions within a company main‐ tain their own user objects and want to add new attributes to the user class. It is also worth mentioning that extensive use of dynamically linked auxiliary classes can lead to problems. If several groups are using different auxiliary classes, it might become hard to determine what attributes you can expect on your user objects. Essen‐ tially, you could end up with many variations of a user class that each group has imple‐ mented through the use of dynamic auxiliary classes. For this reason, use of dynamic auxiliary classes should be closely monitored. In addition, some tools that access Active Directory may not work properly with auxiliary classes. See Also Recipe 4.14 for modifying an object 4.17. Creating a Dynamic Object Problem You want to create an object that is automatically expired and removed from the direc‐ tory after a period of time. Solution Using a graphical user interface 1. Open LDP. 2. Click Connection→Connect and click OK. 3. Click Connection→Bind. 4. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 5. Click View→Tree. Enter the DN of the parent container of the object you want to create and then click OK. 140 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
166
6. Click Browse→Add Child. The Add window will appear. 7. In the DN text box, enter the DN of the new object. 8. In the Attribute text box, enter objectClass. In the Values text box, enter the object class of the object you are creating, such as 'user'. Click Enter. In the Values text box, type dynamicObject and click Enter. 9. In the Attribute text box, type entryTTL. In the Values text box, enter the time to live (TTL) of the object you are creating, such as '3600'. Click Enter. 10. Enter any other attributes and values that you wish to populate in the Attribute and Values text boxes. 11. Click Run. Using a command-line interface Create an LDIF file called create_dynamic_object.ldf with the following contents: dn: cn=jsmith,cn=users,dc=adatum,dc=com changetype: add objectClass: user objectClass: dynamicObject entryTTL: 1800 sAMAccountName: jsmith Then run the following command: > ldifde -v -i -f create_dynamic_object.ldf Discussion The ability to create dynamic objects allows you to create objects that have a limited lifespan before they are automatically removed from the directory. To create a dynamic object, you simply need to specify the objectClass to have a value of dynamicObject in addition to its structural objectClass (e.g., user) value when instantiating the object. The entryTTL attribute can also be set to the number of seconds before the object is automatically deleted. If entryTTL is not set, the object will use the dynamicObjectDe faultTTL attribute specified in the domain. The entryTTL cannot be lower than the dynamicObjectMinTTL for the domain. See Recipe 4.19 for more information on how to view and modify these default values. Dynamic objects have a few special properties worth noting: • A static object cannot be turned into a dynamic object. The object must be marked as dynamic when it is created. • Dynamic objects cannot be created in the Configuration NC and Schema NC. • Dynamic objects do not leave behind tombstone objects. 4.17. Creating a Dynamic Object | 141
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
167
• Dynamic objects that are containers cannot have static child objects. • A dynamic container will not expire prior to any child objects contained within it. If the dynamic container has a lower TTL value than any of the children, once the container’s TTL expires it will be reset to the highest TTL value of the children plus one second. See Also Recipe 4.18 for refreshing a dynamic object; Recipe 4.19 for modifying the default dy‐ namic object properties 4.18. Refreshing a Dynamic Object Problem You want to refresh a dynamic object to keep it from expiring and getting deleted from Active Directory. Solution In each solution in this recipe, an example of adding a user object is used. Modify the examples as needed to refresh whatever object is needed. 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 it blank to do a serverless bind). 4. For Port, enter 389. 5. Click OK. 6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 8. Click OK. 9. Select Browse→Modify. 10. For DN, enter the DN of the dynamic object you want to refresh. 11. For Attribute, enter entryTTL. 142 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
168
12. For Values, enter the new time to live (TTL) for the object in seconds. 13. Under Operation, select Replace. 14. Click Enter. 15. Click Run. Using a command-line interface Create an LDIF file called refresh_dynamic_object.ldf with the following contents: dn: cn=jsmith,cn=users,dc=adatum,dc=com changetype: modify replace: entryTTL entryTTL: 1800 - Then run the following command: > ldifde -v -i -f refresh_dynamic_object.ldf You can also use AdMod with the following syntax: > admod -b <ObjectDN> entryTTL::<TTL in Seconds> Using PowerShell To refresh a dynamic object using PowerShell, use the following command: Set-ADObject -Identity "cn=jsmith,cn=users,dc=adatum,dc=com"↵ -Replace @{entryTTL="1800"} Discussion Dynamic objects expire after their TTL becomes 0. You can determine when a dynamic object will expire by looking at the current value of an object’s entryTTL attribute or by querying msDS-Entry-Time-To-Die, which contains the seconds remaining until expi‐ ration. If you’ve created a dynamic object and need to refresh it so that it will not get deleted, you must reset the entryTTL attribute to a new value. There is no limit to the number of times you can refresh a dynamic object. As long as the entryTTL value does not reach 0, the object will remain in Active Directory. See Also Recipe 4.14 for modifying an object; “Dynamic Objects (Windows)”; Recipe 4.17 for creating a dynamic object 4.18. Refreshing a Dynamic Object | 143
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
169
4.19. Modifying the Default TTL Settings for Dynamic Objects Problem You want to modify the minimum and default TTLs for dynamic objects. Solution In each solution in this recipe, we’ll show how to set the DynamicObjectDefaultTTL setting to 172800. Modifying the DynamicObjectMinTTL can be done in the same manner. Using a graphical user interface 1. Open ADSI Edit. 2. If an entry for the Configuration naming context 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 for your forest. Click on the Ad‐ vanced button if you need to enter alternate credentials. 3. In the left pane, browse to the following path under the Configuration naming context: Services→Windows NT→Directory Service. 4. Right-click cn=Directory Service and select Properties. 5. Edit the msDS-Other-Settings attribute. 6. Click on DynamicObjectDefaultTTL= <xxxxx> and click Remove. 7. The attribute/value pair should have been populated in the “Value to add” field. 8. Edit the number part of the value to be 172800. 9. Click Add. 10. Click OK twice. Using a command-line interface The following ntdsutil command connects to <DomainControllerName>, displays the current values for the dynamic object TTL settings, sets the DynamicObjectDe faultTTL to 172800, commits the change, and displays the results: > ntdsutil "config settings" connections "connect to server↵ <DomainControllerName>" q "show values" "set DynamicObjectDefaultTTL to 172800"↵ 144 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
170
"commit changes" "show values" q q Discussion Two configuration settings apply to dynamic objects: dynamicObjectDefaultTTL Defines the default TTL that is set for a dynamic object at creation time unless another one is set via entryTTL dynamicObjectMinTTL Defines the smallest TTL that can be configured for a dynamic object Unfortunately, these two settings are not stored as discrete attributes. Instead, they are stored as attribute value assertions (AVAs) in the msDS-Other-Settings attribute on the cn=DirectoryServices,cn=WindowsNT,cn=Configuration,<ForestRootDN> ob‐ ject. AVAs are used occasionally in Active Directory on multivalued attributes, in which the values take the form of Setting1=Value1,Setting2=Value2, and so on. For this reason, you cannot simply manipulate AVA attributes as you would another attribute. You have to be sure to add or replace values with the same format, as they existed previously. Using a command-line interface You can use ntdsutil in interactive mode or in single-command mode. In this solution, we’ve included all the necessary commands on a single line. You can, of course, step through each command by simply running ntdsutil in interactive mode and entering each command one by one. See Also Recipe 4.14 for modifying an object; MSDN: Regular Expression (RegExp) Object 4.20. Moving an Object to a Different OU or Container Problem You want to move an object to a different container or OU. Solution Using a graphical user interface 1. Open Active Directory Administrative Center. 4.20. Moving an Object to a Different OU or Container | 145
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
171
2. In the bottom-right pane, enter the search criteria of the desired object and then click the Search button. 3. In the search results, locate the object, right-click it, and then click Move. 4. In the dialog box, browse to the destination container, click to highlight it, and then click OK to move the object. You can also move objects by using the ADSI Edit tool or the Active Directory Users and Computers snap-in. Using a command-line interface To move an object to a new parent container within the same domain, you can use either dsmove or admod, as follows: > dsmove "<ObjectDN>" -newparent "<NewParentDN>" or: > admod -b <ObjectDN> -move "<NewParentDN>" Using PowerShell To move an Active Directory object using PowerShell, use the following syntax: Move-ADObject -Identity <ObjectDN> -TargetPath <NewParentDN> Discussion Using a command-line interface The DSMove utility can work against any type of object (it has no limitations, as with DSAdd and DSMod). The first parameter is the DN of the object to be moved. The second parameter is the new parent container of the object. The -s parameter can ad‐ ditionally be used to designate a specific server to work against. Regardless of the method you use to move objects, you need to ensure that the user who is performing the move has the appropriate permis‐ sions to create objects in the destination container and delete objects from the source container. 146 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
172
See Also Recipe 4.23; MSDN: IADsContainer::MoveHere 4.21. Moving an Object to a Different Domain Problem You want to move an object to a different domain. Solution Using a graphical user interface (ADMT 3.2) To migrate a computer object between domains in the same forest, use the following steps: 1. Open the ADMT MMC snap-in. 2. Right-click on the Active Directory Migration Tool folder and select the Computer Migration Wizard. 3. On the Welcome screen, click Next. 4. On the Domain Selection page, enter the DNS or NetBIOS name of the source and target domains. Click Next. 5. On the Computer Selection Option screen, select the option to select computer from the domain and then click Next. 6. On the Computer Selection screen, click Add and use the object picker to select a computer object. Click OK to return to the Computer Selection screen and then click Next. 7. On the Organizational Unit Selection screen, enter the destination OU in the new domain and then click Next. 8. On the Translate Objects screen, specify which objects should have new ACLs ap‐ plied in the new domain. Select any, none, or all of the following, and then click Next to continue: • Files and folders • Local groups • Printers • Registry • Shares • User profiles 4.21. Moving an Object to a Different Domain | 147
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
173
• User rights 9. On the Computer Options screen, click Next to maintain the default reboot time of 5 minutes. 10. On the Object Property Exclusion screen, select any object properties that you do not want to migrate and then click Next. 11. On the Conflict Management screen, click Next to accept the default that will not migrate the computer if there is a conflict. 12. On the Completing the Computer Migration Wizard screen, review the migration settings and then click Finish to complete the move. Using PowerShell In the following example, the cn=jsmith object in the amer.adatum.com domain will be moved to the emea.adatum.com domain: Move-ADObject -Identity "cn=jsmith,cn=users,dc=amer,dc=adatum,dc=com"↵ -TargetPath "ou=MigratedUsers,dc=emea,dc=adatum,dc=com"↵ -TargetServer dc-emea1.emea.adatum.com -Credential "emea\Administrator" Discussion At the time of this writing, the current version of ADMT, version 3.2, is not supported on Windows Server 2012. Therefore, you should install it on a previous version of Windows Server as part of the migration to Windows Server 2012. You can move objects between domains assuming you follow a few guidelines: • The user performing the move operation must have permission to modify objects in the parent container of both domains. • You need to explicitly specify the target DC (serverless binds usually do not work). This is necessary because the “Cross Domain Move” LDAP control is being used behind the scenes. For more information on controls, see Recipe 4.4. • The move operation must be performed against the RID master for both domains. • When you move a user object to a different domain, its objectSID is replaced with a new SID (based on the new domain), and the old SID is optionally added to the sIDHistory attribute. See Also Recipe 4.4 for more information on LDAP controls; MS KB 238394 (How to Use the MoveTree Utility to Move Objects Between Domains in a Single Forest); MSDN: IADs‐ 148 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
174
Container::MoveHere; “ADMT Guide: Migrating and Restructuring Active Directory Domains” 4.22. Referencing an External Domain Problem You need to create a reference to an external Active Directory domain. 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. 3. Right-click on the top-level node and open a connection to the Configuration NC. 4. Right-click on the Partitions container and select New→Object. Click Next. 5. Right-click on crossRef and click Next. 6. For the cn attribute, enter the FQDN of the external domain—othercorp.com, for example. Click Next. 7. For the nCName attribute, enter the DN of the external domain, such as dc=other corp,dc=com. Click Next. 8. For the dnsRoot attribute, enter the DNS name of a server that can respond to LDAP queries about the domain in question, such as dc1.othercorp.com. 9. Click Next and then Finish to create the crossRef object. Using a command-line interface Create an LDIF file called create_crossref.ldf with the following contents: dn: cn=othercorp.com,cn=partitions,cn=configuration,dc=adatum,dc=com changetype: add objectClass: crossRef cn: othercorp.com nCName: dc=othercorp,dc=com dnsRoot: dc1.othercorp.com 4.22. Referencing an External Domain | 149
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
175
Then run the following command: > ldifde -v -i -f create_crossref.ldf You can also create a crossRef using AdMod as follows: > admod -config -rb cn=othercorp.com,cn=partitions↵ objectClass::crossRef cn::othercorp.com nCName::dc=othercorp,dc=com↵ dnsRoot::dc1.othercorp.com -add Discussion Similar to the way in which DNS servers use iterative queries to resolve hostnames that can be resolved only by remote servers, LDAP uses referrals to resolve queries for objects contained in naming contexts that are not hosted by the local DC. When a DC receives any query, it will search the Partitions container for a crossRef object containing the DN that’s being used as the Base DN of the query. If the DC locates a crossRef that matches the search base of the query, and that crossRef indicates a naming context that’s hosted by the domain controller itself, then the DC will perform the search locally. If the crossRef refers to an NC that’s hosted on a remote server, the DC generates a referral to the server that is pointed to by the crossRef object. If the DC can’t locate a relevant crossRef object, it will use DNS to attempt to generate an additional location to refer the client to. In most cases, Active Directory will generate LDAP referrals automatically. However, you should manually create a crossRef object to generate LDAP referrals for an external domain, such as referrals to othercorp.com that are generated by the adatum.com domain. See Also MS KB 241737 (How to Create a Cross-Reference to an External Domain in Active Directory); MS KB 817872 (How to Create crossRef Objects for a DNS Namespace Subordinate of an Existing Active Directory Forest); MSDN: Referrals [Active Direc‐ tory]; MSDN: When Referrals Are Generated [Active Directory] 4.23. Renaming an Object Problem You want to rename an object and keep it in its current container or OU. 150 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
176
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 that contains the object you want to rename. Click on the Advanced button if you need to enter alternate credentials. 3. In the left pane, browse to the container or OU that contains the object you want to modify. Once you’ve found the object, right-click on it and select Rename. 4. Enter the new name and click OK. You can also rename a leaf object by using LDP as follows: 1. Open LDP. 2. From the menu, select Connection→Connect. 3. For Server, enter the name of a domain controller (or leave it blank for a serverless bind). 4. For Port, enter 389. 5. Click OK. 6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 8. Click OK. 9. Click Browse→Modify RDN. For Old DN, enter the Distinguished Name of the object that you want to rename. For New DN, enter the object’s new name. 10. Click Run to rename the object. Using a command-line interface To rename an object using the built-in DSMove utility, use the following syntax: > dsmove "<ObjectDN>" -newname "<NewName>" To use AdMod, use the following: > admod -b "<ObjectDN>" -rename "<NewName>" 4.23. Renaming an Object | 151
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
177
Using PowerShell To rename an object using PowerShell, use the following syntax: Rename-ADObject -Identity "<Object DN>" -NewName "<New Value of 'name' attribute>" Discussion Before you rename an object, you should ensure that no applications reference it by name. You can make objects rename-safe by requiring all applications that must store a reference to an object to use the GUID of the object, rather than the name. The GUID (stored in the objectGUID attribute) is effectively unique and does not change when an object is renamed. Keep in mind that you may wish to perform other cleanup tasks when renaming an object. In the case of a user who is changing her name, you may wish to update her Display Name and sn attributes to match the new CN. Using a graphical user interface If the parent container of the object you want to rename has a lot of objects in it, you may want to add a new connection entry for the DN of the object you want to rename. This may save you time searching through the list of objects in the container. You can do this by right-clicking ADSI Edit and selecting “Connect to” under Connection Point; select Distinguished Name and enter the DN of the object you want to rename. You can also rename most objects within the Active Directory Users and Computers MMC snap-in (dsa.msc) by navigating to the object in question, right-clicking on it, and selecting Rename. Using a command-line interface The two parameters that are needed to rename an object are the original DN of the object and the new RDN (-newname). The -s option can also be used to specify a server name to work against. See Also MSDN: IADsContainer::MoveHere 152 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
178
4.24. Deleting an Object Problem You want to delete an individual 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 that contains the object you want to delete. Click on the Advanced button if you need to enter alternate credentials. 3. In the left pane, browse to the object you want to delete. 4. Right-click on the object and select Delete. 5. Click Yes to confirm. You can also delete an object using LDP, as follows: 1. Open LDP. 2. From the menu, select Connection→Connect. 3. For Server, enter the name of a domain controller (or leave it blank for a serverless bind). 4. For Port, enter 389. 5. Click OK. 6. From the menu, select Connection→Bind. 7. Accept the default and bind as the currently logged on user or select the option to bind with credentials and then enter the credentials. 8. Click OK. 9. Click Browse→Delete. For DN, enter the Distinguished Name of the object that you want to delete. 10. Click Run to delete the object. 4.24. Deleting an Object | 153
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
179
Using a command-line interface You can delete an object using the built-in dsrm utility, as well as AdMod. For dsrm, use the following syntax: > dsrm "<ObjectDN>" For AdMod, enter the following: > admod -b "<ObjectDN>" -del Using PowerShell To delete the “Branches” OU in the adatum.com domain, use the following command: Remove-ADObject -Identity "ou=Branches,dc=adatum,dc=com" -Confirm:$false Discussion This recipe covers deleting individual objects. If you want to delete a container or OU and all the objects in it, take a look at Recipe 4.25. Using a graphical user interface If the parent container of the object you want to delete has a lot of objects in it, you may want to add a new connection entry for the DN of the object you want to delete. This can save you time searching through the list of objects in the container and could help avoid accidental deletions. You can do this by right-clicking ADSI Edit and selecting “Connect to.” Under Connection Point, select Distinguished Name and enter the DN of the object you want to delete. You can also delete most objects within the Active Directory Users and Computers MMC snap-in (dsa.msc) by navigating to the object in question, right-clicking on it, and selecting Delete. Using a command-line interface The dsrm utility can be used to delete any type of object (there are no limitations based on object type, as with dsadd and dsmod). The only required parameter is the DN of the object to delete. You can also specify -noprompt to keep it from asking for confirmation before deleting. The -s parameter can be used as well to designate a specific server to target. AdMod will not prompt you in this manner. See Also Recipe 4.25 for deleting a container; MS KB 258310 (Viewing Deleted Objects in Active Directory); MSDN: IADsContainer::Delete; MSDN: IADsDeleteOps:: DeleteObject 154 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
180
4.25. Deleting a Container That Has Child Objects Problem You want to delete a container or organizational unit and all child objects contained within. Solution Using a graphical user interface Open ADSI Edit and follow the same steps as in Recipe 4.24. The only difference is that you’ll be prompted to confirm twice instead of once before the deletion occurs. Using a command-line interface You can delete a container and its child objects using the built-in dsrm utility, as well as AdMod. For dsrm, use the following syntax: > dsrm "<ObjectDN>" -subtree For AdMod, enter the following: > admod -b "<ObjectDN>" -del -treedelete Using PowerShell The “Branches” OU in the adatum.com domain contains child objects. To delete it without a confirmation prompt, use the following command: Remove-ADObject -Identity "ou=Branches,dc=adatum,dc=com" -Recursive↵ -Confirm:$false Discussion As you can see from the solutions, there is not much difference between deleting a leaf node and deleting a container that has child objects. However, there is a distinction in what is happening in the background. Deleting an object that has no children can be done with a simple LDAP delete operation. On the other hand, to delete a container and its children, the tree-delete LDAP control has to be used. If you were to do the deletion from an LDAP-based tool like LDP (the Active Directory Administration Tool), you would first need to enable the Subtree De‐ lete control, which has an OID of 1.2.840.113556.1.4.805. LDP provides another option to do a Recursive Delete from the client side. That will essentially iterate through all the objects in the container, deleting them one by one. The Subtree Delete is much more efficient, especially when dealing with large containers. 4.25. Deleting a Container That Has Child Objects | 155
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
181
As with the other operations we’ve discussed in this chapter (create, rename, move, and so on), the user performing the delete operation needs to have the necessary permissions to delete the object or objects in question. Active Directory permissions are discussed more extensively in Chapter 14. See Also Recipe 4.24 for information about deleting objects; Recipe 8.19; Recipe 6.39; MSDN: IADsDeleteOps::DeleteObject 4.26. Viewing the Created and Last-Modified Timestamp of an Object Problem You want to determine when an object was either created or last updated. Solution Using a graphical user interface 1. Follow the steps in Recipe 4.2. 2. Ensure that createTimestamp and modifyTimestamp are included in the list of at‐ tributes to be returned by looking at Attributes under Options→Search. Using a command-line interface You can view the created and modified timestamps using the built-in DSQuery utility, as well as AdFind. For DSQuery, use the following syntax: > dsquery * "<ObjectDN>" -attr name createTimestamp modifyTimestamp For AdFind, use the following: > adfind -default -rb cn=Users -f "cn=Joe Smith"↵ createTimestamp modifyTimestamp Using PowerShell The following command gets the creation date and the last-modified date for a user object: Get-ADUser <sAMAccountName> -Properties * | Select Name,whenCreated,Modified 156 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
182
Discussion When an object is created or modified in Active Directory, the createTimestamp and modifyTimestamp attributes get set with the current time. The createTimestamp at‐ tribute is replicated between domain controllers, so assuming the latest modification of the object in question has replicated to all domain controllers, they will all contain the timestamp when the object was created. whenChanged and modifyTimestamp are not replicated, which means that their values will be local to an individual domain controller. Additionally, modifyTimestamp is a constructed attribute. See Also Recipe 4.2 for viewing the attributes of an object; Chapter 12 for a more detailed de‐ scription of the Active Directory replication process 4.27. Modifying the Default LDAP Query Policy Problem You want to view or modify the default LDAP query policy of a forest. The query policy contains settings that restrict search behavior, such as the maximum number of entries that can be returned from a search. Solution Using a graphical user interface 1. Open ADSI Edit. 2. In the Configuration partition, browse to Services→Windows NT→Directory Service→Query Policies. 3. In the left pane, click on the Query Policies container, then right-click on the Default Query Policy object in the right pane and select Properties. 4. Double-click on the lDAPAdminLimits attribute. 5. Click on the attribute you want to modify and click Remove. 6. Modify the value in the “Value to add” box and click Add. 7. Click OK twice. Using a command-line interface To view the current settings, use the following command: > ntdsutil "ldap pol" conn "con to server <DomainControllerName>" q "show values" 4.27. Modifying the Default LDAP Query Policy | 157
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
183
To change the MaxPageSize value to 2,000, you can do the following: > ntdsutil "ldap pol" conn "con to server <DomainControllerName>" q ldap policy: set MaxPageSize to 2000 ldap policy: Commit Changes Discussion The LDAP query policy contains several settings that control how domain controllers handle searches. By default, one query policy is defined for all domain controllers in a forest, but you can create additional ones and apply them to a specific domain controller or even at the site level (so that all domain controllers in the site use that policy). Query policies are stored in the Configuration NC as queryPolicy objects. The default query policy is located at cn=Default Query Policy,cn=Query-Policies,cn=Direc tory Service,cn=Windows NT,cn=Services, <ConfigurationPartitionDN>. The at‐ tribute lDAPAdminLimits of a queryPolicy object is multivalued and contains each setting for the policy in name/value pairs. Table 4-4 contains the available settings. Table 4-4. LDAP query policy settings Name Default value Description MaxPoolThreads 4 per proc Maximum number of threads that are created by the DC for query execution. MaxDatagramRecv 4096 Maximum number of datagrams that can be simultaneously processed by the DC. MaxReceiveBuffer 10485760 Maximum size in bytes for an LDAP request that the server will attempt to process. If the server receives a request that is larger than this value, it will close the connection. InitRecvTimeout 120 secs Initial receive timeout. MaxConnections 5000 Maximum number of open connections. MaxConnIdleTime 900 secs Maximum amount of time a connection can be idle. MaxPageSize 1000 Maximum number of records that will be returned by LDAP responses. MaxQueryDuration 120 secs Maximum length of time the domain controller can execute a query. MaxTempTableSiz 10000 Maximum size of temporary storage that is allocated to execute queries. MaxResultSetSize 262144 Controls the total amount of data that the domain controller stores for this kind of search. When this limit is reached, the domain controller discards the oldest of these intermediate results to make room to store new intermediate results. MaxNotificationPer Conn 5 Maximum number of notifications that a client can request for a given connection. Instead of modifying the default LDAP query policy, you can create a new one from scratch. In the Query Policies container (where the default query policy object is located), create a new queryPolicy object and set the lDAPAdminLimits attribute as just described based on the settings you want configured. Then modify the attribute query PolicyObject on the nTDSDSA object of a domain controller you want to apply the new 158 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
184
policy to. This can be done via the Active Directory Sites and Services snap-in by browsing to the nTDSDSA object of a domain controller (cn=NTDS Settings), right-clicking on it, and selecting Properties. You can then select the new policy from a drop-down menu beside Query Policy. Click OK to apply the new policy. You should not change the default query policy in production unless you’ve done plenty of testing. Changing some of the settings may result in unexpected application or domain controller behavior, such as a sig‐ nificant failure of your Active Directory domain controllers. See Also Recipe 4.19; MS KB 315071 (How to View and Set LDAP Policy in Active Directory by Using Ntdsutil.exe) 4.28. Exporting Objects to an LDIF File Problem You want to export objects to an LDIF file. Solution Using a graphical user interface None of the standard Microsoft tools support exporting LDIF from a GUI. Using a command-line interface > ldifde -f output.ldf -l <AttrList> -p <Scope> -r "<Filter>" -d "<BaseDN>" Discussion The LDIF specification defined in RFC 2849 describes a well-defined file-based format for representing directory entries. The format is intended to be both human- and machine-parseable, which adds to its usefulness. LDIF is the de facto standard for im‐ porting and exporting a large number of objects in a directory and is supported by virtually every directory vendor, including Microsoft. Using a command-line interface The -f switch specifies the name of the file to use to save the entries to, -s is the DC to query, -l is the comma-separated list of attributes to include, -p is the search scope, -r 4.28. Exporting Objects to an LDIF File | 159
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
185
is the search filter, and -d is the base DN. If you encounter any problems using ldifde, the -v switch enables verbose mode and can help identify problems. See Also Recipe 4.29 for importing objects using LDIF; RFC 2849 (The LDAP Data Interchange Format [LDIF]—Technical Specification); MS KB 237677 (Using LDIFDE to Import and Export Directory Objects to Active Directory) 4.29. Importing Objects Using an LDIF File Problem You want to import objects into Active Directory using an LDIF file. The file could contain object additions, modifications, and deletions. Solution Using a command-line interface To import objects using the ldifde utility, you must first create an LDIF file with the objects to add, modify, or delete. Here is an example LDIF file that adds a user: dn: cn=jsmith,cn=users,dc=adatum,dc=com changetype: add objectClass: user samaccountname: jsmith sn: JSmith Once you’ve created the LDIF file, you just need to run the ldifde command to import the new objects: > ldifde -i -f input.ldf Discussion For more information on the LDIF format, check RFC 2849. Using a command-line interface To import with ldifde, simply specify the -i switch to turn on import mode and -f <filename> for the file. It can also be beneficial to use the -v switch to turn on verbose mode to get more information in case of errors. The Windows Server 2003 version of ldifde also includes the –j switch that will create a logfile for troubleshooting purposes. The LDIF file can specify a deletion or a modification instead of an addition. To modify a user, set the changetype to modify. To delete a user, set the changetype to delete. 160 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
186
See Also Recipe 4.28 for information on LDIF; RFC 2849 (The LDAP Data Interchange Format [LDIF]—Technical Specification); MS KB 237677 (Using LDIFDE to Import and Export Directory Objects to Active Directory) 4.30. Exporting Objects to a CSV File Problem You want to export objects to a comma-separated values (CSV) file. The CSV file can then be opened and manipulated from a spreadsheet application or with a text editor. Solution Using a graphical user interface You can export objects to a CSV file using Active Directory Users and Computers, as follows: 1. Open Active Directory Users and Computers. 2. Browse to the desired container or OU. 3. Right-click on the container or OU and click Export List. 4. Choose the CSV file type, name the file, and then click Save to save the file. Using a command-line interface You can export objects to a CSV file using the built-in csvde utility, as well as AdFind. For csvde, use the following syntax: > csvde -f output.csv -l <AttrList> -p <Scope> -r "<Filter>" -d "<BaseDN>" You can also export information to a CSV file using adfind: > adfind -b <SearchBase> -f <Filter> -csv <Attr1> <Attr2> <Attr3> Using PowerShell You can also export objects to a CSV file by using PowerShell. In the following command, all user objects in the Users OU in the adatum.com domain will be exported to c:\temp \users.csv: Get-ADUser -SearchBase "cn=users,dc=adatum,dc=com" -Filter * |↵ Select Name,UserPrincipalName,samAccountName |↵ Export-Csv c:\temp\users.csv 4.30. Exporting Objects to a CSV File | 161
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
187
Discussion Once you have a CSV file containing entries, you can use a spreadsheet application such as Excel to view, sort, and manipulate the data. Using a graphical user interface Before performing the export, add any needed columns to the view in Active Directory Users and Computers. To add columns, click on View and then click Add/Remove Columns. Using a command-line interface The parameters used by csvde are nearly identical to those used by ldifde. The -f switch specifies the name of the file to use to save the entries to, -s is the DC to query, -l is the comma-separated list of attributes to include, -p is the search scope (base, onelevel, or subtree), -r is the search filter, and -d is the base DN. If you encounter any issues, the -v switch enables verbose mode and can help identify problems. AdFind offers a number of additional switches to customize the behavior of CSV file output, including the following: -csv xxx CSV output. xxx is an optional string that specifies the value to use for empty attributes. -csvdelim x Delimiter to use for separating attributes in CSV output. The default is (,). -csvmvdelim x Delimiter to use for separating multiple values in output. The default is (;). -csvq x Character to use for quoting attributes. The default is ("). See Also Recipe 4.31 for importing objects using a CSV file 4.31. Importing Objects Using PowerShell and a CSV File Problem You want to import objects into Active Directory using a CSV file. 162 | Chapter 4: Searching and Manipulating Objects
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
188
Solution Using PowerShell You must first create a CSV file containing the objects to add. The first line of the file should contain a comma-separated list of attributes you want to set, with DN being the first attribute. Here is an example: DN,objectClass,cn,sn,userAccountControl,sAMAccountName,userPrincipalName The rest of the lines should contain entries to add. If you want to leave one of the attributes unset, then leave the value blank (followed by a comma). Here is a sample CSV file that would add two user objects: DN,objectClass,sn,userAccountControl,sAMAccountName,userPrincipalName "cn=jim,cn=users,dc=adatum,dc=com",user,Smith,512,jim,jim@adatum.com "cn=john,cn=users,dc=adatum,dc=com",user,,512,john,john@adatum.com Once you’ve created the CSV file, you just need to import the new objects by using PowerShell: > -Import-CSV c:\temp\input.csv | New-ADUser Discussion Prior to the New-ADUser cmdlet, CSVDE was often used to import objects from a CSV file. However, starting with Windows Server 2008, it became much more difficult to use CSVDE due to the inability to meet password complexity requirements of a domain. CSVDE creates users with a blank password, which usually doesn’t meet password complexity requirements of a domain. With the New-ADUser cmdlet, new disabled users can be created without a password, even if there is a password complexity requirement. See Also Recipe 4.30 for exporting objects in CSV format; Recipe 6.2; Recipe 6.3 4.31. Importing Objects Using PowerShell and a CSV File | 163
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
189
null
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
190
CHAPTER 5 Organizational Units 5.0. Introduction An LDAP directory such as Active Directory stores data in a hierarchy of containers and leaf nodes called the directory information tree (DIT). Leaf nodes are end points in the tree, while containers can store other containers and leaf nodes. In Active Directory, the two most common types of containers are organizational units (OUs) and container objects. The container objects are generic containers that do not have any special prop‐ erties about them other than the fact that they can contain objects. Organizational units, on the other hand, have some special properties, such as the ability to link a Group Policy Object (GPO) to an OU. In most cases when designing a hierarchy of objects in Active Directory, especially users and computers, you should use OUs instead of con‐ tainers. There is nothing you can do with a container that you can’t do with an OU, but the reverse is certainly not the case. The Anatomy of an Organizational Unit Organizational units can be created as a child of a domain object or another OU; by default, OUs cannot be added as a child of a container object. (See Recipe 5.13 for more on how to work around this.) OUs themselves are represented in Active Directory by organizationalUnit objects. Table 5-1 contains a list of some interesting attributes that are available on organizationalUnit objects. Table 5-1. Attributes of organizationalUnit objects Attribute Description description Text description of the OU. gPLink List of GPOs that have been linked to the OU. gPOptions Contains 1 if GPO inheritance is blocked and 0 otherwise. 165
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
191
Attribute Description msDS-Approx-Immed-Subordinates Approximate number of direct child objects in the OU. See Recipe 5.11 for more information. managedBy DN of user or group that is in charge of managing the OU. ou Relative distinguished name of the OU. modifyTimestamp Timestamp of when the OU was last modified. createTimeStamp Timestamp of when the OU was created. 5.1. Creating an OU Problem You want to create an OU. Solution Using a graphical user interface 1. Open the Active Directory Administrative Center. 2. In the left pane, click to highlight the domain. 3. In the right pane, click New and then click Organizational Unit. 4. Enter a name for the OU, enter a description, fill in any of the desired fields, and then click OK to create the OU. Using a command-line interface You can create a new OU using the built-in DSAdd utility, as well as AdMod. To create an OU using DSAdd, use the following syntax: > dsadd ou "<OrgUnitDN>" -desc "<Description>" To create an OU with AdMod, use the following syntax: > admod -b "<OrgUnitDN>" objectclass::organizationalUnit↵ description::"<Description>" -add For example, creating the Finance OU with the description of “Finance OU” in the adatum.com domain would look like this: > admod -b ou=Finance,dc=adatum,dc=com objectclass::organizationalUnit↵ description::"Finance OU" -add > AdMod V01.18.00cpp Joe Richards (joe@joeware.net) March 2012 > 166 | Chapter 5: Organizational Units
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
192
> DN Count: 1 > Using server: dc1.adatum.com > Adding specified objects... > DN: ou=Finance,dc=adatum,dc=com... > > The command completed successfully Using PowerShell To create an organizational unit named “Finance,” create a description of “Finance OU,” and not protect the OU from accidental deletion, use the following command: New-ADOrganizationalUnit "Finance" -ProtectedFromAccidentalDeletion $False↵ -Description "Finance OU" Discussion OUs are used to structure data within Active Directory. Typically, there are three reasons you might need to create an OU: Segregate objects It is common practice to group related data into an OU. For example, user objects and computer objects are typically stored in separate containers (in fact, this is the default configuration with Active Directory). One reason for this is to make search‐ ing the directory easier. Delegate administration One of two primary reasons for creating an OU is to delegate administration. With OUs you can give a person or group of people rights to perform certain adminis‐ trative functions on objects within an OU. Apply a GPO An OU is the lowest-level container object that a GPO can be applied to. If you have different types of users within your organization that need to apply different GPOs, the easiest way to set that up is to store the users in different OUs and apply GPOs accordingly. In each solution in this recipe, the description attribute of the new OU was set. This is not a mandatory attribute, but it is good practice to set it so that others browsing the directory have a general understanding of the purpose of the OU. Also, consider setting the managedBy attribute to reference a user or group that is the owner of the OU. The –ProtectedFromAccidentalDeletion parameter configures the OU so that it cannot be deleted by using the traditional deletion methods (e.g., right-clicking on it and then selecting Delete from the menu). See Also “Create an Organizational Unit Design” 5.1. Creating an OU | 167
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
193
5.2. Enumerating the OUs in a Domain Problem You want to enumerate all containers and OUs in a domain, which effectively displays the structure of the domain. Solution Using a graphical user interface 1. Open the Active Directory Users and Computers snap-in (dsa.msc). 2. If you need to change domains, right-click on Active Directory Users and Com‐ puters in the left pane, select Change Domain, enter the domain name, and click OK. 3. In the left pane, you can browse the directory structure. Using a command-line interface The following command will enumerate all OUs in the current domain of the user running the command using the built-in DSQuery utility: > dsquery ou You can also retrieve this information with AdFind, using the following syntax: > adfind -default -f "objectcategory=organizationalUnit" -dn This adfind syntax can be shortened as follows: > adfind -default -sc oudmp Output from the adfind command will resemble the following: > adfind -default -f "objectcategory=organizationalUnit" -dn > > AdFind V01.46.00cpp Joe Richards (joe@joeware.net) March 2012 > > Using server: dc1.adatum.com:389 > Directory: Windows Server 8 > Base DN: dc=adatum,dc=com > > dn:ou=Domain Controllers,dc=adatum,dc=com > dn:ou=Finance,dc=adatum,dc=com > dn:ou=FinanceTemps,ou=Finance,dc=adatum,dc=com > > 3 Objects returned 168 | Chapter 5: Organizational Units
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
194
Using PowerShell To enumerate all OUs in the current domain using PowerShell, run the following command: Get-ADOrganizationalUnit -Filter * | Select DistinguishedName Discussion Using a graphical user interface If you want to expand all containers and OUs within an OU, you have to manually expand each one within ADUC or the Active Directory Administrative Center; there is no “expand all” option. Using a command-line interface To enumerate both OUs and containers, you have to use a more generic dsquery com‐ mand. The following command will display all containers and OUs in the domain of the user running the command: > dsquery * domainroot -filter↵ "(|(objectcategory=container)(objectcategory=organizationalunit))"↵ -limit 0 5.3. Finding an OU Problem You want to find a specific OU within an Active Directory domain. Solution Using a graphical user interface 1. Open the ADUC snap-in (dsa.msc). 2. If you need to change domains, right-click on the Active Directory Users and Computers label in the left pane, select Change Domain, enter the domain name, and click OK. 3. Right-click on the domain node and select Find. 4. In the Find drop-down box, select Organizational Units. In the Name: text box, enter the name of the OU. 5. Click Find Now. 5.3. Finding an OU | 169
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf
195
Using a command-line interface > adfind -f "ou=<OU Name>" Using PowerShell To find any OU with “Test” in the name under the EMEA OU, run the following command: Get-ADOrganizationalUnit -SearchBase "ou=emea,dc=adatum,dc=com"↵ -LDAPFilter {(ObjectCategory=OrganizationalUnit)} |↵ Where {$_.Name -Match "Test"} The command can be shortened to omit the search base, which will then search the current domain, as shown in the following command: Get-ADOrganizationalUnit -LDAPFilter {(ObjectCategory=OrganizationalUnit)} |↵ Where {$_.Name -Match "Test"} Discussion In a heavily nested environment, you may need to locate an OU based on its name when you don’t necessarily know its location. By using the ADUC GUI or a command-line tool with a search scope of subtree, you can easily recurse through the entire domain structure to find an OU based on its name, description, or any other attribute. In AdFind, you can use wildcards. When designing your Active Directory structure, you should try to keep OU nesting from becoming too deep, since processing many levels of Group Policy Objects can greatly increase the logon times for your cli‐ ents. In the interests of keeping things simple, it’s often a good idea to keep your OU structure shallow whenever possible. See Also Recipe 5.2; Recipe 5.4 5.4. Enumerating the Objects in an OU Problem You want to enumerate all the objects in an OU. 170 | Chapter 5: Organizational Units
http://ter40.free.fr/ebooktmp/new/Active%20Directory%20Cookbook,%204th%20Edition.pdf