在PowerShell中订阅对象的静态事件的语法是什么? [英] What is the syntax to subscribe to an object's static event in PowerShell?

查看:90
本文介绍了在PowerShell中订阅对象的静态事件的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Register-ObjectEvent 在所需的对象实例中查找对象实例参数 InputObject .对象的静态( Shared )事件的语法是什么?

Register-ObjectEvent looks for a object instance in the required parameter InputObject. What is the syntax for an object's static (Shared) event?

更新: TimeChanged :

$systemEvents = [Microsoft.Win32.SystemEvents]
$timeChanged = Register-ObjectEvent -InputObject $systemEvents
-EventName 'TimeChanged' -Action { Write-Host "Time changed" }

不幸的是, PowerShell ISE 中不会发信号通知SystemEvents.这是一个使用对象的staic事件的示例,该事件在任何地方都有效:

Unfortunately, the SystemEvents will not be signaled in PowerShell ISE. Here's a sample using an object's staic event that works everywhere:

$networkInformation = [System.Net.NetworkInformation.NetworkChange];
$networkAddressChanged = Register-ObjectEvent -InputObject $networkInformation 
-EventName 'NetworkAddressChanged' 
-Action { Write-Host "NetworkAddressChanged event signaled" }

推荐答案

如果将静态类型分配给变量,则可以订阅静态事件.

If you assign a static type to a variable, you can subscribe to static events.

例如:

$MyStaticType = [MyStaticNamespace.MyStaticClass]
Register-ObjectEvent -InputObject $MyStaticType -EventName MyStaticEvent -Action {Write-Host "Caught a static event"}

要查找某个类型可能具有的任何静态事件,可以将Get-Member与-Static开关一起使用

To find any static events a type may have, you can use Get-Member with the -Static switch

[MyStaticNamespace.MyStaticClass] | get-member -static -membertype event

在尝试访问[Microsoft.Win32.SystemEvents]事件时,我确实注意到,我需要在提升的提示符下运行(在Vista及更高版本上),才能访问消息.

I did notice when trying to access [Microsoft.Win32.SystemEvents] events, that I needed to be running in an elevated prompt (on Vista and above) in order to access the messages.

这篇关于在PowerShell中订阅对象的静态事件的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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