Powershell,如何获取最后一次Windows更新安装的日期或至少检查更新的日期?

7
我正在尝试找到一种检索最后一个Windows更新已安装或已检查的日期/时间的方法。目前,我找到了一个函数,可以列出最近的Windows更新,但是对于这样一个简单的功能来说,它包含的数据太多、太臃肿了。其次,我尝试访问注册表,但是没有成功检索到我需要的值。我在Windows 10机器上进行测试,但是该软件可能驻留在Windows Server 2012 R2上。以下是我尝试过的一些代码示例:
$key = “SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install” 
$keytype = [Microsoft.Win32.RegistryHive]::LocalMachine 
$RemoteBase = [Microsoft.Win32.RegistryKey]::OpenBaseKey($keytype,"My Machine") 
$regKey = $RemoteBase.OpenSubKey($key) 
$KeyValue = $regkey.GetValue(”LastSuccessTime”) 

$System = (Get-Date -Format "yyyy-MM-dd hh:mm:ss")  

此外,只需要尝试Get-ChildItem命令。
$hello = Get-ChildItem -Path “hkcu:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\”

foreach ($a in $hello) {

$a

}

我已经在regedit中检查过,这个键不存在。前往“Windows更新”路径只显示应用程序更新而不是Windows更新。

编辑 使用以下命令似乎更接近我的目标: Get-HotFix | Where {$_.InstallDate -gt 30}

但是,如何只检索最近30天内安装的内容?这并没有显示出许多结果,即使使用 Select $_.InstallDate.

5个回答

13

一个选项:

 gwmi win32_quickfixengineering |sort installedon -desc 

另一种选择是使用COM对象Microsoft.Update.Session,可以在这里找到:https://p0w3rsh3ll.wordpress.com/2012/10/25/getting-windows-updates-installation-history/ 简而言之:

$Session = New-Object -ComObject Microsoft.Update.Session 
$Searcher = $Session.CreateUpdateSearcher()
$HistoryCount = $Searcher.GetTotalHistoryCount()
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa386532%28v=vs.85%29.aspx
$Searcher.QueryHistory(0,$HistoryCount) | ForEach-Object {$_}

整洁了,但结果不多,只有11个。这正常吗?其次,能否只获取最近30天的数据? - Harvey
未经测试,您可以尝试使用以下命令:gwmi win32_quickfixengineering |sort installedon -desc |?{ ((get-date)-$_.installedon) -lt 30 } - Loïc MICHEL
1
这个方法没有起作用,但我能看出你想要什么。似乎是一个类型问题...不过我刚刚找到了这个...但是无法获取可靠的信息:'gwmi -cl win32_reliabilityRecords'。 - Harvey
这里列出了一些时间:gwmi -cl win32_reliabilityRecords | select timegenerated - Harvey
1
有一个不错的替代方案,可以使用COM对象,我正在更新答案。 - Loïc MICHEL
显示剩余2条评论

6
这里是如何用一行PowerShell代码知道最后一次Windows更新的日期和时间:
(New-Object -com "Microsoft.Update.AutoUpdate"). Results | fl

您还可以使用以下脚本在 Windows Server 上进行大量检查:
$ servers = Get-ADComputer -Filter {(OperatingSystem-like "* windows * server *") -and (Enabled -eq "True")} -Properties OperatingSystem | Sort Name | select -Unique Name

foreach ($ server in $ servers) {
write-host $ server.Name

   Invoke-Command -ComputerName $ server.Name -ScriptBlock {
(New-Object -com "Microsoft.Update.AutoUpdate"). Results}
}

来源于:https://www.sysadmit.com/2019/03/windows-update-ver-fecha-powershell.html

我想知道与另一个答案 Get-HotFix |?{$_.InstalledOn -gt ((Get-Date).AddDays(-30))} 的区别。你的答案显示了我今天的最新更新,而另一个答案没有显示,但显示了更多带有 kbnumber 的更新。 - Timo

4

Get-HotFix |?{$_.InstalledOn -gt ((Get-Date).AddDays(-30))}

可以列出最近30天内安装的所有热补丁。

4
欢迎来到 Stack Overflow!尽管这段代码可能解决了问题,但是包含解释会真正有助于提高您的帖子质量。请记住,您正在为未来的读者回答问题,那些人可能不知道您提出代码建议的原因。请还尽量不要在代码中添加过多的解释性注释,这会降低代码和解释的可读性! - kayess

1

使用PowerShell,您可以像这样获取最后一次Windows更新的日期:

$lastWindowsUpdate = (Get-Hotfix | Sort-Object -Property InstalledOn -Descending | Select-Object -First 1).InstalledOn

0

Pre-PowerShell 7

用户可以使用Loïc MICHEL的代码通过WMI进行查询:

Get-WmiObject Win32_QuickFixEngineering | Sort-Object -Property InstalledOn -Descending

PowerShell 7之后

由于切换到.NET Core,PowerShell 7已经弃用了WMI。相反,您可以使用CIM。

Get-CimInstance Win32_QuickFixEngineering | Sort-Object -Property InstalledOn -Descending

替代方案

kayessMartin发布了出色的、简单的示例,使用Get-HotFix,适用于PowerShell 5.1到7.3版本(可能也适用于其他版本)。

Get-HotFix | Sort-Object -Property InstalledOn -Descending

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接