I have a script that restarts a few services on server B, this script can be executed from server A. I need to execute this script when SQL has been stopped/started on Server C.
I've looked into WMI events, and that works if I add it on the SQL server (C), but I can't make a remote connection form that server to server A or B.
So I'm looking for a "trigger/event" on server A that triggers when SQL on a remote server has started/stopped.
I found that the __EventFilter
instance has a PScomputerName variable, but I can't change that to another name than the local machine.
This is the event/filter and consumer:
#Creating a new event filter
$instanceFilter = ([wmiclass]"\\.\root\subscription:__EventFilter").CreateInstance()
$instanceFilter.QueryLanguage = "WQL"
$instanceFilter.Query = "select * from __instanceModificationEvent within 5 where targetInstance isa 'win32_Service' and TargetInstance.Name='sql service'"
$instanceFilter.Name = "ServiceFilter"
#$instanceFilter.PSComputerName = "SQL server"
$instanceFilter.EventNamespace = 'root\cimv2'
$result = $instanceFilter.Put()
$newFilter = $result.Path
#Creating a new event consumer
$instanceConsumer = ([wmiclass]"\\.\root\subscription:ActiveScriptEventConsumer").CreateInstance()
$instanceConsumer.ScriptingEngine = "VBScript"
$instanceConsumer.ScriptFileName = "script.vbs"
$result = $instanceConsumer.Put()
$newConsumer = $result.Path
#Bind filter and consumer
$instanceBinding = ([wmiclass]"\\.\root\subscription:__FilterToConsumerBinding").CreateInstance()
$instanceBinding.Filter = $newFilter
$instanceBinding.Consumer = $newConsumer
$result = $instanceBinding.Put()
$newBinding = $result.Path