首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >注册-WMIEvent在PS5中工作,而不是PS7?

注册-WMIEvent在PS5中工作,而不是PS7?
EN

Stack Overflow用户
提问于 2022-08-19 01:36:43
回答 1查看 64关注 0票数 1

下面的脚本在PS5_ISE & CMD中工作。

代码语言:javascript
复制
#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中运行它时,我得到了以下内容:

代码语言:javascript
复制
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.

但是..。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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:

  • 默认情况下,您不会看到这个。
  • 只有当您显式地选择使用WMI加载PowerShell模块(包含WMI的Microsoft.PowerShell.Management模块)时,您才会看到它。 (Import-Module -UseWindowsPowerShell Microsoft.PowerShell.Management)
代码语言:javascript
复制
- 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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73411019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档