Powershell 6 的哪个库包含 get-wmiobject 命令? [英] What Library for Powershell 6 contains the get-wmiobject command?

查看:24
本文介绍了Powershell 6 的哪个库包含 get-wmiobject 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting the following error upon attempting to use the get-WmiObject command in PowerShell (version 6):

PS C:Userszsofi> 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`

推荐答案

Gert Jan Kraaijeveld 的有用回答 提供了一个解决方案真正仅在 Windows PowerShell 中可用的 cmdlet(不在 PowerShell [Core] 6+ 中).

Gert Jan Kraaijeveld's helpful answer offers a solution for cmdlets that truly are available only in Windows PowerShell (not also in PowerShell [Core] 6+).

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

In this particular case, however, as Lee_Daily notes in a comment, you can use the
Get-CimInstance cmdlet
, which is available in PowerShell [Core] 6+ too:

Get-CimInstance CIM_Product | Select-Object Name, PackageCache

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

Note the CIM_Product class name; CIM classes typically have the same properties as their WMI Win32_* counterparts.

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

In PowerShell Core, where all future development effort will go, the CIM cmdlets are your only option, but it is advisable to use the CIM (*-Cim*) cmdlets even in Windows PowerShell, because the WMI (*-Wmi*) cmdlets were deprecated in PowerShell version 3 (released in September 2012), when the CIM cmdlets were introduced; from the Get-CimInstance docs:

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

Starting in Windows PowerShell 3.0, this cmdlet has been superseded by Get-CimInstance.

至于 为什么 CIM cmdlet 是更好的选择(引自 这篇 TechNet 博客文章):

As for why the CIM cmdlets are the better choice (quoted from this TechNet blog post):

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

The big drawback to the WMI cmdlets is that they use DCOM to access remote machines. DCOM isn’t firewall friendly, can be blocked by networking equipment, and gives some arcane errors when things go wrong.

同一篇博文还描述了 CIM cmdlet 如何:

The same blog post also describes how the CIM cmdlets:

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

  • use the same standards-based remoting mechanism as PowerShell itself (WS-Management, via its Windows implementation, WinRM)

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

  • That is, computers that are set up for PowerShell remoting (see about_Remote_Requirements) implicitly support targeting via the CIM cmdlets.

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

However, you can still use the DCOM protocol (like the WMI cmdlets did) on an opt-in basis, using the New-CimSessionOption cmdlet.

支持会话

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

function slightly differently than their obsolete WMI counterparts in that the objects returned do not have methods directly; methods must be called via Invoke-CimMethod.

这篇关于Powershell 6 的哪个库包含 get-wmiobject 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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