尝试在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`
Gert Jan Kraaijeveld's helpful answer为cmdlet提供了解决方案,这些cmdlet仅在Windows PowerShell中才可用(在PowerShell Core中也不可用)。
但是,在这种情况下,正如Lee_Daily在评论中指出的,您可以使用Get-CimInstance
cmdlet,该< 在PowerShell Core中也可用(v6 +)] >:Get-CimInstance
为什么通常应使用CIM cmdlet代替WMI cmdlet:注意
Get-CimInstance CIM_Product | Select-Object Name, PackageCache
类名称; CIM类通常具有与其WMICIM_Product
对应物相同的属性。
Core中,所有未来的开发工作都将投入使用,CIM cmdlet是您唯一的选择,但是即使在Windows PowerShell]中也建议使用CIM(Win32_*
)cmdlet。 ,因为WMI(*-Cim*
)cmdlet在PowerShell版本3中] [[已弃用(2012年9月发布),因此在引入CIM cmdlet时;从*-Wmi*
:从Windows PowerShell 3.0开始,此cmdlet已由Get-CimInstance
docs取代。
关于WMI cmdlet的最大缺点是它们使用DCOM访问远程计算机。 DCOM对防火墙不友好,可能会被网络设备阻止,并且在出现问题时会出现一些不可思议的错误。为什么
CIM cmdlet是更好的选择(引自Get-CimInstance
):
同一篇博客文章还描述了CIM cmdlet的方式:([C0,通过其Windows实现,
- 使用与PowerShell本身相同的
基于标准的远程处理
机制
Get-CimInstance
)]具有方法必须通过this TechNet blog post
。