Page 1 of 1

output computer, AD User

Posted: 23 Apr 2021 12:20
by logicassault
I'm trying to have a batch file read a list of computers from a text file and do a query to the computer for the active username and then do a dsquery to get the display name for the associated username. Then output the results of COMPUTERNAME and the DISPLAYNAME it finds in AD for the user.

Code: Select all

@Echo OFF
For /F "Usebackq Delims=" %%# in (
    "C:\users\myuser\desktop\List.txt"
  ) do (
  for /F "tokens=1" %%f in (
    'query user /server:%%# ^| find "Active"'
  ) do (
  dsquery user -samid %%f|dsget user -display  >> C:\output.txt
  ) 

)

Re: output computer, AD User

Posted: 23 Apr 2021 13:47
by Squashman
Is there a question? I don't see anything syntactically wrong with your code.

Re: output computer, AD User

Posted: 23 Apr 2021 14:18
by logicassault
I got it almost working..

Code: Select all

@Echo OFF
For /F "Usebackq Delims=" %%# in ("C:\users\user\desktop\List.txt") do (
  for /F "tokens=1" %%f in ('query user /server:%%# ^| find "Active"') do (

    for /F "skip=1 tokens=1,2" %%u in ('dsquery user -samid %%f^|dsget user -display') do (
    if not %%u=="dsget succeeded" echo %%# %%u %%v >> C:\output.txt
    )
  ) 
)
The problem is the dsget command outs an extra line at the end that says "dsget succeeded" and I need to make it not write this line.

Re: output computer, AD User

Posted: 25 Apr 2021 07:25
by aGerman
"dsget succeeded" will be tokenized where only "dsget" is assigned to %%u. Maybe just use another FIND or FINDSTR filter, like this:

Code: Select all

    for /F "skip=1 tokens=1,2" %%u in ('dsquery user -samid %%f^|dsget user -display^|findstr /ivc:"dsget succeeded"') do (
Steffen

Re: output computer, AD User

Posted: 26 Apr 2021 11:22
by logicassault
Perfect. thanks.