Unregister-Event 不删除孤立事件订阅 [英] Unregister-Event not removing orphaned event subscriptions

查看:25
本文介绍了Unregister-Event 不删除孤立事件订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用它来订阅特定计算机的关机事件:

I'm using this to subscribe to shutdown events for a particular computer:

Register-WMIEvent -ComputerName MyServer1 -Query "SELECT * FROM Win32_ComputerShutdownEvent" -SourceIdentifier "ShutdownWatcher" -Action { 
    Write-Host "MyServer1 has been shutdown"
}

这可以找到并在计算机关闭时收到关闭文本.

This works find and I receive the shutdown text when the computer is shut down.

问题是,一旦事件触发,如果我尝试使用以下内容删除事件订阅,则不会发生任何事情并且订阅仍然存在.

The problem is that once the event has fired if I try to use the following to remove the event subscription nothing happens and the subscription remains.

Unregister-Event -SourceIdentifier "ShutdownWatcher"

就好像事件订阅是孤立的,现在无法删除.它是孤立的并且完全没用,因为在执行另一次关机时它不会触发.一旦订阅被触发,我想删除它.

It's as though the event subscription is orphaned and now can't be removed. It's orphaned and completely useless as it doesn't fire when another shutdown is performed. I'd like to remove the subscription once it has fired.

有人知道如何删除孤立事件订阅吗?一种方法是关闭 Powershell 会话,但我真的不想这样做.

Anyone know how to remove orphaned event subscriptions ? One way is to shutdown the Powershell session but I don't really want to do that.

推荐答案

我在使用计时器时遇到了同样的问题.我不知何故在我的 PowerShell 会话中留下了一个具有相同 SourceIdentifier 名称的计时器.每当我等待 10 秒计时器时,它总是会立即返回.

I had the same problem with a timer. I had somehow left a timer with the same SourceIdentifier name in my PowerShell session. Whenever I would wait for my 10 second timer, it would always return immediately.

我保存了等待事件的输出并且可以访问事件标识符,这允许我通过事件标识符删除事件.如果使用 SourceIdentifier,则无法访问孤立事件.我可以通过检查旧的 Wait-Event.TimeGenerated 的输出来判断它是孤立的.您将需要能够通过唯一的 EventIdentifier 访问孤立事件.

I saved the output of the Wait-Event and could access the EventIdentifier, which allowed me to Remove-Event by the EventIdentifier. If I used the SourceIdentifier, I could not access the orphaned event. I could tell it was orphaned by examining the output of my Wait-Event.TimeGenerated, which was old. You will need to be able to access the orphaned event by the EventIdentifier which is unique.

$eventDone = "Done"
$job2 = Register-ObjectEvent -InputObject $timer2 -EventName Elapsed -SourceIdentifier $eventDone

$timer2.Interval = 10000;
$timer2.AutoReset = $false;
$timer2.Enabled = $true;

$waitEvent = Wait-Event -SourceIdentifier $eventDone;
# Look at the $waitEvent.TimeGenerated for a clue it is old
# use the $waitEvent.EventIdentifier to access the event and remove it
$orphanEventID = $waitEvent.EventIdentifier
Remove-Event -EventIdentifier $orphanEventID

这篇关于Unregister-Event 不删除孤立事件订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