System.Update.LastInstallTime на Windows Server 2016 (Решение)

Started by Akira, November 09, 2017, 11:48:40 AM

Previous topic - Next topic

Akira

System.Update.LastInstallTime берет информацию из реестра
ветка HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install
параметр LastSuccessTime

В 2016 этот ключ отсутствует.
Решено ежедневным запуском на серверах следующего PowerShell скрипта

$Session = New-Object -ComObject "Microsoft.Update.Session"
$Searcher = $Session.CreateUpdateSearcher()

$historyCount = $Searcher.GetTotalHistoryCount()

$a=$Searcher.QueryHistory(0, $historyCount) | Select-Object Title, Description, Date,

    @{name="Operation"; expression={switch($_.operation){

        1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}

}}} | where {$_.operation -eq "Installation"} | Select-Object -First 1

$datetimestring = Get-Date $a.Date -Format "yyyy-MM-dd hh:mm:ss"

New-Item 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update' -Name Results
New-Item 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results' -Name Install

Set-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install' -Name LastSuccessTime -Value $datetimestring


Он возможно избыточен, но работает.