Detect if Windows Service Exists (VBS)

Purpose: Return true if predefined Windows Service Exists, return false if it does not exist

You might find yourself in a situation where some of the applications have been installed in one way and the others in another way. Depending on the problem, you might just want to clean the table and reinstall the the applications with your definitions. If this is not possible or would not be cost efficient, you could use SCCM to check if “something” exists and alter the workstations where this something is missing. In this example this “something” is a missing Windows Service.

Usage: cscript.exe IT_APP_VBS_GetWindowServiceStatus WINDOWSSERVICENAME
Configuration: You need to insert your own WINDOWS SERVICE NAME as a parameter for the script.
Reference: http://themonitoringguy.com/scripts-code/monitoring-windows-services-vbscript/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
' *********************************************************************************
' ** Script Name: IT_APP_VBS_GetWindowServiceStatus
' ** Created by:  Cheyne (01-03-2011) / http://themonitoringguy.com
' ** Modified by: Jyri Lehtonen (28.06.2013) / http://it.peikkoluola.net
' **
' ** Purpose: Return true if predefined Windows Service Exists, return false if it does not exist
' ** Usage: IT_APP_VBS_GetWindowServiceStatus WINDOWSSERVICENAME
' **
' ** License: This program is free software: you can redistribute it and/or modify
' ** it under the terms of the GNU General Public License as published by
' ** the Free Software Foundation, either version 3 of the License, or
' ** (at your option) any later version.
' **
' ** This program is distributed in the hope that it will be useful,
' ** but WITHOUT ANY WARRANTY; without even the implied warranty of
' ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' ** GNU General Public License for more details.
' *********************************************************************************
 
'Declare Variables
Dim objWMIService, objProcess, colProcess, Status, strComputer, strService
 
'Assign Arguments
strComputer = "."
strService = WScript.Arguments(0) 
Status= false
 
'Check For Arguments - Quit If None Found
If Len(strService) < 1 Then
    Wscript.echo "No Arguments Entered - Exiting Script"
    WScript.Quit
End If
 
'Setup WMI Objects
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("SELECT DisplayName, Status, State FROM Win32_Service WHERE DisplayName = '" & strService & "'")
 
'Check For Running Service
For Each objProcess in colProcess
    If InStr(objProcess.DisplayName,strService) > 0 And objProcess.State = "Running" Then
    Status = true
    End If
Next
 
If Status = true Then
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Running"
    'Perform Some Pass Logic Here
    WScript.quit 0
Else
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Not Running"
    'Perform Some Failed Logic Here
    WScript.quit 1
End If
' *********************************************************************************
' ** Script Name: IT_APP_VBS_GetWindowServiceStatus
' ** Created by:  Cheyne (01-03-2011) / http://themonitoringguy.com
' ** Modified by: Jyri Lehtonen (28.06.2013) / http://it.peikkoluola.net
' **
' ** Purpose: Return true if predefined Windows Service Exists, return false if it does not exist
' ** Usage: IT_APP_VBS_GetWindowServiceStatus WINDOWSSERVICENAME
' **
' ** License: This program is free software: you can redistribute it and/or modify
' ** it under the terms of the GNU General Public License as published by
' ** the Free Software Foundation, either version 3 of the License, or
' ** (at your option) any later version.
' **
' ** This program is distributed in the hope that it will be useful,
' ** but WITHOUT ANY WARRANTY; without even the implied warranty of
' ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' ** GNU General Public License for more details.
' *********************************************************************************

'Declare Variables
Dim objWMIService, objProcess, colProcess, Status, strComputer, strService
 
'Assign Arguments
strComputer = "."
strService = WScript.Arguments(0) 
Status= false
 
'Check For Arguments - Quit If None Found
If Len(strService) < 1 Then
    Wscript.echo "No Arguments Entered - Exiting Script"
    WScript.Quit
End If
 
'Setup WMI Objects
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("SELECT DisplayName, Status, State FROM Win32_Service WHERE DisplayName = '" & strService & "'")
 
'Check For Running Service
For Each objProcess in colProcess
    If InStr(objProcess.DisplayName,strService) > 0 And objProcess.State = "Running" Then
	Status = true
    End If
Next
 
If Status = true Then
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Running"
    'Perform Some Pass Logic Here
	WScript.quit 0
Else
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Not Running"
    'Perform Some Failed Logic Here
	WScript.quit 1
End If

Leave a Reply

Your email address will not be published. Required fields are marked *


six − two =

Post Navigation