0phoi5 Posted December 15, 2015 Share Posted December 15, 2015 (edited) Hi all,More batch script goodness. I'm using PSTools' psloggedon for this.In CMD, if I input 'psloggedon -l -x \\HOSTNAME' I get the following result... Connecting to Registry of \\HOSTNAME... Users logged on locally: DOMAIN\USER_ONE DOMAIN\USER_TWO DOMAIN\USER_THREE DOMAIN\USER_FOUR Using FOR in a batch script, I would expect skipping the first 2 populated lines (using /F) and setting the delimiter as '\' should allow me to pipe just the usernames to the screen, however it isn't working.I am using the following... @echo off for /f "skip=2 tokens=2 delims=\" %%a in ('psloggedon -l -x \\%Hostname%') do set "LoggedOnUsers=%%a" echo "%LoggedOnUsers%" ...which results in the variable %LoggedOnUsers% being echo'd as simply... USER_TWO The end result I am aiming for is to output a list of all usernames logged on to a Hostname, regardless of whether it's just 1 user or many users, and then offer a CHOICE to the user of which username they want to select. For example, if a machine had 4 users logged on to it, the return would be; A. USER_ONE B. USER_TWO C. USER_THREE D. USER_FOUR Select a user : A,B,C,D [set variable based on whether input is A, B, C or D to the username for that selection] Where am I going wrong?Thank you. *edit* Amended spelling. Edited December 15, 2015 by haze1434 Quote Link to comment Share on other sites More sharing options...
cooper Posted December 15, 2015 Share Posted December 15, 2015 Let me rewrite your script in java, so you might see more clearly where the problem lies. String user; for (String username : scriptThatIsolatesUserNames()) { user = username; } System.out.println( user ); The clue is in the curly braces. Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted December 15, 2015 Author Share Posted December 15, 2015 I'm not very au fait with Java, you're making me think too hard for a work morning cooper! Is it down to me setting a variable with a variable? Should I just stick to echoing %%a instead of setting %LoggedOnUsers%? Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted December 15, 2015 Author Share Posted December 15, 2015 (edited) Ah, this seems to work... @echo off set "userone=" set "usertwo=" set "userthree=" set "userfour=" for /F "skip=1 tokens=2 delims=\" %%a in ('psloggedon -l -x \\%Hostname%') do if not defined userone set userone=%%a for /F "skip=2 tokens=2 delims=\" %%b in ('psloggedon -l -x \\%Hostname%') do if not defined usertwo set usertwo=%%b for /F "skip=3 tokens=2 delims=\" %%c in ('psloggedon -l -x \\%Hostname%') do if not defined userthree set userthree=%%c for /F "skip=4 tokens=2 delims=\" %%d in ('psloggedon -l -x \\%Hostname%') do if not defined userthree set userthree=%%d echo A. %userone% echo B. %usertwo% echo C. %userthree% echo D. %userfour% Does this seem OK? I don't want to be using 'bad code'. Thank you. Edited December 15, 2015 by haze1434 Quote Link to comment Share on other sites More sharing options...
cooper Posted December 15, 2015 Share Posted December 15, 2015 The point was that you were consistently overwriting the variable from within the loop, then checking the value of the variable outside of the loop. For will (should) do the bit after "do" (but on the same line) for each iteration. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.