Skip navigation

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

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 off
if \{%1\}==\{\} @echo Syntax: ComputerCN CN&goto :EOF
if exist "%TEMP%\ComputerCN.vbs" goto doit
@echo.Set 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"
:doit
set %1=Not Found
for /f "Tokens=*" %%a in ('cscript //nologo "%TEMP%\ComputerCN.vbs"') do (
 set %1=%%a
)


Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish