Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

Problem mit Register-WmiEvent

$
0
0

Hi,

i have problems by executing the following script:

$wmiParameters = @{
    Query = "SELECT * FROM __InstanceModificationEvent WITHIN 1 where TargetInstance ISA 'Win32_Service'";
    sourceIdentifier = "Dienst:Status:Aktion"
}

$actions = {
    [console]::Beep(440,10)
    $dienst = $eventArgs.NewEvent.TargetInstance
    Write-Host -ForegroundColor Yellow
    Write-Host "Dienst " $dienst.name ": Der Status hat sich geändert. Status ist nun: " $dienst.State "!" -ForegroundColor Yellow
    if($dienst.State -eq 'Stopped')
    {
        Write-Host "Neustart des Dienstes ..." -ForegroundColor Yellow
        Start-Service $dienst.name $wmiQuery
    }
}


Register-WmiEvent $wmiParameters -Action $actions

# Register-WmiEvent -query "SELECT * FROM __InstanceModificationEvent WITHIN 1 where TargetInstance ISA 'Win32_Service'" -sourceIdentifier "Dienst:Status:Aktion" -action $Actions

It produces the following error:

Register-WmiEvent : Es konnte keine Registrierung für das Ereignis vorgenommen werden. Der Klassenname ist ungültig. Gültige Klassennamen enthalten Buchstaben, Ziffern und Unterstriche. In C:\Users\Administrator\Scripts\eventWatch2.ps1:20 Zeichen:1 + Register-WmiEvent $wmiParameters -Action $actions+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (:) [Register-WmiEvent], ArgumentException+ FullyQualifiedErrorId : INVALID_QUERY_IDENTIFIER,Microsoft.PowerShell.Commands.RegisterWmiEventComman

The script works if i use the commented last line to execute it. So i think the error can't be the classname 'win32_service'. I think there is a problem with the $wmiParameters.

Best regards

Michael




Viewing all articles
Browse latest Browse all 2314


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>