已使用Powershell安装的MSKB修补程序更新清单

时间:2014-10-31 16:09:00

标签: powershell csv remote-server

我尝试过PowerShell的Get-Hotfix并且不喜欢结果。我不需要安装任何新的更新。我希望以CSV格式得到结果,因为我要列出大约30台服务器。

这是我到目前为止所做的:

$servern = 'ABC'

$Session =  [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session",$servern))
$Searcher = $Session.CreateUpdateSearcher()
$HistoryCount = $Searcher.GetTotalHistoryCount()
$Searcher.QueryHistory(1, $historyCount) | Select-Object Date,$servern,
$temp = "" | Select Computer, operation, resultcode,resultcode 

$temp.Computer = $servern,
$temp.operation = expression={switch($_.operation){
    1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}},
$temp.resultcode = expression={switch($_.resultcode){
        1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"};
        4 {"Failed"}; 5 {"Aborted"}
}}
$temp.Title

# What we would like to see is:
# SERVER, DATE, TITLE, OPERATION, RESULTCODE
# ABC,5/5/2011 3:29:52 PM,Update for Windows Server 2003 (KB927891),Installation,Succeeded
# ABC,5/5/2011 3:30:01 PM,Cumulative Security Update for Outlook Express for Windows Server 2003 (KB929123),Installation,Succeeded
# etc..

将这些结果以CSV格式显示。

谢谢!

1 个答案:

答案 0 :(得分:0)

这就是我所拥有的,这非常有效!

感谢来自" StuStars"的评论。在 http://social.technet.microsoft.com/wiki/contents/articles/4197.how-to-list-all-of-the-windows-and-software-updates-applied-to-a-computer.aspx

使用的其他资源:

另外,感谢Boe Prox的拆分例程来分离KB文章。这将在本地列出更新。

$Session = New-Object -ComObject "Microsoft.Update.Session"
$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
$Searcher.QueryHistory(0, $historyCount) | Select-Object Date,
   @{name="Operation"; expression={switch($_.operation){
       1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}},
   @{name="Status"; expression={switch($_.resultcode){
       1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"};
       4 {"Failed"}; 5 {"Aborted"}
}}},
   @{n='KB';e={(($_.Title -split('\('))[1] -split('\)'))[0] }},
   Title
 | Export-Csv -NoType "$Env:userprofile\Desktop\Windows Updates.csv"

并在桌面上放置一个名为Windows Updates.csv的文件。

让我们重新做一点。我们假设您可以访问这组服务器,并且可以运行脚本来查询它们。现在,让我们对此进行过滤。这使用具有名称的服务器列表(Named_computers.txt),例如:

ABC
DEF

代码是:

$results = @()
$serverlist = "D:\WORK\ps\Named_computers.txt"

foreach ($computer in Get-Content $serverlist) {
    $Session =  [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session",$computer))
    $Searcher = $Session.CreateUpdateSearcher()
    $historyCount = $Searcher.GetTotalHistoryCount()
    $results += $Searcher.QueryHistory(0, $historyCount) | Select-Object Date,
        @{n='ComputerName';e={$computer}},
        @{name="Operation"; expression={switch($_.operation){
            1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}},
        @{name="Status"; expression={switch($_.resultcode){
            1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"};
            4 {"Failed"}; 5 {"Aborted"}
        }}},
        @{n='KB';e={(($_.Title -split('\('))[1] -split('\)'))[0] }},
        Title
        }
$results |
where { $_.Date -gt "01/01/2014"} |
Export-Csv -NoType "$Env:userprofile\Desktop\Windows Updates.csv"

让我们稍微改变一下,并提供一些我在周末使用的过滤器。这使用带有名称和IP地址的服务器列表(`Monitored_computers.txt),例如:

ABC,1.1.1.1
DEF,2.2.2.2

代码是:

$results = @()
$serverlist = "D:\WORK\ps\monitored_computers.txt"

foreach ($computer in Get-Content $serverlist) {
$servern=$computer.split(",")[0]
$ip=$computer.split(",")[1]

    $Session =  [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session",$servern))
    $Searcher = $Session.CreateUpdateSearcher()
    $historyCount = $Searcher.GetTotalHistoryCount()
    $results += $Searcher.QueryHistory(0, $historyCount) | Select-Object Date,
        @{n='ComputerName';e={$servern}},
        @{n='IP';e={$ip}},
        @{name="Operation"; expression={switch($_.operation){
            1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}},
        @{name="Status"; expression={switch($_.resultcode){
            1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"};
            4 {"Failed"}; 5 {"Aborted"}
        }}},
        @{n='KB';e={(($_.Title -split('\('))[1] -split('\)'))[0] }},
        Title
        }
$results |
#where { $_.Date -gt "01/01/2014" } |
where { $_.Date -gt "01/01/2014" -and  $_.Status -ne "Succeeded" } |
Export-Csv -NoType "$Env:userprofile\Desktop\Windows Updates.csv"