Skip to content

Instantly share code, notes, and snippets.

@regeter
Created September 11, 2012 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save regeter/3699265 to your computer and use it in GitHub Desktop.
Save regeter/3699265 to your computer and use it in GitHub Desktop.
Script that installs WMI Event consumer to restart a windows service on resume
' Reto Egeter, fullparam.wordpress.com, Feb 2012
' Streamlined process. Installs correctly for x86, x64 and sandboxed 6432WOW.
Set wShe = CreateObject("WScript.Shell")
DebugMode = True
'No Security Warnings
set oEnv = wShe.Environment("PROCESS")
oEnv("SEE_MASK_NOZONECHECKS") = 1 'This prevent Security Zone checking for the period of execution.
Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
objCtx.Add "__ProviderArchitecture", 64 'Load 64bit version of WMI if available.
Set objLocator = CreateObject("Wbemscripting.SWbemLocator")
'Run Script in 64Bit if 64Bit OS detected.
If oEnv("PROCESSOR_ARCHITECTURE") = "x86" And oEnv("PROCESSOR_ARCHITEW6432") <> "" Then
Set objServices = objLocator.ConnectServer("","root\cimv2","","",,,,objCtx)
Set objShare = objServices.Get("Win32_Process")
Set objInParam = objShare.Methods_("Create").inParameters.SpawnInstance_()
'Add the input parameters.
objInParam.Properties_.Item("CommandLine") = "WScript.exe " & WScript.ScriptFullName
objInParam.Properties_.Item("CurrentDirectory") = Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\")-1)
'Execute the method and obtain the return status.
'The OutParameters object in objOutParams is created by the provider.
Set objOutParams = objServices.ExecMethod("Win32_Process", "Create", objInParam)
'List OutParams
If debugmode = "True" Then
Select Case objOutParams.ReturnValue
Case 0 WScript.Echo "Command Executed"
Case 2 WScript.Echo "Access denied"
Case 3 WScript.Echo "Insufficient privilege"
Case 9 WScript.Echo "Path not found"
Case 21 WScript.Echo "Invalid parameter"
Case Else WScript.Echo "Unknown failure"
End Select
End If
WScript.Quit(0)
End If
'Ensure Running as SYSTEM
Set WshNetwork = WScript.CreateObject("WScript.Network")
If WshNetwork.UserName <> "SYSTEM" Then
If DebugMode = True Then WScript.Echo "Current User Name: """ & WshNetwork.UserName & """ This needs to run as SYSTEM."
wShe.LogEvent 4, Now & " Installing of the 1E WMI resume script (SYSTEM user) on this machine failed, install script was not run as SYSTEM"
WScript.Quit (1)
End If
Set objServices = objLocator.ConnectServer("","root\subscription","","",,,,objCtx)
'Create new ActiveScriptEventConsumer
Set newActiveScriptEventConsumer = objServices.Get("ActiveScriptEventConsumer").SpawnInstance_
newActiveScriptEventConsumer.Name = "RestartService"
newActiveScriptEventConsumer.ScriptingEngine = "VBScript"
newActiveScriptEventConsumer.ScriptText = "' Reto Egeter, Feb 2012" & vbNewLine & ""&_
"' Script that will stop and start the wireless service. This will write an event into the application log" & vbNewLine & ""&_
"" & vbNewLine & ""&_
"strComputer = ""."" " & vbNewLine & ""&_
"Set objWMIService = GetObject(""winmgmts:"" & ""{impersonationLevel=impersonate}!\\"" & strComputer & ""\root\cimv2"") " & vbNewLine & ""&_
"" & vbNewLine & ""&_
"StopService(""WZCSVC"")" & vbNewLine & ""&_
"QueryServiceStopped(""WZCSVC"")'Waiting until service is stopped." & vbNewLine & ""&_
"StartService(""WZCSVC"") " & vbNewLine & ""&_
"" & vbNewLine & ""&_
"" & vbNewLine & ""&_
"Function StartService(SrvName) " & vbNewLine & ""&_
" Dim colServiceList, objService " & vbNewLine & ""&_
" Set colServiceList = objWMIService.ExecQuery(""Select * from Win32_Service where Name='"" & srvName & ""'"") " & vbNewLine & ""&_
" For Each objService in colServiceList " & vbNewLine & ""&_
" errReturn = objService.Change( , , , , ""Automatic"") 'Enabling Autostart." & vbNewLine & ""&_
" StartService = objService.StartService() " & vbNewLine & ""&_
" Next " & vbNewLine & ""&_
"End Function " & vbNewLine & ""&_
"" & vbNewLine & ""&_
"Function StopService(SrvName) " & vbNewLine & ""&_
" Dim colServiceList, objService " & vbNewLine & ""&_
" Set colServiceList = objWMIService.ExecQuery(""Select * from Win32_Service where Name='"" & srvName & ""'"") " & vbNewLine & ""&_
" For Each objService in colServiceList " & vbNewLine & ""&_
" StopService = objService.StopService() " & vbNewLine & ""&_
" Next " & vbNewLine & ""&_
"End Function " & vbNewLine & ""&_
"" & vbNewLine & ""&_
"Function QueryServiceStopped(SrvName) 'Until it is not stopping" & vbNewLine & ""&_
" Dim colSrvList, objSrvc " & vbNewLine & ""&_
" Set colSrvList = objWMIService.ExecQuery(""Select * from Win32_Service where Name='"" & srvName & ""'"") " & vbNewLine & ""&_
" For Each objSrvc in colSrvList " & vbNewLine & ""&_
" If objSrvc.InterrogateService() = 4 Then '4 means stopping" & vbNewLine & ""&_
" QueryServiceStopped(SrvName) 'Recursive Function" & vbNewLine & ""&_
" End If" & vbNewLine & ""&_
" Next " & vbNewLine & ""&_
"End Function "
Set newActiveScriptEventConsumerObj = newActiveScriptEventConsumer.Put_ ' Save the new ActiveScriptEventConsumer
wShe.LogEvent 4, Now & " Installing of the FullParam WMI resume script (SYSTEM user): Created ActiveScriptEventConsumer"
'Create new EventFilter
Set newEventFilter = objServices.Get("__EventFilter").SpawnInstance_
newEventFilter.EventNamespace = "root\cimv2"
newEventFilter.Name = "Resume"
newEventFilter.Query = "Select * from Win32_PowerManagementEvent WITHIN 1 WHERE EventType = 7 " 'Check Every Second for Power Event Resume
newEventFilter.QueryLanguage = "WQL"
Set newEventFilterObj = newEventFilter.Put_ ' Save the new EventFilter
wShe.LogEvent 4, Now & " Installing of the 1E WMI resume script (SYSTEM user): Created __EventFilter"
'Create new FilterToConsumerBinding
Set newFilterToConsumerBinding = objServices.Get("__FilterToConsumerBinding").SpawnInstance_
newFilterToConsumerBinding.Filter = newEventFilterObj
newFilterToConsumerBinding.Consumer = newActiveScriptEventConsumerObj
Set newFilterToConsumerBindingObj = newFilterToConsumerBinding.Put_ ' Save the new FilterToConsumerBinding
wShe.LogEvent 4, Now & " Installing of the 1E WMI resume script (SYSTEM user): Created __FilterToConsumerBinding"
wShe.LogEvent 4, Now & " Installing of the 1E WMI resume script (SYSTEM user): Finished Installing the script on this machine"
oEnv.Remove("SEE_MASK_NOZONECHECKS")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment