Q. How can my batch retrieve the Common Name of the current computer?

Jerold Schulman

December 17, 2006

1 Min Read
ITPro Today logo

Every object in Active Directory has a unique Distinguished Name, as in CN=JSI009,CN=Computers,DC=JSIINC,DC=COM, where the first CN= defines the Common Name, JSI009.

Using standard commands, I have scripted ComputerCN.bat to retrieve the Common Name of the current computer.

The syntax for using ComputerCN.bat is:

[call] ComputerCN CN

Where CN is a call directed environment variable that will contain the computer's Common Name.

ComputerCN.bat contains:

@echo offif {%1}=={} @echo Syntax: ComputerCN CN&goto :EOFif exist "%TEMP%ComputerCN.vbs" goto [email protected] objSysInfo = CreateObject("ADSystemInfo") >"%TEMP%ComputerCN.vbs"@echo.strComputerDN = objSysInfo.ComputerName>>"%TEMP%ComputerCN.vbs"@echo.Set objComputer = GetObject("LDAP://" ^& strComputerDN)>>"%TEMP%ComputerCN.vbs"@echo.Wscript.Echo objComputer.cn>>"%TEMP%ComputerCN.vbs":doitset %1=Not Foundfor /f "Tokens=*" %%a in ('cscript //nologo "%TEMP%ComputerCN.vbs"') do ( set %1=%%a)


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