2014-10-31 99 views
-1

我试过PowerShell的Get-Hotfix,不喜欢结果。我不需要安装任何新的更新。我想以CSV格式获得结果,因为我有大约30台服务器要列出。使用Powershell已安装的MSKB补丁更新清单

这是我到目前为止有:

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

谢谢!

+0

这不是你一个答案,但您使用WSUS?如果是这样,您可以使用WSUS 4/PowerShell 3中提供的WSUS cmdlet吗?通过这种方式,WSUS将为您保留所有这些信息,并且您可以在中心位置进行查询。 – briantist 2014-10-31 16:36:49

+0

我不知道它是WSUS还是Shavlik?我们的服务器更新由与我们不同的团队管理。我们正在独立地尝试捕获安装的服务器/补丁的日志或列表,然后与补丁团队联系。 – Leptonator 2014-10-31 16:43:54

回答

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" 

让我们改变了一阵,并提供我用周末一对夫妇的过滤器。它使用服务器列表(`Monitored_computers.txt)的名称和IP地址,例如:

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"