Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

How can I take output of psloggedon command to a variable


  • Please log in to reply
8 replies to this topic

#1 manasrrp

manasrrp

  • Members
  • 11 posts
  • OFFLINE
  •  
  • Local time:01:02 PM

Posted 03 March 2018 - 09:41 AM

How can I take output of psloggedon command to a variable
 
for example
 
set lgnuser=  output user name of psloggedon command
 
C:\>psloggedon  \\HOSTPC49
 
PsLoggedon v1.35 - See who's logged on
Copyright © 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.

Edited by manasrrp, 04 March 2018 - 01:16 AM.


BC AdBot (Login to Remove)

 


#2 britechguy

britechguy

    Been there, done that, got the T-shirt


  • Moderator
  • 8,708 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Staunton, VA
  • Local time:03:32 AM

Posted 03 March 2018 - 10:39 AM

Here is an example from a .bat file I've been using for years to parse out the current system date.

 

REM Snag today's actual system date
set DATESNAG=%DATE%
 
REM Get the first character from the DATE command
set FIRSTCHAR=%DATESNAG:~0,1%

Brian  AKA  Bri the Tech Guy (website in my user profile) - Windows 10 Home, 64-Bit, Version 1803, Build 17134 

     . . . the presumption of innocence, while essential in the legal realm, does not mean the elimination of common sense outside it.  The willing suspension of disbelief has its limits, or should.

    ~ Ruth Marcus,  November 10, 2017, in Washington Post article, Bannon is right: It’s no coincidence The Post broke the Moore story


 

 

 

              

 


#3 manasrrp

manasrrp
  • Topic Starter

  • Members
  • 11 posts
  • OFFLINE
  •  
  • Local time:01:02 PM

Posted 04 March 2018 - 01:07 AM

C:\>psloggedon  \\HOSTPC49
 
PsLoggedon v1.35 - See who's logged on
Copyright © 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.

Edited by manasrrp, 04 March 2018 - 01:10 AM.


#4 britechguy

britechguy

    Been there, done that, got the T-shirt


  • Moderator
  • 8,708 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Staunton, VA
  • Local time:03:32 AM

Posted 04 March 2018 - 11:21 AM

REM  Set the machine name variable to whatever you need it to be, possibly from a different variable
Set MACHNM=HOSTPC49
 
psloggedon  \\%MACHNM% | FINDSTR "%MACHNM%" > junk.txt
 
SET /P LOGGEDON=<junk.txt
 
del junk.txt
 
SET LOGGEDON=%LOGGEDON:~20%
 
----------------------------------------------------------------------
 
The final set statement presumes that you will always have the date-time stamp at the start of the line as "mm-dd-yyyy hh:mm:ss " or "dd-mm-yyyy hh:mm:ss "

Brian  AKA  Bri the Tech Guy (website in my user profile) - Windows 10 Home, 64-Bit, Version 1803, Build 17134 

     . . . the presumption of innocence, while essential in the legal realm, does not mean the elimination of common sense outside it.  The willing suspension of disbelief has its limits, or should.

    ~ Ruth Marcus,  November 10, 2017, in Washington Post article, Bannon is right: It’s no coincidence The Post broke the Moore story


 

 

 

              

 


#5 britechguy

britechguy

    Been there, done that, got the T-shirt


  • Moderator
  • 8,708 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Staunton, VA
  • Local time:03:32 AM

Posted 04 March 2018 - 12:03 PM

By the way, the above also presumes you are always going to have a single line that would match the string in FINDSTR.

 

If you need to do this cyclically with multiple strings it gets more complicated and a FOR loop would be needed.


Brian  AKA  Bri the Tech Guy (website in my user profile) - Windows 10 Home, 64-Bit, Version 1803, Build 17134 

     . . . the presumption of innocence, while essential in the legal realm, does not mean the elimination of common sense outside it.  The willing suspension of disbelief has its limits, or should.

    ~ Ruth Marcus,  November 10, 2017, in Washington Post article, Bannon is right: It’s no coincidence The Post broke the Moore story


 

 

 

              

 


