please help with script to find WHO is logged on

All questions related to installations, configurations and maintenance of Advanced Host Monitor (including additional tools such as RMA for Windows, RMA Manager, Web Servie, RCC).
Post Reply
menno
Posts: 158
Joined: Fri May 21, 2010 1:27 am

please help with script to find WHO is logged on

Post by menno »

Hi HM team
I have problems running a script that finds out WHO ( usernames ) is logged on to the system ( remote desktop services ) on Windows 2012 R2

i just want to have a reply that shows the usernames
for example reply : John,Joe,Crista,Madeline

can you please help me with this script ?
UPDATE:

i made some progress but still not working
this is my current script , but not working ...

Code: Select all


Option Explicit 

const statusAlive       = "Host is alive:" 
const statusDead        = "No answer:" 
const statusUnknown     = "Unknown:" 
const statusNotResolved = "Unknown host:" 
const statusOk          = "Ok:" 
const statusBad         = "Bad:" 
const statusBadContents = "Bad contents:" 

Dim objWMIService, objComputer, colComputer 
Dim strLogonUser, strLogonUser1, strComputer 
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem") 
For Each objComputer in colComputer 
If not objComputer.UserName = "" Then 
strLogonUser = Split(objComputer.UserName,"\") 
strLogonUser(1) = UCase(Left(strLogonUser(1),1))& Trim(Mid(strLogonUser(1),2,20)) 
WScript.StdOut.Write strLogonUser(1) & "," 
End If 
Next 
Error in script:
Error number :500
Source :Microsoft VBScript runtime error
Description :Variable is undefined: 'WScript'
Text :
Line :21
Column :0


Many thanks in advance
Menno
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

A assume you are trying to use Active Script test?

Please use Shell Script test instead: http://www.ks-soft.net/hostmon.eng/mfra ... m#chkShell
Script may look like the following:

Code: Select all

Option Explicit
const statusUnknown     = "scriptRes:Unknown:"
const statusOk          = "scriptRes:Ok:"
const statusBad         = "scriptRes:Bad:"

Dim objWMIService, objComputer, colComputer
Dim strLogonUser, strLogonUser1, strComputer, strRepl
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
strRepl = ""
For Each objComputer in colComputer
If not objComputer.UserName = "" Then
strLogonUser = Split(objComputer.UserName,"\")
strLogonUser(1) = UCase(Left(strLogonUser(1),1))& Trim(Mid(strLogonUser(1),2,20))
strRepl = strRepl & strLogonUser(1) & ","
End If 
Next
WScript.StdOut.Write statusOk&strRepl
Start cmd: cmd /c cscript /B /E:VBScript %Script% %Params%
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

What test method do you use?
Error message looks like you are using Active Script test method.
While script looks like script for Shell Script test method.

If you want to use Active Script, please check the manual:
Active Script
Requirements to the script:
1. script must contain "performtest" function. HM always call function with this name
2. "performtest" function must not have any parameters
3. "performtest" function must return string value. This string contains two parts separated by colon (:). First obligatory part contains test status, it can take following values:
"Host is alive"
"No answer"
"Unknown"
"Unknown host"
"Ok"
"Bad"
"Bad contents"
Second optional part contains "Reply" value, HostMonitor displays this value in Reply field, writes to log files, uses to displays charts (Log Analyzer), etc.
E.g.
performtest = "Ok:300 Kb"
performtest = "Bad:90 %"
I would recommend Shell Script test method instead

Regards
Alex
menno
Posts: 158
Joined: Fri May 21, 2010 1:27 am

Post by menno »

thanks for the info
i now use a shell script
the script you provided is working but ...
it gives a OK back.

i just want to see a reply like : john,joe,madaline,peter
( so these names must be in the reply field )
these are the people who are logged into the system via remote desktop services.

so .. the status is not that important ( like Ok or bad )

i hope you understand my question
Many many thanks in advnce
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

This script should return usernames list (strRepl variable).
Is your HostMonitor started as application or service?
menno
Posts: 158
Joined: Fri May 21, 2010 1:27 am

Post by menno »

our Hostmonitor is started as a normal application on a dedicated VM machine

m
menno
Posts: 158
Joined: Fri May 21, 2010 1:27 am

Post by menno »

mmm did everything exactly as you told me be still no users in the reply field...

it just giving me the status OK but no users are displayed in the reply field

can you help me out here ?

thanks m

start cmd : cmd /c cscript /B /E:VBScript %Script% %Params%

Code: Select all

Option Explicit 
const statusUnknown     = "scriptRes:Unknown:" 
const statusOk          = "scriptRes:Ok:" 
const statusBad         = "scriptRes:Bad:" 

Dim objWMIService, objComputer, colComputer 
Dim strLogonUser, strLogonUser1, strComputer, strRepl 
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem") 
strRepl = "" 
For Each objComputer in colComputer 
If not objComputer.UserName = "" Then 
strLogonUser = Split(objComputer.UserName,"\") 
strLogonUser(1) = UCase(Left(strLogonUser(1),1))& Trim(Mid(strLogonUser(1),2,20)) 
strRepl = strRepl & strLogonUser(1) & "," 
End If 
Next 
WScript.StdOut.Write statusOk&strRepl
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Different WMI class should be used in the script in order to detect Active user sessions.

Could you try the following VB script:

Code: Select all

Option Explicit
const statusUnknown     = "scriptRes:Unknown:"
const statusOk          = "scriptRes:Ok:"
const statusBad         = "scriptRes:Bad:"

Dim objWMI, colSessions, objSession, colList, objItem
Dim strComputer, strUserList, strNameOfUser, strUserDomain

strComputer = "."
Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colSessions = objWMI.ExecQuery("Select * from Win32_Process Where Name='explorer.exe'")

For Each objSession in colSessions
  objSession.GetOwner strNameOfUser, strUserDomain
  If strUserList <> "" Then strUserList = strUserList & ","
  strUserList = strUserList & strNameOfUser

Next

WScript.StdOut.Write statusOk & strUserList
Start cmd: cmd /c cscript /B /E:VBScript %Script% %Params%
menno
Posts: 158
Joined: Fri May 21, 2010 1:27 am

Post by menno »

Thanks .. this is working
:D :D :D :D
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

You are welcome :D
Post Reply