How can I take output of psloggedon command to a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
manasrrp
Posts: 4
Joined: 01 Feb 2018 23:41
Contact:

How can I take output of psloggedon command to a variable

#1 Post by manasrrp » 03 Mar 2018 08:43

How can I take output of psloggedon command to a variable

for example

set lgnuser= output user name of psloggedon command

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How can I take output of psloggedon command to a variable

#2 Post by penpen » 03 Mar 2018 11:39

I don't have this program, so i don't know the possible output, but in case the above is a cmd-shell program you could try to use a for/F loop (simple version; assumed data contains no exclamation mark):

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "user.length=0"
for /F "tokens=*" %%a in ('psloggedon') do (
	set /a "user.length+=1"
	set "user[!user.length!]=%%~a
)
for /l %%a in (1, 1, %user.length%) do echo(user[%%~a]=!user[%%~a]!
endlocal
goto :eof
Note that the above only works, if "psloggedon" prints the data to stdout.


penpen

manasrrp
Posts: 4
Joined: 01 Feb 2018 23:41
Contact:

Re: How can I take output of psloggedon command to a variable

#3 Post by manasrrp » 04 Mar 2018 00:06

C:\>psloggedon

PsLoggedon v1.35 - See who's logged on
Copyright (C) 2000-2016 Mark Russinovich
Sysinternals - www.sysinternals.com

Users logged on locally:
04-03-2018 10:13:06 HOSTPC49\userID26

No one is logged on via resource shares.

--------------------------------------------------------------------------------
As above , I want to store bold output to a variable declared in set command.

manasrrp
Posts: 4
Joined: 01 Feb 2018 23:41
Contact:

Re: How can I take output of psloggedon command to a variable

#4 Post by manasrrp » 04 Mar 2018 11:04

After creating it to a batch file the output is as below

user[1]=PsLoggedon v1.35 - See who's logged on
user[2]=Copyright (C) 2000-2016 Mark Russinovich
user[3]=Sysinternals - www.sysinternals.com
user[4]=Users logged on locally:
user[5]=04-03-2018 22:10:42 HOSTPC49\userID26
user[6]=No one is logged on via resource shares.

But my requirement is to extract only the below output and store it to a variable

userID26

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

Re: How can I take output of psloggedon command to a variable

#5 Post by Squashman » 04 Mar 2018 11:52

Inside the first FOR command, pipe the output of psloggedon to the find command and use the computername as the search argument.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How can I take output of psloggedon command to a variable

#6 Post by penpen » 05 Mar 2018 07:02

The profile might not be stored locally, so searching for the computer might be insufficient.
But now that i know where to find PsLoggedOn, i downloaded it and found a usefull switch ("-x"), so that the following hopefully should list all users logged in (assumed, that the list doesn't change in between):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
>"temp.txt" PsLoggedOn.exe -nobanner

set "user.length=0"
for /f "tokens=*" %%a in ('PsLoggedOn.exe -nobanner -x ^| findstr /B /L /X /V /G:"temp.txt"') do (
	set /a "user.length+=1"
	for /F "tokens=* delims=" %%b in ('set /a "user.length"') do set "user[%%~b]=%%~a
)

for /l %%a in (1, 1, %user.length%) do set "user[%%~a]"

del "temp.txt"

endlocal
goto :eof
Alternatively you could create a search file manually (which has the advantage of always giving correct results):

Code: Select all

>"temp.txt" (
	echo(Users logged on locally:
	echo(
	echo(No one is logged on via resource shares.
	rem (...
)
penpen

Edit: Corrected a flaw in the solution and added alternative creation of "temp.txt".

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How can I take output of psloggedon command to a variable

#7 Post by penpen » 06 Mar 2018 06:30

I just noticed, that you don't need any temp file:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set "path=%path%;.\PSTools"
set "user.length=0"

for /f "tokens=*" %%a in ('PsLoggedOn.exe -nobanner -x ^| findstr /R /C:"^[ ]"') do (
	set /a "user.length+=1"
	for /F "tokens=* delims=" %%b in ('set /a "user.length"') do set "user[%%~b]=%%~a
)

for /l %%a in (1, 1, %user.length%) do set "user[%%~a]"

endlocal
goto :eof
penpen

Post Reply