问题描述
尝试在PowerShell(版本6)中使用get-WmiObject
命令时收到以下错误:
I am getting the following error upon attempting to use the get-WmiObject
command in PowerShell (version 6):
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`
推荐答案
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
在PowerShell Core 中,所有将来的开发工作都将投入使用,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:
对于 为什么 CIM cmdlet是更好的选择(引自此TechNet博客帖子):
As for why the CIM cmdlets are the better choice (quoted from this TechNet blog post):
同一篇博客文章还描述了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.
但是,您仍然可以使用 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命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!