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

已使用Powershell安装的MSKB修补程序更新的清单,powershell,csv,remote-server,Powershell,Csv,Remote Server,我尝试过PowerShell的获取修补程序,但不喜欢结果。我不需要安装任何新的更新。我想得到一个CSV格式的结果,因为我有大约30个服务器列表 以下是我到目前为止的情况: $servern = 'ABC' $Session = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session",$servern)) $Searcher = $Session.CreateUpdateSearcher(

我尝试过PowerShell的
获取修补程序
,但不喜欢结果。我不需要安装任何新的更新。我想得到一个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中


谢谢

这是我所拥有的,效果很好

感谢“斯图之星”在

使用的其他资源:

另外,感谢从中分离出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
的文件

让我们再做一点。我们假设您可以访问服务器集,并且可以运行脚本来查询它们。现在,让我们在上面放置一个过滤器。这将使用带有名称的服务器列表(
命名为_computers.txt
),例如:

ABC
DEF
ABC,1.1.1.1
DEF,2.2.2.2
代码是:

$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"
$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"
让我们稍微修改一下,并提供我周末使用的几个过滤器。这将使用服务器列表(`Monitored_computers.txt),其中包含名称和IP地址,例如:

ABC
DEF
ABC,1.1.1.1
DEF,2.2.2.2
代码是:

$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"
$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"

这不是您的答案,但您是否正在使用WSUS?如果是,您可以使用WSUS 4/PowerShell 3中提供的WSUS cmdlet吗?这样,WSUS会为您跟踪所有这些信息,您可以在中心位置查询它。我不知道它是WSUS还是Shavlik还是?对我们服务器的更新由与我们不同的组管理。我们正在独立地尝试捕获已安装的服务器/补丁的日志或列表,然后与补丁团队联系。