JSI Tip 9855. How can I return the NetBIOS and distinguished name of all computers in my Active Directory domain, using built-in commands?

Jerold Schulman

October 27, 2005

1 Min Read
ITPro Today logo in a gray background | ITPro Today

Using only built-in commands, I have scripted ADComp.bat to return the NetBIOS and distinguished name of all the computers in the domain you are logged on to.

The syntax for using ADComp.bat is:

for /f "Tokens=1* Delims=;" %%c in ('call ADComp') do ( set name=%%c set dn=%%d . . .)

NOTE: The output of ADComp.bat is a semi-colon delimited string, like:

JSI001;"CN=JSI001,OU=Domain Controllers,DC=JSIINC,DC=COM"JSI009;"CN=JSI009,CN=Computers,DC=JSIINC,DC=COM"

ADComp.bat contains:

@echo offsetlocalif exist "%TEMP%ADComp.vbs" goto [email protected] Error Resume Next>"%TEMP%ADComp.vbs"@echo.Dim objConnection, objCommand, objRootDSE, strDNSDomain>>"%TEMP%ADComp.vbs"@echo.Dim strFilter, strQuery, objRecordSet>>"%TEMP%ADComp.vbs"@echo.Set objConnection = CreateObject("ADODB.Connection")>>"%TEMP%ADComp.vbs"@echo.Set objCommand = CreateObject("ADODB.Command")>>"%TEMP%ADComp.vbs"@echo.objConnection.Provider = "ADsDSOOBject">>"%TEMP%ADComp.vbs"@echo.objConnection.Open "Active Directory Provider">>"%TEMP%ADComp.vbs"@echo.Set objCommand.ActiveConnection = objConnection>>"%TEMP%ADComp.vbs"@echo.Set objRootDSE = GetObject("LDAP://RootDSE")>>"%TEMP%ADComp.vbs"@echo.strDNSDomain = objRootDSE.Get("defaultNamingContext")>>"%TEMP%ADComp.vbs"@echo.strBase = "" >>"%TEMP%ADComp.vbs"@echo.strFilter = "(&(objectCategory=computer))">>"%TEMP%ADComp.vbs"@echo.strAttributes = "distinguishedName,Name">>"%TEMP%ADComp.vbs"@echo.strQuery = strBase ^& ";" ^& strFilter ^& ";" ^& strAttributes ^& ";subtree">>"%TEMP%ADComp.vbs"@echo.objCommand.CommandText = strQuery>>"%TEMP%ADComp.vbs"@echo.objCommand.Properties("Page Size") = 99999>>"%TEMP%ADComp.vbs"@echo.objCommand.Properties("Timeout") = 300>>"%TEMP%ADComp.vbs"@echo.objCommand.Properties("Cache Results") = False>>"%TEMP%ADComp.vbs"@echo.Set objRecordSet = objCommand.Execute>>"%TEMP%ADComp.vbs"@echo.objRecordSet.MoveFirst>>"%TEMP%ADComp.vbs"@echo.Do Until objRecordSet.EOF>>"%TEMP%ADComp.vbs"@echo.    strDN = objRecordSet.Fields("distinguishedName")>>"%TEMP%ADComp.vbs"@echo.    strSA = objRecordSet.Fields("Name")>>"%TEMP%ADComp.vbs"@echo.    Wscript.Echo strSA ^& ";

^& strDN ^&

">>"%TEMP%ADComp.vbs"@echo.    objRecordSet.MoveNext>>"%TEMP%ADComp.vbs"@echo.Loop>>"%TEMP%ADComp.vbs"@echo.objConnection.Close>>"%TEMP%ADComp.vbs"@echo.Set objConnection = Nothing>>"%TEMP%ADComp.vbs"@echo.Set objCommand = Nothing>>"%TEMP%ADComp.vbs"@echo.Set objRootDSE = Nothing>>"%TEMP%ADComp.vbs"@echo.Set objRecordSet = Nothing>>"%TEMP%ADComp.vbs":startcscript //nologo "%TEMP%ADComp.vbs"endlocal



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like