安装Windows更新后检查重新启动状态的Powershell

问题描述:

这里是我用于搜索WSUS安装的Windows更新的代码,我想为重新启动挂起/完成的状态添加一列。有没有开关?安装Windows更新后检查重新启动状态的Powershell

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

$Searcher = $Session.CreateUpdateSearcher() 

$historyCount = $Searcher.GetTotalHistoryCount() 

$Searcher.QueryHistory(0, $historyCount) | Select-Object Date, 

    @{name="Operation"; expression={switch($_.operation){ 

     1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}}, 

    @{name="Status"; expression={switch($_.resultcode){ 

     1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"}; 

     4 {"Failed"}; 5 {"Aborted"} 

}}}, Title | Out-GridView 
+0

我不认为我们有任何检查重启状态的特定开关。我认为我们必须明确地检查 –

简要介绍一下COM对象的属性和方法并不显示任何内容。 You can query update before to see if they might trigger a reboot但它不是客户如何反应的保证。

可能还有其他方法,但是如果您想确切知道当前状态,一个建议就是在注册表中查找。

If a patch was installed by WindowsUpdates that requires a reboot it should leave a registry entry in this location

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired

所以你只需要检查是否有在关键就吴关心的是它的挂起状态的任何值。

$pendingRebootKey = "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" 
$results = (Get-Item $pendingRebootKey -ErrorAction SilentlyContinue).Property 

if($results){ 
    # Reboot is pending 
} 

使用-ErrorAction是因为有用,根据文章:

注意, RebootRequired键被自动删除当机器重新启动,因为它是 挥发性(仅保留在内存中)。

这可能隐藏其它潜在的问题,所以你可能需要将逻辑切换到一个try/catch,看的东西像ItemNotFoundException特定错误,如果有关注那里。

+0

如何将此添加到我当前的输出中作为新列?一个新的对象? –

+0

这是在主机上执行正确吗?您当前的输出列表更新。尽管您可能能够将更新连接到注册表数据,但我不确定如何以令人满意的方式为您添加此更新。您可以添加另一个计算属性,但它是运行计算机的属性,而不是您正在查询的更新。 – Matt