New to scripting trying to get a basic one to work - Pull Logged in Users with a Custom Inventory Rule
Good morning all,
This all comes from needing a way to find out if anyone is currently logged. I can do this from the command line with:
query user /server:computername
I would like to just make this a custom inventory rule but apparently this will not run as localuser so I was advised to run it as a script, output it to a log then have a custom inventory rule just read the log.
i can run this process manually on my own PC. First in the CLI I run :
query user > c:\dell\query_user.txt
then from the CLI I run:
notepad.exe c:\dell\query_user.txt
So my question is, how do I setup the first part of this to run (query user /server:computername) to run as a script so I can then run a custom inventory rule to read the log?
Many thanks
Answers (2)
Hi, Welcome.
Let's do this....
1- Create a BAT file that will run the command output the results into a TXT file, I'm going to named it Query-User.bat
2- Create a new Script (Don't forget to target the computer or use device labels as target, and select your O.S.) and Launch your BAT File Like this:
3- Use a Custom Inventory Rule to read the TXT file and return what's inside:
ShellCommandTextReturn(cmd /c type C:\ProgramData\Quest\Users.txt)
4- Wait for OR Force Inventory on that Computer, and then you should be able to see THIS!
**We have admin, batman and robin currently logged in, robin is the last active user, he logged in one minute after batman**
This is how it looks like from the computer:
Let us know how that goes.
You need to remember that the information returned by a custom inventory rule is only updated when the computer runs an inventory update. So your "Current logged in user" will be the user that was logged into the computer when the inventory ran, which is not necessarily who is logged in when you are looking at the data. Also, if you are using a script to output the information and then upload it to the SMA, the data is dependent on when the script ran, so unless you are scheduling the script to run in conjunction with your inventory interval your "Current logged in user" will be even more inaccurate.
In reality, if you really need to know the user that is currently logged into a computer you will have to use a tool that queries that data in real time. I prefer using PSTools' psloggedon, personally.