【问题标题】:Powershell, How to get date of last Windows update install or at least checked for an update?Powershell,如何获取上次 Windows 更新安装的日期或至少检查更新的日期?
【发布时间】:2015-11-16 09:56:16
【问题描述】:

我正在尝试找到一种方法来检索上次安装或检查 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 更新。

编辑 这条线似乎更接近我的目标: 获取热修复 | {$_.InstallDate -gt 30}

但是,如何仅检索过去 30 天内安装的那些?这并没有显示很多结果,即使使用Select $_.InstallDate

【问题讨论】:

    标签: windows powershell


    【解决方案1】:

    一个选项:

     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 天的数据?
    • 未测试,你可以试试gwmi win32_quickfixengineering |sort installedon -desc |?{ ((get-date)-$_.installedon) -lt 30 }
    • 这不起作用,但我可以看到你想得到什么。这似乎是一个类型问题......但是我刚刚发现了这个......但无法获得相关信息:'gwmi -cl win32_reliabilityRecords'
    • 这列出了一些时间:gwmi -cl win32_reliabilityRecords |选择时间生成
    • 有一个很好的替代使用 com 对象,我正在更新答案。
    【解决方案2】:

    在这里,您可以在一行 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。
    【解决方案3】:

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

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!
    猜你喜欢
    • 2014-07-25
    • 1970-01-01
    • 2017-01-29
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多