WMI管理軟體安裝卸載

設計為使用 Windows Installer 的應用程序可以通過 WMI 的Win32_Product 類訪問,但是並不是目前使用的所有應用程序都使用 WindowsInstaller。由於 Windows Installer為處理可安裝的應用程序提供了範圍最廣的各種標準技術,因此我們將集中討論這些應用程序。使用替代安裝常式的應用程序通常不受 WindowsInstaller 管理。處理這些應用程序的具體技術將取決於安裝程序軟體和應用程序開發人員的決定。

若要列出在本地或遠程系統上使用 Windows Installer 安裝的應用程序,請使用以下簡單 WMI 查詢:

PS> Get-WmiObject -Class Win32_Product -ComputerName .IdentifyingNumber : {7131646D-CD3C-40F4-97B9-CD9E4E6262EF}Name : Microsoft .NET Framework 2.0Vendor : Microsoft CorporationVersion : 2.0.50727Caption : Microsoft .NET Framework 2.0

若要在顯示器上顯示 Win32_Product 對象的所有屬性,請使用格式設置 cmdlet(例如 Format-Listcmdlet)的 Properties 參數,其值為 *(所有)。

Advertisements

PS> Get-WmiObject -Class Win32_Product -ComputerName . | Where-Object -FilterScript {$_.Name -eq "Microsoft .NET Framework 2.0"} | Format-List -Property *Name : Microsoft .NET Framework 2.0Version : 2.0.50727InstallState : 5Caption : Microsoft .NET Framework 2.0Description : Microsoft .NET Framework 2.0IdentifyingNumber : {7131646D-CD3C-40F4-97B9-CD9E4E6262EF}InstallDate : 20060506InstallDate2 : 20060506000000.000000-000InstallLocation :PackageCache : C:\WINDOWS\Installer\619ab2.msiSKUNumber :Vendor : Microsoft Corporation

也可以使用 Get-WmiObject Filter 參數只選擇 Microsoft .NETFramework 2.0。由於在此命令中使用的篩選器是 WMI 篩選器,因此它使用 WMI 查詢語言 (WQL) 語法,而不是Windows PowerShell 語法。那麼相應命令就變為:

Advertisements

Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='Microsoft .NET Framework 2.0'"| Format-List -Property *

