Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've had CPU spikes when listening to WMI events in cases where I have failed to detach properly from my events on exit/cleanup. You might want to check you are not "leaking" WMI event subscriptions. Just in case detach from the event as early as possible and make sure you always do it.</p> <p>To illustrate further, here's an example from <a href="http://rads.stackoverflow.com/amzn/click/1590599403" rel="nofollow noreferrer">my PowerShell book</a> that listens to WMI events using the PSEventing library:</p> <blockquote> <p>Add-PSSnapin PSEventing -ErrorAction SilentlyContinue</p> <p>$queryString = @' SELECT * FROM __InstanceModificationEvent WITHIN 10 WHERE TargetInstance ISA 'Win32_Service' AND TargetInstance.Name = 'w3svc' AND TargetInstance.State = 'Stopped' '@</p> <p>$query = New-Object System.Management.WQLEventQuery ` -argumentList $queryString </p> <p>$watcher = New-Object System.Management.ManagementEventWatcher($query)</p> <p>Connect-EventListener watcher EventArrived </p> <p>$watcher.Start()</p> <p>echo "Waiting for the W3CSVC service to stop..." Get-Event -wait | <code> foreach {</code> Write-Host -foreground Red "The W3SVC service has stopped!" }</p> <p>$watcher.Stop() </p> <p>Disconnect-EventListener watcher EventArrived</p> <p>echo "done"</p> </blockquote> <p>If I do not do the <strong>Disconnect-EventListener</strong> bit upon script exit, I get CPU spikes the third or fourth time I attach to the event. My guess is that the system still tries to deliver events.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload