下面的脚本在PS5_ISE & CMD中工作。
#Just incase Event has been previously registered
Try {
Unregister-Event -SourceIdentifier 'disk' -Force -ErrorAction Stop
}
Catch {}
$REArgs = @{Query = "Select * from __InstanceCreationEvent within 1 where targetinstance isa 'win32_logicaldisk'"
SourceIdentifier = "disk"
Timeout = 1000
}
Register-WmiEvent @REArgs然而,当我试图在PS7.2.6中运行它时,我得到了以下内容:
PSv7>..\test\set-wmidisklistener.ps1
Register-WmiEvent: G:\BEKDocs\Scripts\test\Set-WMIDiskListener.ps1:11
Line |
11 | Register-WmiEvent @REArgs
| ~~~~~~~~~~~~~~~~~
| The term 'Register-WmiEvent' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of
| the name, or if a path was included, verify that the path is correct and try again.但是..。
PSv7>get-command Register*
CommandType Name Version Source
----------- ---- ------- ------
Function Register-ClusteredScheduledTask 1.0.0.0 ScheduledTasks
Function Register-DnsClient 1.0.0.0 DnsClient
Function Register-IscsiSession 1.0.0.0 iSCSI
Function Register-PSRepository 2.2.5 PowerShellGet
Function Register-PSRepository 2.2.5 PowerShellGet
Function Register-PSRepository 1.0.0.1 PowerShellGet
Function Register-ScheduledTask 1.0.0.0 ScheduledTasks
Function Register-StorageSubsystem 2.0.0.0 Storage
Cmdlet Register-ArgumentCompleter 7.2.6.500 Microsoft.PowerShell.Core
Cmdlet Register-CimIndicationEvent 7.0.0.0 CimCmdlets
Cmdlet Register-EngineEvent 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Register-ObjectEvent 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Register-PackageSource 1.4.7 PackageManagement
Cmdlet Register-PSSessionConfiguration 7.2.6.500 Microsoft.PowerShell.Core
Cmdlet Register-ScheduledJob 1.1.0.0 PSScheduledJob
Cmdlet Register-WmiEvent 3.1.0.0 Microsoft.PowerShell.Management
ExternalScript RegisterManifest.ps1 C:\Program Files\PowerShell\7\RegisterManifest.ps1
Application Register-CimProvider.exe 10.0.1904… C:\Windows\system32\Register-CimProvider.exe发布于 2022-08-19 02:14:16
标准建议适用于:
cmdlets (例如Get-CimInstance) 取代了 Windows (例如Get-WmiObject)在Windows v3中(2012年9月发布)。
因此, WMI应该避免使用,尤其是因为PowerShell (核心) 7+ --未来所有的努力都将去做--甚至不再有了。但是,请注意,WMI仍然是cmdlets的基础。有关更多信息,请参见这个答案。
cmdlets包含在CimCmdlets模块中。要列出它们,请运行
Get-Command -Module CimCmdlets
虽然WMI仍然是cmdlets的基础,但只有Windows的While与它们的CIM继承者之间存在着不同之处,它们的名称不同,特别是需要使用Invoke-CimMethod来调用方法。
从名字上看,寄存器-CimIndicationEvent 似乎是 注册-WmiEvent的继承者。
至于为什么您在get-command Register* in PowerShell (Core)的输出中看到了PowerShell:
Microsoft.PowerShell.Management模块)时,您才会看到它。
(Import-Module -UseWindowsPowerShell Microsoft.PowerShell.Management)- However, this is ill-advised, given the inherent limitations of this feature (also, said module contains many of PowerShell's core cmdlets, but no proxies are created for those that also exist natively in PowerShell (Core)) and the native availability of the CIM cmdlets.https://stackoverflow.com/questions/73411019
复制相似问题