尝试get-WmiObject在 PowerShell(版本 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`
Run Code Online (Sandbox Code Playgroud) 我试图执行一些简单的if语句,但所有基于[Microsoft.Management.Infrastructure.CimInstance]的新cmdlet似乎都没有公开.count方法?
$Disks = Get-Disk
$Disks.Count
Run Code Online (Sandbox Code Playgroud)
不归还任何东西.我发现我可以将它转换为[数组],这使得它返回一个.NET .count方法,如预期的那样.
[Array]$Disks = Get-Disk
$Disks.Count
Run Code Online (Sandbox Code Playgroud)
这可以直接将其作为以前cmdlet的数组投射:
(Get-Services).Count
Run Code Online (Sandbox Code Playgroud)
推荐的解决方法是什么?
一个不起作用的例子:
$PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue}
Else If ($PageDisk.Count -gt 1) {Write-Host "Too many drives found, manually select it."}
Else If ($PageDisk.Count -eq 1) { Do X }
Run Code Online (Sandbox Code Playgroud)
选项A(演员阵容):
[Array]$PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue} …Run Code Online (Sandbox Code Playgroud) 可能是一个愚蠢的问题,但我只是好奇。
为该类下的应用程序调用卸载时Get-CIMInstance和调用卸载时有区别吗?我问的唯一原因是:Get-WMIObjectWin32_Product
Get-CIMInstance卸载应用程序时,将使用某些程序重新启动我的计算机。Get-WMIObject卸载应用程序无需重新启动即可运行。另外,将 a 管道连接Get-Member到任何Get-CIMInstance产品,并没有给我卸载的方法,但它确实使用Get-WMIObject. 开发者就是这么写的吗?虽然,Invoke-CIMMethod -Name Uninstall仍然有效。
Get-CIMInstance这是我使用/卸载多个应用程序时所做的操作Invoke-CIMMethod -Name Uninstall:
Get-CimInstance -ClassName win32_product | Where-Object Name -Match "Visual" |
ForEach-Object -Process {
Invoke-CimMethod -InputObject $_ -Name Uninstall
}
#Methods Returned
<#
Get-CimInstance -ClassName win32_product | Where-Object Name -Match "Visual" | Get-Member -MemberType Method
TypeName: Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_Product
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object ICloneable.Clone()
Dispose …Run Code Online (Sandbox Code Playgroud)