請注意,WQL 查詢常用的字元(例如空格或等號)在 Windows PowerShell 中有特殊含義。因此,謹慎的做法是始終將Filter 參數的值放在一對引號內。還可以使用 Windows PowerShell 轉義字元,即倒引號(`),但它可能不會提高可讀性。以下命令等效於前面的命令,並返回相同結果,但卻使用倒引號將特殊字元轉義,而不是將整個篩選器字元串放在引號內。

Get-WmiObject -Class Win32_Product -ComputerName . -Filter Name`=`'Microsoft` .NET` Framework` 2.0`' | Format-List -Property *

若要只列出您希望查看的屬性,請使用格式設置 cmdlet 的 Property 參數來列出所需的屬性。

Get-WmiObject -Class Win32_Product -ComputerName . | Format-List -Property Name,InstallDate,InstallLocation,PackageCache,Vendor,Version,IdentifyingNumber...Name : HighMAT Extension to Microsoft Windows XP CD Writing WizardInstallDate : 20051022InstallLocation : C:\Program Files\HighMAT CD Writing Wizard\PackageCache : C:\WINDOWS\Installer\113b54.msiVendor : Microsoft CorporationVersion : 1.1.1905.1IdentifyingNumber : {FCE65C4E-B0E8-4FBD-AD16-EDCBE6CD591F}...

最後,若要只查找已安裝應用程序的名稱,一個簡單的 Format-Wide語句即可簡化輸出:

Get-WmiObject -Class Win32_Product -ComputerName . | Format-Wide -Column 1

儘管現在已有幾種方式來查看使用 Windows Installer完成安裝的應用程序,但我們尚未考慮其他應用程序。由於大多數標準應用程序會向 Windows 註冊它們的卸載程序,因此可以在Windows 註冊表中查找這些程序,以便在本地處理它們。

列出所有可卸載的應用程序

儘管不能保證找到系統中的所有應用程序,但可以找到在「添加或刪除程序」對話框中列出的所有程序。「添加或刪除程序」將在以下註冊表項中查找這些應用程序:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall

還可以檢查此註冊表項以查找應用程序。若要使查看 Uninstall 項更容易,可以將 Windows PowerShell驅動器映射到此註冊表位置:

PS>Name Provider Root CurrentLocation---- -------- ---- ---------------Uninstall Registry HKEY_LOCAL_MACHINE\SOFTWARE\Micr...
PS> (Get-ChildItem -Path Uninstall:).Count459

可以從 Get-ChildItem開始使用多種技術進一步搜索此應用程序列表。若要獲取應用程序的列表並將它們保存在$UninstallableApplications 變數中,請使用以下命令:

$UninstallableApplications = Get-ChildItem -Path Uninstall:

例如,若要在 Uninstall 項中查找應用程序的顯示名稱,請使用以下命令:

PS> Get-ChildItem -Path Uninstall: | ForEach-Object -Process { $_.GetValue("DisplayName") }

無法保證這些值是唯一的。在以下示例中,兩個已安裝項都顯示為「Windows Media Encoder 9Series」:

PS> Get-ChildItem -Path Uninstall: | Where-Object -FilterScript { $_.GetValue("DisplayName") -eq "Windows Media Encoder 9 Series"}Hive: Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\UninstallSKC VC Name Property--- -- ---- --------0 3 Windows Media Encoder 9 {DisplayName, DisplayIcon, UninstallS...0 24 {E38C00D0-A68B-4318-A8A6-F7... {AuthorizedCDFPrefix, Comments, Conta...

安裝應用程序

可以使用 Win32_Product 類在遠程或本地安裝 Windows Installer程序包。遠程安裝時,應使用通用命名約定 (UNC) 網路路徑指定 .msi 程序包的路徑,這是因為 WMI 子系統無法識別Windows PowerShell 路徑。例如,若要將位於網路共享 \\AppServ\dsp 中的 NewPackage.msi程序包安裝到遠程計算機 PC01 上,請在 Windows PowerShell 提示符下鍵入以下命令:

(Get-WMIObject -ComputerName PC01 -List | Where-Object -FilterScript {$_.Name -eq "Win32_Product"}).Install(\\AppSrv\dsp\NewPackage.msi)

不使用 Windows Installer技術的應用程序可能有特定於應用程序的方法來進行自動部署。若要確定是否有自動部署的方法,請查看應用程序的文檔,或嚮應用程序供應商的支持體系諮詢。在某些情況下,即使應用程序供應商沒有專門將應用程序設計為自動安裝,該安裝程序軟體製造商仍然可能有某些自動化的技術。

刪除應用程序

使用 Windows PowerShell 刪除 Windows Installer程序包的方式與安裝程序包的方式大致相同。下面是基於程序包名稱選擇要卸載的程序包的示例;在某些情況下,用IdentifyingNumber 進行篩選可能更容易:

(Get-WmiObject -Class Win32_Product -Filter "Name='ILMerge'" -ComputerName . ).Uninstall()

刪除其他應用程序並不那麼簡單,甚至在本地這樣做時也如此。可以通過提取UninstallString 屬性來查找這些應用程序的命令行卸載字元串。此方法對 WindowsInstaller 應用程序和出現在 Uninstall 項下面的舊程序有效:

Get-ChildItem -Path Uninstall: | ForEach-Object -Process { $_.GetValue("UninstallString") }

如果需要,可以按顯示名篩選輸出:

Get-ChildItem -Path Uninstall: | Where-Object -FilterScript { $_.GetValue("DisplayName") -like "Win*"} | ForEach-Object -Process { $_.GetValue("UninstallString") }

但是,如果不進行某些修改,這些字元串可能無法在 Windows PowerShell 提示符下直接使用。

升級 Windows Installer 應用程序

若要升級應用程序,需要知道應用程序的名稱,以及應用程序升級程序包的路徑。有了這些信息,就可以使用單個 WindowsPowerShell 命令升級應用程序:

(Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='OldAppName'").Upgrade(\\AppSrv\dsp\OldAppUpgrade.msi)

Advertisements

你可能會喜歡