下面是我用来搜索由WSUS安装的Windows更新的代码,我想再添加一列,以了解重新启动的状态。有开关吗?
$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发布于 2017-08-16 15:21:29
简单地看一下COM对象属性和方法,就不会显示这方面的任何内容。看看它们是否会触发重新启动,但这并不能保证客户端的反应。
可能还有其他方法,但是如果您想要确定当前的状态,一个建议是在注册表中查找。
如果WindowsUpdates安装了一个需要重新启动的修补程序,它应该在这个位置留下一个注册表项。
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之类的特定错误。
https://stackoverflow.com/questions/45716422
复制相似问题