2014-10-31 94 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"