gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:31:44 25 4
gpt4 key购买 nike

这是我用于搜索 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

最佳答案

简单看一下 COM 对象的属性和方法并没有显示任何内容。 You can query update before to see if they might trigger a reboot但这并不能保证客户将如何 react 。

可能还有其他方法,但如果您想确定当前状态,一个建议是查看注册表。

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

因此,就 WU 而言,您只需检查该键中是否有任何 值即可了解其待定状态。

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

if($results){
# Reboot is pending
}

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

Note that the RebootRequired key is automatically deleted when the machine reboots as it's volatile (only held in memory).

这可能会隐藏其他潜在问题,因此您可能需要将逻辑更改为 try/catch 并查看特定错误,如 ItemNotFoundException(如果存在问题)。

关于安装 Windows 更新后检查重启状态的 Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45716422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com