PowerShell:如何查找和卸载MS Office Update

时间:2010-05-04 03:33:26

标签: powershell ms-office uninstall

我一直在寻找一种干净的方法来卸载大量工作站上的MSOffice安全更新。我发现了一些尴尬的解决方案,但没有像使用PowerShell和get-wmiobject Win32_QuickFixEngineering以及.Uninstall方法那样干净或一般。

[显然,Win32_QuickFixEngineering仅指Windows补丁。请参阅:http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/93cc0731-5a99-4698-b1d4-8476b3140aa3]

问题1:是否无法使用get-wmiobject查找MSOffice更新?有很多类和名称空间,我不得不怀疑。

此特定的Office更新(KB978382)可在此处的注册表中找到(对于Office Ultimate):

HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\{91120000-002E-0000-0000-0000000FF1CE}_ULTIMATER_{6DE3DABF-0203-426B-B330-7287D1003E86}

通过以下方式显示卸载命令:

msiexec /package {91120000-002E-0000-0000-0000000FF1CE} /uninstall {6DE3DABF-0203-426B-B330-7287D1003E86}

并且最后一个GUID在不同版本的Office之间似乎是不变的。

我也发现了这样的更新:
    $wu = new-object -com "Microsoft.Update.Searcher"
    $wu.QueryHistory(0,$wu.GetTotalHistoryCount()) | where {$_.Title -match "KB978382"}

我喜欢这种搜索,因为它不需要在注册表中进行任何调查,但是:

问题2:如果我发现它是这样的,我怎么处理找到的信息以方便卸载?

由于

1 个答案:

答案 0 :(得分:4)

扩展第二种方法(使用Microsoft.Update.Searcher),此解决方案应允许您按标题搜索更新,然后将其卸载:

$TitlePattern = 'KB978382'

$Session = New-Object -ComObject Microsoft.Update.Session

$Collection = New-Object -ComObject Microsoft.Update.UpdateColl
$Installer = $Session.CreateUpdateInstaller()
$Searcher = $Session.CreateUpdateSearcher()

$Searcher.QueryHistory(0, $Searcher.GetTotalHistoryCount()) | 
    Where-Object { $_.Title -match $TitlePattern } |
    ForEach-Object {
        Write-Verbose "Found update history entry $($_.Title)"
        $SearchResult = $Searcher.Search("UpdateID='$($_.UpdateIdentity.UpdateID)' and RevisionNumber=$($_.UpdateIdentity.RevisionNumber)")
        Write-Verbose "Found $($SearchResult.Updates.Count) update entries"
        if ($SearchResult.Updates.Count -gt 0) {
            $Installer.Updates = $SearchResult.Updates
            $Installer.Uninstall()
            $Installer | Select-Object -Property ResultCode, RebootRequired, Exception
            # result codes: http://technet.microsoft.com/en-us/library/cc720442(WS.10).aspx
        }
    }