【问题标题】:What Library for Powershell 6 contains the get-wmiobject command?Powershell 6 的哪个库包含 get-wmiobject 命令?
【发布时间】:2019-02-02 16:27:40
【问题描述】:

尝试在 PowerShell(版本 6)中使用 get-WmiObject 命令时出现以下错误:

PS C:\Users\zsofi> Get-WmiObject Win32_product | select name, packagecache

Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-WmiObject Win32_product | select name, packagecache
+ ~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException`

【问题讨论】:

  • 根据 MSDocs 网站The requested page is not available for PowerShell 6. You have been redirected to the newest product version this page is available for. ...并且此时“最高版本”是 5.1。据我所知,您应该能够使用Get-CimInstance cmdlet - Get-CimInstance (CimCmdlets) | Microsoft Docs — docs.microsoft.com/en-us/powershell/module/cimcmdlets/…

标签: powershell wmi powershell-core cim


【解决方案1】:

Gert Jan Kraaijeveld's helpful answer 为真正仅在 Windows PowerShell 中可用(在 PowerShell [Core] 6+ 中也不可用)提供的 cmdlet 解决方案。

然而,在这种特殊情况下,正如 Lee_Daily 在评论中指出的那样,您可以使用
Get-CimInstance cmdlet
在PowerShell [Core] 6+ 也是

Get-CimInstance CIM_Product | Select-Object Name, PackageCache

注意CIM_Product 类名; CIM 类通常具有与其对应的 WMI Win32_* 相同的属性。

为什么通常应该使用 CIM cmdlet 而不是 WMI cmdlet:

在 PowerShell Core 中,所有未来的开发工作都将用于,CIM cmdlet 是您唯一的选择,但建议使用 CIM (*-Cim*) cmdlet,即使在 Windows PowerShell,因为 WMI (*-Wmi*) cmdlet 在 PowerShell 版本 3(2012 年 9 月发布)中已弃用,当引入了 CIM cmdlet;来自Get-CimInstance docs

从 Windows PowerShell 3.0 开始,此 cmdlet 已被 Get-CimInstance 取代。

至于为什么 CIM cmdlet 是更好的选择(引自this TechNet blog post):

WMI cmdlet 的最大缺点是它们使用 DCOM 来访问远程计算机。 DCOM 对防火墙不友好,可能会被网络设备阻止,并且在出现问题时会出现一些神秘错误。

同一篇博文还介绍了 CIM cmdlet 如何:

  • 使用与 PowerShell 本身相同的基于标准的远程处理机制WS-Management,通过其 Windows 实现,WinRM

    • 也就是说,为 PowerShell 远程处理设置的计算机(请参阅 about_Remote_Requirements)隐式支持通过 CIM cmdlet 进行定位。

    • 但是,您仍然可以使用 New-CimSessionOption cmdlet 在选择加入的基础上使用 DCOM 协议(就像 WMI cmdlet 所做的那样)。

  • 支持会话

  • 功能与过时的 WMI 对应物略有不同,因为返回的对象没有直接的方法; 方法必须通过Invoke-CimMethod调用。

【讨论】:

    【解决方案2】:

    据我所知,唯一的方法是兼容性模块。这是 Microsoft 提供的一个非常简洁的模块,它实际上通过隐式远程处理到同一台机器上的 Windows Powershell 5.1 会话使 Windows PS cmdlet 在 PS Core 中可用。 https://github.com/PowerShell/WindowsCompatibility

    【讨论】:

    猜你喜欢
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2019-06-15
    相关资源
    最近更新 更多