0
4

2 回答 2

2

这是我使用备用查询对任务的看法,注册 Win32_Service 修改事件,其中 Win32_Service.State 值为“已停止”。我使用 $Event.SourceEventArgs.NewEvent.TargetInstance.Name 来获取服务名称:

function Stop-MyService($SystemName)
{
    $ControllerSvc = $SystemName

    $query = "SELECT * FROM __InstanceModificationEvent WITHIN 2 " +
             "WHERE TargetInstance Isa 'Win32_Service' " +
             "AND TargetINstance.Name = '" + $ControllerSvc + "' " +
             "AND TargetInstance.State = 'Stopped'"

    $action = 
    { 
        Write-Host $Event.SourceEventArgs.NewEvent.TargetInstance.Name," stopped "
        Unregister-Event -SourceIdentifier "ControllerSvcEvent"    
    }

    Register-WMIEvent -query $query -SourceIdentifier "ControllerSvcEvent" -action $action
}

Stop-MyService 'MSSQL$SQLEXPRESS'

我使用 SQL Server Express 进行测试。

于 2012-10-16T15:06:19.643 回答
0

尝试$ControllerSvc通过-MessageData参数of Register-WMIEvent,那么它应该可以通过$Event,根据这篇文章

于 2012-10-16T14:30:15.600 回答