CheckExternalScripts: check_users_loggedin.vbs

File check_users_loggedin.vbs, 2.8 KB (added by mike2k, 5 months ago)
Line 
1'########################################################################
2'# Name: Michel van Son (michelvs@phison.nl)                                                    #
3'# Company: Phison Technologies                                                                                 #
4'# Date: 12-02-2010                                                                                                             #
5'# Purpose: External check script to get number of users logged in              #
6'# Changes: [dd-mm-yyyy] Name | changes                                                                 #
7'########################################################################
8
9'################################################################################################################
10'# Without arguments, the script will only return the number of users logged in with exit code 0                                #
11'# When supplying arguments, the first argument is the warning treshold and the second is the critical treshold #
12'# More or less then 2 arguments will result in an error with status Unknown(3)                                                                 #
13'################################################################################################################
14
15Option Explicit
16
17'Declare variables.
18Dim valWarning
19Dim valCritical
20Dim objWMIService
21Dim objUsers
22Dim valUsers
23
24
25'Check for arguments.
26if Wscript.Arguments.Count > 0 then
27        if Wscript.Arguments.Count > 2 then
28                Wscript.Echo "Too many arguments given. Please enter warning and critical tresholds."
29                Wscript.Quit(3)
30        elseif Wscript.Arguments.Count < 2 then
31                Wscript.Echo "Too few arguments given. Please enter warning and critical tresholds."
32                Wscript.Quit(3)
33        else
34                valWarning = Wscript.Arguments.Item(0)
35                valCritical = Wscript.Arguments.Item(1)
36                valWarning = CInt(valWarning)
37                valCritical = CInt(valCritical)
38        end if
39end if
40
41'Query the WMI service for the users.
42'Use 2 WHERE clauses for Interactive(2) and RemoteInteractive(10) logons.
43'More information: http://msdn.microsoft.com/en-us/library/aa394189%28VS.85%29.aspx
44Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\localhost\root\cimv2")
45Set objUsers = objWMIService.ExecQuery ("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")
46valUsers = objUsers.Count
47valUsers = CInt(valUsers)
48
49'Check the user count, echo it and exit with proper exitcode.
50if valUsers > 0 then
51        if Not IsEmpty(valCritical) then
52                if valUsers >= valCritical then
53                        Wscript.Echo "Critical: Users logged in: " & valUsers & "|'users'=" & valUsers & ";" & valWarning & ";" & valCritical
54                        Wscript.Quit(2)
55                elseif valUsers >= valWarning then
56                        Wscript.Echo "Warning: Users logged in: " & valUsers & "|'users'=" & valUsers & ";" & valWarning & ";" & valCritical
57                        Wscript.Quit(1)
58                else
59                        Wscript.Echo "OK: Users logged in: " & valUsers & "|'users'=" & valUsers & ";" & valWarning & ";" & valCritical
60                        Wscript.Quit(0)
61                end if
62        else
63                Wscript.Echo "OK: Users logged in: " & valUsers & "|'users'=" & valUsers
64                Wscript.Quit(0)
65        end if
66else
67        Wscript.Echo "OK: No users logged in.|'users'=" & valUsers
68        Wscript.Quit(0)
69end if