I hate having to reinvent the wheel, and eventually I might get all of my code snip-its into GitHub, until then, here it is. Why use VBScript over PowerShell? In my use case, logon scripts. Specifically for performance in relation to using Ivanti’s Environment Manager (formerly AppSense EM). There was an interesting test done, which can be found here: https://helgeklein.com/blog/2015/09/performance-footprint-of-powershell-logon-scripts/
For the purpose of this post, I am not going to really dive much into the why, etc. This is simply so I can come back and copy\paste\modify the code as I need it. There is also some detailed information on the service states here: https://msdn.microsoft.com/en-us/library/windows/desktop/ee126211%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
This script is intended for condition logic in AppSense, see here: https://help.ivanti.com/ap/help/en_US/em/10.1/Content/Environment_Manager/Custom_and_Execute_Actions.htm
'CODE PROVIDED AS IS BY ITTECHSMITH.COM 'USE AT YOUR OWN RISK, ETC. 'EXIT CODE Variable eCode = 0 'Default Condition\Exit Code is True. Modify as needed in your logic below. 'ENTER THE SERVICE NAME YOU ARE LOOKING FOR serviceName = "..." Set wmi = GetObject("winmgmts://./root/cimv2") state = wmi.Get("Win32_Service.Name='" & serviceName & "'").State 'PERFORM SOME ACTION DEPENDING ON SERVICE STATE If state = "Stopped" Then WScript.Echo state eCode = 0 ElseIf state = "Start Pending" Then WScript.Echo state WScript.Echo "Give it some more time to start." eCode = 0 ElseIf state = "Stop Pending" Then WScript.Echo state WScript.Echo "Give it some more time to stop." eCode = 0 ElseIf state = "Running" Then WScript.Echo state eCode = 0 ElseIf state = "Contiue Pending" Then WScript.Echo state WScript.Echo "Give it some more time..." eCode = 0 ElseIf state = "Pause Pending" Then WScript.Echo state WScript.Echo "Give it some more time..." eCode = 0 ElseIf state = "Paused" Then WScript.Echo state eCode = 0 ElseIf state = "Unknown" Then WScript.Echo state eCode = 0 Else WScript.Echo "Now you did it... nothing works." eCode = 1 End If WScript.Quit eCode