How can I take output of psloggedon command to a variable
for example
set lgnuser= output user name of psloggedon command
How can I take output of psloggedon command to a variable
Moderator: DosItHelp
Re: How can I take output of psloggedon command to a variable
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):
Note that the above only works, if "psloggedon" prints the data to stdout.
penpen
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
penpen
Re: How can I take output of psloggedon command to a variable
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.
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.
Re: How can I take output of psloggedon command to a variable
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
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
Re: How can I take output of psloggedon command to a variable
Inside the first FOR command, pipe the output of psloggedon to the find command and use the computername as the search argument.
Re: How can I take output of psloggedon command to a variable
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):
Alternatively you could create a search file manually (which has the advantage of always giving correct results):
penpen
Edit: Corrected a flaw in the solution and added alternative creation of "temp.txt".
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
Code: Select all
>"temp.txt" (
echo(Users logged on locally:
echo(
echo(No one is logged on via resource shares.
rem (...
)
Edit: Corrected a flaw in the solution and added alternative creation of "temp.txt".
Re: How can I take output of psloggedon command to a variable
I just noticed, that you don't need any temp file:
penpen
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