簡體   English   中英

Get-WMIObject 卸載與 Get-CIMInstance 卸載

[英]Get-WMIObject Uninstall vs Get-CIMInstance Uninstall

可能是一個愚蠢的問題,但我只是好奇。

Win32_Product class 下的應用程序調用卸載時, Get-CIMInstanceGet-WMIObject之間有區別嗎? 我問的唯一原因是因為:

  • 使用Get-CIMInstance卸載應用程序,將使用某些程序重新啟動我的計算機。
  • 使用Get-WMIObject卸載應用程序無需重新啟動即可運行。

此外,通過管道將Get-Member傳遞給任何Get-CIMInstance產品,並沒有給我提供卸載的方法,但它確實使用了Get-WMIObject 開發人員就是這樣寫的嗎? 雖然, Invoke-CIMMethod -Name Uninstall仍然有效。

獲取 CIMInstance / 卸載

下面是我使用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                   Method     void Dispose(), void IDisposable.Dispose()
Equals                    Method     bool Equals(System.Object obj)
GetCimSessionComputerName Method     string GetCimSessionComputerName()
GetCimSessionInstanceId   Method     guid GetCimSessionInstanceId()
GetHashCode               Method     int GetHashCode()
GetObjectData             Method     void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Ser...
GetType                   Method     type GetType()
ToString                  Method     string ToString()
#>

獲取 WMIObject / 卸載

Get-WMIObject -ClassName win32_product | Where-Object Name -Match "Visual" | 
    ForEach-Object -Process { 
        Invoke-WMIMethod -InputObject $_ -Name Uninstall 
                            }
#Methods Returned
<#
Get-WMIObject -Class win32_product | Where-Object Name -Match "Visual" | Get-Member -MemberType Method


   TypeName: System.Management.ManagementObject#root\cimv2\Win32_Product

Name      MemberType Definition
----      ---------- ----------
Configure Method     System.Management.ManagementBaseObject Configure(System.UInt16 InstallState, System.UInt16 InstallLevel, S...
Reinstall Method     System.Management.ManagementBaseObject Reinstall(System.UInt16 ReinstallMode)
Uninstall Method     System.Management.ManagementBaseObject Uninstall()
Upgrade   Method     System.Management.ManagementBaseObject Upgrade(System.String PackageLocation, System.String Options)
#>

請原諒長篇大論,只是一個好奇的頭腦。

如果不允許,請刪除/關閉。

使用 WMI cmdlet 和較新的 CIM cmdlet 之間存在許多差異。 Get-WMIObject is deprecated in windows PowerShell and has been removed from PowerShell Core, so the general recommendation is to go with CIM. 不過,這些方法的行為不應有所不同,因此我無法解釋您提到的重啟行為。

Get-CimInstance don't have the methods, but you can pass them to Invoke-CimMethod`。

$instance = Get-CimInstance win32_process -Filter "Name = 'powershell_ise.exe'"
$instance | Invoke-CimMethod -MethodName 'Terminate'

您可以使用Get-CimClass發現方法

(Get-CimClass win32_process ).CimClassMethods

如果您需要 arguments 用於給定方法,則可以使用 hash 表作為參數通過-Arguments參數傳遞它們。 您可以在幫助文件或此處找到示例

您也可以直接使用 Invoke-WMIMethod:

Invoke-CimMethod -Query "SELECT * FROM Win32_Process WHERE Name = 'powershell_ise.exe'" -MethodName Terminate

我通常不這樣做,因為將 -Filter 與-Filter一起使用會更-CLassName ,並且-Filter is missing 不適用於Invoke-CimMethod但是,這些只是個人喜好。

我建議您也閱讀CIM Cmdlet 簡介

此外,Win32_Product 的名聲也很差。 如果你用谷歌搜索,你可以獲得更多信息,但這是我通過 SO 問題快速找到的一篇文章:為什么 Win32_Product 是壞消息

作為一般規則,您應該在命令中向左移動過濾。 使用-Query-Filter參數,而不是獲取所有實例並在之后使用Where{} 特別是考慮到 Win32_Product 的已知性能問題。

或者使用 get-package,假設它是一個 msi 安裝:

get-package *visual* | uninstall-package

win32_product class 也是出了名的慢,因為它會在使用時驗證所有 msi。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM