Page 1 of 1

Get Currently logged in user

Posted: Thu May 24, 2007 8:47 pm
by keith
Is it possible to get the currently logged in user on a machine. I know that you can get it via wscriptwitht he following code:

Function QPO
On Error Resume next
strLookFor = "Kbyrum"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'explorer.exe'")
For Each objProcess in colProcessList
objProcess.GetOwner strUserName, strUserDomain
'WScript.StdOut.Write strInput
Wscript.stdout.write strComputer &",Is logged into by "&strUserDomain & "\" & strUserName &","&time
if strUserName = strLookfor then
Wscript.echo strUserName & "is logged into " & strComputer
end if
Next
End Function

Re: Get Currently logged in user

Posted: Fri May 25, 2007 12:56 pm
by winkelman
Did you perform a SNMP Walk and try to find the correct OID (if it exists)? That's usually the best way to search for specific SNMP info...

Re: Get Currently logged in user

Posted: Fri May 25, 2007 6:14 pm
by keith
Unfortunately I have no clue how to do snmp stuff. I have experimented with it in the dude but have had no success. Do you know of any good online tutorials on snmp?

Re: Get Currently logged in user

Posted: Fri May 25, 2007 6:23 pm
by winkelman
You can start here: http://en.wikipedia.org/wiki/Snmp

Basically, if you Google on SNMP, you will find a ton of stuff. Just take a day (or two, or three... :-)) to learn and play and you'll get started...

Also: if you want to query Windows machines you'd first have to install the SNMP Agent. It's provided with Windows, but not installed by default. Go to Add/Remove Programs, Windows Components and look for in there. You may also have to go into Windows Services (after installation of the Agent) to configure the SNMP Agent a little there.

Re: Get Currently logged in user

Posted: Fri May 25, 2007 7:36 pm
by keith
I will squeeze that into my schedule. I did however get it to work using a vb script instead. I saved the script as WHoison.vbs and added it as a tool. The code is below for anyone who is interested

DIM strComputer
DIM strUsername

strComputer=WScript.Arguments.Item(0)


'-------------------------- GET USERNAME CURRENTLY LOGGED IN ------------------------------------------------------------

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objItem In colItems
strUsername = objItem.UserName
Next

wscript.echo strUsername & " is currently logged on to this computer"

wscript.quit

Re: Get Currently logged in user

Posted: Tue May 29, 2007 10:38 am
by winkelman
I've checked and logged in user name(s) does not seem to be something query-able through SNMP. You can query for an array of users, but there's no way to find out which one(s) is/are logged in.

Of course, there are lot's of ways to expand on the default SNMP MIB...

Anyway, seems you did a good job creating the VB-script :).

Re: Get Currently logged in user

Posted: Thu May 31, 2007 1:46 am
by keith
Can't claim credit for that, all I did was copy from somebody else :)