#6 manasrrp

manasrrp
  • Topic Starter

  • Members
  • 11 posts
  • OFFLINE
  •  
  • Local time:01:02 PM

Posted 04 March 2018 - 12:28 PM

I am not expert in batch scripting but little bit idea on it and I had change your script to as below 

 

@ECHO OFF
REM  Set the machine name variable to whatever you need it to be, possibly from a different variable
PSLOGGEDON  \\%COMPUTERNAME% | FINDSTR "%COMPUTERNAME%" > junk.txt
SET /P LOGGEDON=<junk.txt
DEL junk.txt
SET LOGGEDON=%LOGGEDON:~20%
ECHO %LOGGEDON%
 
---------------------------------
Output after running the batch as 
 
ry of \\HOSTPC49...


#7 manasrrp

manasrrp
  • Topic Starter

  • Members
  • 11 posts
  • OFFLINE
  •  
  • Local time:01:02 PM

Posted 04 March 2018 - 12:51 PM

Actually PSLOGGEDON is a command line tool developed by Sysinternals - www.sysinternals.com 

The tool is written to display the information of locally as well as remote computer loggedon user with below format.

 

c:\Users\Manas\Desktop>psloggedon \\%computername%

 
PsLoggedon v1.35 - See who's logged on
Copyright © 2000-2016 Mark Russinovich
Sysinternals - www.sysinternals.com
 
Users logged on locally:
     04-03-2018 22:10:42        HOSTPC49\userID26 
 
No one is logged on via resource shares.
 
I only want to extract and store the user ID i.e. userID26 to a variable, that only.

Edited by manasrrp, 04 March 2018 - 12:54 PM.


#8 britechguy

britechguy

    Been there, done that, got the T-shirt


  • Moderator
  • 8,708 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Staunton, VA
  • Local time:03:32 AM

Posted 04 March 2018 - 01:22 PM

@ECHO OFF
REM  Set the machine name variable to whatever you need it to be, possibly from a different variable
PSLOGGEDON  \\%COMPUTERNAME% | FINDSTR "%COMPUTERNAME%" > junk.txt
SET /P LOGGEDON=<junk.txt
DEL junk.txt
SET LOGGEDON=%LOGGEDON:*\=%
ECHO %LOGGEDON%
 
----------------------------------------------------------------
 
If what you want is only what comes after the '\' character in the line, the substitute the set statement in bold above for the prior one that used the hard coded count of 20.  This is better, anyway, as it won't matter what the length of the computer name is.  That SET command will strip everything from the start of the string up to and including the "\" character.

Brian  AKA  Bri the Tech Guy (website in my user profile) - Windows 10 Home, 64-Bit, Version 1803, Build 17134 

     . . . the presumption of innocence, while essential in the legal realm, does not mean the elimination of common sense outside it.  The willing suspension of disbelief has its limits, or should.

    ~ Ruth Marcus,  November 10, 2017, in Washington Post article, Bannon is right: It’s no coincidence The Post broke the Moore story


 

 

 

              

 


#9 manasrrp

manasrrp
  • Topic Starter

  • Members
  • 11 posts
  • OFFLINE
  •  
  • Local time:01:02 PM

Posted 06 March 2018 - 01:06 PM

Hi,

 

I had run & tested your modified script , it display all most 90% accurate result. I think if you download this PsTools from www.sysinternals.com web site, you can batter understand how this command will display output on screen. Basically this tool is used to display all the computers name where user has logged on, either locally or any other system inside a domain network by using proper syntax.

 

For Ex: if we use simple c:\>psloggedon   it display only the user name logged on local system

   But    if we use as like c:\>psloggedon -l -x userID   it scan all computers throughout the domain network and display only the computer list where user has loggedon on by its ID.

 

Location of text character is different in both the case according to use of command. 

 

Actually my further purpose is to use that variable in IF statement command line to execute next script.


Edited by manasrrp, 06 March 2018 - 01:11 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users