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

Har*_*vey 2 windows powershell

我试图找到一种方法来检索安装或检查最后一次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")  
Run Code Online (Sandbox Code Playgroud)

此外,只需尝试Get-ChildItem

$hello = Get-ChildItem -Path “hkcu:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\”

foreach ($a in $hello) {

$a

}
Run Code Online (Sandbox Code Playgroud)

我已经检查了regedit,这个密钥不存在.转到"Windows Update"路径仅显示应用更新,而不显示Windows更新.

编辑我似乎更接近我的目标:Get-HotFix | 哪里{$ _.InstallDate -gt 30}

但是,我如何只追溯过去30天内安装过的那些?即使使用,这也没有显示出许多结果Select $_.InstallDate

Loï*_*HEL 9

一个选项 :

 gwmi win32_quickfixengineering |sort installedon -desc 
Run Code Online (Sandbox Code Playgroud)

使用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 {$_}
Run Code Online (Sandbox Code Playgroud)


小智 6

在这里,您可以了解如何在一行 Powershell 中了解上次 Windows 更新的日期和时间:

(New-Object -com "Microsoft.Update.AutoUpdate"). Results | fl
Run Code Online (Sandbox Code Playgroud)

您还可以使用以下脚本在 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}
}
Run Code Online (Sandbox Code Playgroud)

摘自:https://www.sysadmit.com/2019/03/windows-update-ver-fecha-powershell.html