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

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

    Join 2,045 other followers

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:

@echo off
:: specify 1 or more computers on the commandline
  if !%1!==!! goto :noName
  goto :haveName
  goto :eof
  call :details %USERDNSDOMAIN% %1
  if !%1!==!! goto :eof
  goto :havename
goto :eof
:: 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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: