The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Email Subscription

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 716 other subscribers

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:

Without further ado, the batch file:

[sourcecode language=”text”]
@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
[/sourcecode]

–jeroen

Leave a Comment

 

Discover more from The Wiert Corner - irregular stream of stuff

Subscribe now to keep reading and get access to the full archive.

Continue reading