0

I am trying to make a batch file that will go through a text file of computer names and get the username that is logged in on each. I am not sure how to set the variables I need within the loops. I have tried several of the solutiions that I have found on here but none of them seem to work. output.txt contains:

PCHTW-HTT10425 

PCHTW-HTT10437

These are the computer names that I am trying to pass. When I echo %%A it gives the names, but setting name to %%A does nothing. Reading up on it I thought adding in the SETLOCAL EnableDelayedExpansion would do the trick but it appears to have done nothing. I have also tried echo name:!name! but it also doesn't work.

 for /F "tokens=*" %%A in (%FILEPATH%output.txt) do (
        setlocal EnableDelayedExpansion
        echo %%A
        SET /p name=%%A
        echo name:%name%
        FOR /F "tokens=2 delims=\" %%F IN ('wmic /NODE:"%name%" computersystem get UserName') DO (
            SET username=%%F
        )
        echo %username%
        pause
        ENDLOCAL
    )
Auguste
  • 2,007
  • 2
  • 17
  • 25
shoes
  • 15
  • 6
  • you need [delayed expansion](http://stackoverflow.com/a/30284028/2152082). Delete `SET /p name=%%A`. Don't set `%username%`, it's a systemvariable - choose another name, maybe `%usrname%`. (you don't need the variable `%name%`- just use `%%A` instead). – Stephan May 27 '16 at 14:31
  • @Stephan Would I then be able to pass !usrname! on to another command. For example if I wanted to schedule a task on a computer could I do `schtasks /Create /ru !usrname!`? – shoes May 27 '16 at 14:59
  • Yes, why not? Inside the `for` loop you can even use `schtasks /Create /ru %%F` - wait - there is that ugly `CR`... Wait, I'll add an answer. – Stephan May 27 '16 at 15:04

2 Answers2

0

Your question is HOW TO GET LOGGEDON USER FOR A LIST OF COMPUTERS

How does my correct answer not answer the question. MINE IS ONE LINE.

wmic /output:"c:\Outputfile.html" /node:@"COMPUTERLIST.TXT" computersystem get username /format:htable

Putting an @ sign in front of node means node contains a text file of computernames or IP addresses (you can mix and match).

See wmic /?, wmic /node /?, wmic /output /?, wmic /append /?, wmic computersystem /?, wmic computersystem get /?, wmic computersystem call /?, wmic computersystem set /?, wmic /format /?.

To get a list of currently turned on computers

for /f "skip=3" %A in ('net view ^| findstr /v /C:"The command completed successfully"') do Echo %A

And without leading \\ as required by wmic

for /f "skip=3 delims=\" %%A in ('net view ^| findstr /v /C:"The command completed successfully"') do Echo %%A
0
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (%FILEPATH%output.txt) do (
  echo %%A
  FOR /F "tokens=2 delims=\" %%F IN ('wmic /NODE:"%%A" computersystem get UserName') DO (
    for /f "delims=" %%B in ("%%F") do ( 
      echo %%B
      schtasks /create /ru %%B ...
    )
  )
  pause
)

NOTE: wmic has ugly line endings (an additional Carriage Return), so there is some more code to get rid of that. I used one of several possibilities from dbenham.

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91