【发布时间】:2016-04-21 06:52:06
【问题描述】:
【问题讨论】:
标签: powershell wmi-query wmic cmdlets
【问题讨论】:
标签: powershell wmi-query wmic cmdlets
试试这个方法:
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
$HistoryCount = $Searcher.GetTotalHistoryCount()
$Updates = $Searcher.QueryHistory(0,$HistoryCount)
$Updates | Select Title,@{l='Name';e={$($_.Categories).Name}},Date
【讨论】:
编辑:
要获取所有更新(仅通过 Windows 更新安装,即使对于第 3 方),然后将结果导出到文本文件,您可以使用以下脚本:
$session = [activator]::CreateInstance([type]::GetTypeFromProgID(“Microsoft.Update.Session”,$ComputerName))
$us = $session.CreateUpdateSearcher()
$qtd = $us.GetTotalHistoryCount()
$hot = $us.QueryHistory(0, $qtd)
# Output object
$OutPut = @()
#Iterating and finding updates
foreach ($Upd in $hot) {
# 1 means in progress and 2 means succeeded
if ($Upd.operation -eq 1 -and $Upd.resultcode -eq 2) {
$OutPut += New-Object -Type PSObject -Prop @{
‘ComputerName’=$computername
‘UpdateDate’=$Upd.date
‘KB’=[regex]::match($Upd.Title,’KB(\d+)’)
‘UpdateTitle’=$Upd.title
‘UpdateDescription’=$Upd.Description
‘SupportUrl’=$Upd.SupportUrl
‘UpdateId’=$Upd.UpdateIdentity.UpdateId
‘RevisionNumber’=$Upd.UpdateIdentity.RevisionNumber
}
}
}
#Writing updates to a text file
$OutPut | Out-File -FilePath "C:\output.txt" -Append
较早的回复:
您可以使用来自 Technet 的这个精彩脚本,而不是创建自己的脚本:PowerShell script to list all installed Microsoft Windows Updates
由于您需要文本格式的输出,我已经更新了该文章中的脚本,以便为文本文件中的所有已安装更新生成输出。一切都是可配置的。检查下面的完整脚本。最后一行是我调用可重用方法并将输出生成到文本文件的地方。您可以根据您的环境更改此文本文件的路径。
Function Get-MSHotfix
{
$outputs = Invoke-Expression "wmic qfe list"
$outputs = $outputs[1..($outputs.length)]
foreach ($output in $Outputs) {
if ($output) {
$output = $output -replace 'y U','y-U'
$output = $output -replace 'NT A','NT-A'
$output = $output -replace '\s+',' '
$parts = $output -split ' '
if ($parts[5] -like "*/*/*") {
$Dateis = [datetime]::ParseExact($parts[5], '%M/%d/yyyy',[Globalization.cultureinfo]::GetCultureInfo("en-US").DateTimeFormat)
} elseif (($parts[5] -eq $null) -or ($parts[5] -eq ''))
{
$Dateis = [datetime]1700
}
else {
$Dateis = get-date([DateTime][Convert]::ToInt64("$parts[5]", 16))-Format '%M/%d/yyyy'
}
New-Object -Type PSObject -Property @{
KBArticle = [string]$parts[0]
Computername = [string]$parts[1]
Description = [string]$parts[2]
FixComments = [string]$parts[6]
HotFixID = [string]$parts[3]
InstalledOn = Get-Date($Dateis)-format "dddd d MMMM yyyy"
InstalledBy = [string]$parts[4]
InstallDate = [string]$parts[7]
Name = [string]$parts[8]
ServicePackInEffect = [string]$parts[9]
Status = [string]$parts[10]
}
}
}
}
Get-MSHotfix|Select-Object -Property Computername, KBArticle,InstalledOn, HotFixID, InstalledBy|Format-Table | Out-File -FilePath "D:\output.txt"
【讨论】:
使用 powershell 运行以下命令。您可以轻松地将Select-Object 重定向到out-file
Get-WmiObject -Class Win32_QuickFixEngineering
请参考Win32_QuickFixEngineering 了解更多信息。
【讨论】:
最简单的方法
Install-Module PSWindowsUpdate -force
Get-WuHistory
Install-Module 在 Posh5 上工作。对于老年人使用巧克力:cinst PSWindowsUpdate.
【讨论】: