Using dsquery and dsget to get computer information from a domain
Posted by jpluimers on 2014/05/13
This article is a very brief example of how to use dsquery/dsget/find to get computer information from in the active direrctory of a domain.
The main aim for myself is to condense the information here, and have some links for background information.
If you have the right credentials then the below batch file works very well.
It uses these tools:
- dsquery to query the active directory on your domain controller for the existence and Distinguished Name (or ID/path) of various objects (in this example dsquery computer to check if a computer exists in a domain)
- dsget which can get you various detail information about an object (for instance dsget computer used in this example))
- find to raise the correct errorlevel (and indicate if we indeed found a CN – or Common Name – from a distinguished name)
The ds* tools do not raise any errorlevel, so that’s what find is used for.
Further reading:
- as the AD uses LDAP:
Active Directory – Wikipedia, the free encyclopedia.
Lightweight Directory Access Protocol – Wikipedia, the free encyclopedia. - SolutionBase: Getting started with Windows Server 2003’s directory service command-line tools | TechRepublic.
- SolutionBase: Using the Dsquery command in Windows Server 2003 | TechRepublic.
- SolutionBase: Using the Dsget command in Windows Server 2003 | TechRepublic.
- Search user in AD using dsquery and dsget – Collection.
- Episode 9 – Easy But Useful Windows Domain Queries.
Without further ado, the batch file:
@echo off
:: specify 1 or more computers on the commandline
if !%1!==!! goto :noName
goto :haveName
:noName
echo %COMPUTERNAME%
call :details %USERDNSDOMAIN% %COMPUTERNAME%
call :details %USERDOMAIN% %COMPUTERNAME%
goto :eof
:haveName
call :details %USERDNSDOMAIN% %1
shift
if !%1!==!! goto :eof
goto :havename
goto :eof
:details
:: FIND sets errorlevel
dsquery computer domainroot -d %1 -name %2 | find "CN">nul
if %errorlevel% EQU 0 (
dsquery computer domainroot -d %1 -name %2
dsquery computer domainroot -d %1 -name %2 | dsget computer -l -dn -samid -sid -desc -loc -disabled
dsquery computer domainroot -d %1 -name %2 | dsget computer -memberof
)
goto :eof
–jeroen






Leave a comment