KS-Soft. Network Management Solutions
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister    ProfileProfile    Log inLog in 

please help with script to find WHO is logged on

 
Post new topic   Reply to topic    KS-Soft Forum Index -> Configuration, Maintenance, Troubleshooting
View previous topic :: View next topic  
Author Message
menno



Joined: 21 May 2010
Posts: 157

PostPosted: Sat Sep 17, 2016 4:55 am    Post subject: please help with script to find WHO is logged on Reply with quote

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:


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
Back to top
View user's profile Send private message
KS-Soft Europe



Joined: 16 May 2006
Posts: 2832

PostPosted: Mon Sep 19, 2016 8:38 am    Post subject: Reply with quote

A assume you are trying to use Active Script test?

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

Code:
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%
Back to top
View user's profile Send private message Send e-mail Visit poster's website
KS-Soft



Joined: 03 Apr 2002
Posts: 12795
Location: USA

PostPosted: Mon Sep 19, 2016 8:48 am    Post subject: Reply with quote

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:
Quote:
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
Back to top
View user's profile Send private message Visit poster's website
menno



Joined: 21 May 2010
Posts: 157

PostPosted: Mon Sep 19, 2016 10:14 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
KS-Soft Europe



Joined: 16 May 2006
Posts: 2832

PostPosted: Mon Sep 19, 2016 11:48 am    Post subject: Reply with quote

This script should return usernames list (strRepl variable).
Is your HostMonitor started as application or service?
Back to top
View user's profile Send private message Send e-mail Visit poster's website
menno



Joined: 21 May 2010
Posts: 157

PostPosted: Mon Sep 19, 2016 1:35 pm    Post subject: Reply with quote

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

m
Back to top
View user's profile Send private message
menno



Joined: 21 May 2010
Posts: 157

PostPosted: Tue Sep 20, 2016 10:07 am    Post subject: Reply with quote

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:

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
Back to top
View user's profile Send private message
KS-Soft Europe



Joined: 16 May 2006
Posts: 2832

PostPosted: Tue Sep 20, 2016 4:13 pm    Post subject: Reply with quote

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

Could you try the following VB script:
Code:
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%
Back to top
View user's profile Send private message Send e-mail Visit poster's website
menno



Joined: 21 May 2010
Posts: 157

PostPosted: Wed Sep 21, 2016 12:03 am    Post subject: Reply with quote

Thanks .. this is working
Back to top
View user's profile Send private message
KS-Soft Europe



Joined: 16 May 2006
Posts: 2832

PostPosted: Wed Sep 21, 2016 7:31 am    Post subject: Reply with quote

You are welcome
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    KS-Soft Forum Index -> Configuration, Maintenance, Troubleshooting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group

KS-Soft Forum Index