output computer, AD User

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
logicassault
Posts: 3
Joined: 23 Apr 2021 12:07

output computer, AD User

#1 Post by logicassault » 23 Apr 2021 12:20

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
  ) 

)

Squashman
Expert
Posts: 4288
Joined: 23 Dec 2011 13:59

Re: output computer, AD User

#2 Post by Squashman » 23 Apr 2021 13:47

Is there a question? I don't see anything syntactically wrong with your code.

logicassault
Posts: 3
Joined: 23 Apr 2021 12:07

Re: output computer, AD User

#3 Post by logicassault » 23 Apr 2021 14:18

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.

aGerman
Expert
Posts: 4140
Joined: 22 Jan 2010 18:01
Location: Germany

Re: output computer, AD User

#4 Post by aGerman » 25 Apr 2021 07:25

"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

logicassault
Posts: 3
Joined: 23 Apr 2021 12:07

Re: output computer, AD User

#5 Post by logicassault » 26 Apr 2021 11:22

Perfect. thanks.

Post Reply