Directory Services |
At the top of the Active Directory® tree, above all directory providers, is the Namespaces container, which implements the IADsNamespaces COM interface. To write a script that can operate in multiple environments, the Namespaces container will provides your script access to directory services independent of the namespace provider. Binding to the Namespaces container first enables the same script access to WinNT, LDAP, NDS, NWCOMPAT, or other providers without having to write a separate script for each.
To bind to the Namespaces container, use the following syntax.
Set myObj = GetObject("ADs:")
The following code example lists the namespace providers installed on a computer.
[Visual Basic]
Dim myADS
Dim member
Set myADS = GetObject("ADs:")
For Each member In myADS
WScript.Echo member.Name
Next
From here, it is possible to find the domains within each namespace. The following code example enumerates the Namespaces container to identify what providers are available, then it enumerates each provider namespace to return the domains available under that namespace.
[Visual Basic]
Dim myADS
Dim namespace
Dim domain
Set myADS = GetObject("ADs:")
On Error Resume Next
For Each namespace In myADS
WScript.Echo "Domains in " & namespace.Name
For Each domain In namespace
If Err.Number = &H800704B8 Then
WScript.Echo " This namespace contains no domains"
Elseif Err.Number <> 0 Then
WScript.Echo "Unexpected error: " & Err.Number
WScript.Echo Err.Description
WScript.Quit(1)
Else
WScript.Echo " " & domain.Name
End If
Next
Next
Be aware of the On Error Resume Next statement in the code example. If a namespace does not contain any domains, ADSI will return the hex error code 800704B8 (an "Extended Error") and stop execution of the script. NDS and NWCOMPAT also return an error if the namespace is empty, however the LDAP provider and others do not. The use of the On Error and If Err.Number statements, later in the script, allows the code to continue enumerating other provider's namespaces without stopping if one of them contains no domains.
The error code listed above is ADSI specific. Error codes vary by provider and are generally provider-specific. For other providers, refer to their error code listings. For more information about trapping ADSI errors, see Errors and Error Trapping.