Powershell,如何获取上次 Windows 更新安装的日期或至少检查更新的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33732541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Powershell, How to get date of last Windows update install or at least checked for an update?
提问by Harvey
I am trying to find a way of retrieving the date/time of which the last windows update was either installed, or checked for.
我试图找到一种方法来检索上次安装或检查 Windows 更新的日期/时间。
So far I have found a function that allows to list recent Windows Updates, but it is far too much data and too bloated for such a simple function. Secondly I have tried to access the registry although I am having no luck in retriving the value I am after.
到目前为止,我已经找到了一个允许列出最近的 Windows 更新的功能,但是对于这样一个简单的功能来说,它的数据太多并且过于臃肿。其次,我尝试访问注册表,尽管我在检索我所追求的值方面没有运气。
I am testing this on a Windows 10 Machine although the software will probably reside on Windows Server 2012 R2.
我正在 Windows 10 机器上对此进行测试,尽管该软件可能会驻留在 Windows Server 2012 R2 上。
Here is an example of some of the code I have tried:
这是我尝试过的一些代码的示例:
$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")
Also, just trying the Get-ChildItem
另外,只需尝试 Get-ChildItem
$hello = Get-ChildItem -Path “hkcu:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\”
foreach ($a in $hello) {
$a
}
I've checked in regedit and this key does not exist. Going to the "Windows Update" path shows only App Updates and not Windows updates.
我已经检查了 regedit 并且这个键不存在。转到“Windows 更新”路径仅显示应用程序更新而不是 Windows 更新。
EDIT I seem to be closer to my goal with this line: Get-HotFix | Where {$_.InstallDate -gt 30}
编辑我似乎更接近我的目标这一行:Get-HotFix | 其中 {$_.InstallDate -gt 30}
However how to I only retrive those of which have been installed in the last 30 days? And this doesnt show many results, even using Select $_.InstallDate
但是,如何仅检索过去 30 天内安装的那些?这并没有显示出很多结果,即使使用Select $_.InstallDate
回答by Lo?c MICHEL
an option :
一个选项 :
gwmi win32_quickfixengineering |sort installedon -desc
Another alternative, using the com object Microsoft.Update.Sessioncan be find here : https://p0w3rsh3ll.wordpress.com/2012/10/25/getting-windows-updates-installation-history/in short :
另一种选择,使用 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 {$_}
回答by OuttaBeer
Get-HotFix |?{$_.InstalledOn -gt ((Get-Date).AddDays(-30))}
Get-HotFix |?{$_.InstalledOn -gt ((Get-Date).AddDays(-30))}
回答by John Burkhead
Here you have how to know the date and time of the last Windows update in a single line of Powershell:
在这里,您可以在一行 Powershell 中了解上次 Windows 更新的日期和时间:
(New-Object -com "Microsoft.Update.AutoUpdate"). Results | fl
You also have the following script to check it massively in Windows Server:
您还可以使用以下脚本在 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}
}
Extracted from: https://www.sysadmit.com/2019/03/windows-update-ver-fecha-powershell.html
摘自:https: //www.sysadmit.com/2019/03/windows-update-ver-fecha-powershell.html