Search code examples
batch-filedsquery

dsquery computer with no description


I'm trying to make a batch script to list all laptops in my domain that doesn't have a computer description.

I can't seem to make DSQUERY find computers without descriptions Example:

DSQUERY Computer -name [COMPUTERNAMES]* -desc "" -limit 0 | dsget computer -dn -desc
dsquery failed:The search filter cannot be recognized.
type dsquery /? for help.
dsget failed:'Target object for this command' is missing.
type dsget /? for help.

In the example above I tried to search for -desc "" which doesn't work. Do any of you have some trick to list computers without descriptions? Grateful for every reply!

I have revised my script a bit to actually break apart the 'dsget computer -dn -desc' like this:

DSQUERY Computer -name LAPTOP* -limit 0 | dsget computer -dn -desc>computers_unformatted.txt
FOR /F "tokens=2,15,16,17,18,19,20 delims=,= " %%a IN (computers_unformatted.txt) DO (
    ECHO.%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g>>computers_unformatted2.txt
)

Output looks like this:

LAPTOP039|John|Doe|T430|Windows7|| 
LAPTOP040|||||| 

I'm onto something good here but I can't seem to write anything that makes the output become just (in the example output above) 'LAPTOP040'


Solution

  • Here you go:

    @echo off
    setlocal
    
    set "dsq=dsquery * dc^=acme^,dc^=one^,dc^=com -limit 0 -filter "^&^(ObjectCategory^=Computer^)^(ObjectClass^=Computer^)" -attr cn description"
    for /f "skip=1 tokens=1,2*" %%a in ('%dsq%') do ( 
      if "%%b" EQU "" echo %%a is missing a description
    )
    exit /b
    

    And just for fun, I whipped this up

    @echo off
    setlocal enabledelayedexpansion
    
    set "dsq=dsquery * dc^=acme^,dc^=one^,dc^=com -limit 0 -filter "^&^(ObjectCategory^=Computer^)^(ObjectClass^=Computer^)" -attr cn description"
    for /f "skip=1 tokens=1,2*" %%a in ('%dsq%') do ( 
        if "%%b" EQU "" (
        echo(%%a is missing a description
        set /p "ans=Would you like to add a description for %%a now? (Y/N) "
            if /i "!ans!" EQU "Y" ( 
              set /p "desc=Enter a new description for %%a: "
              dsadd computer "%%a" -desc !desc!
            ) ELSE (
              Echo Moving on to next computer.
            )
        )
    )
    exit /b