2012-03-14 47 views
1

大家好我正在研究一個腳本來比較多個服務器之間的補丁級別列表,並顯示在任一服務器上缺失的補丁列表。該腳本應陣列中的每個服務器之間的比較,並給輸出使用Get-Hotfix我試圖和同樣使用compare-object進行比較,並獲得服務器名評估$_.sideindicator -match "=>"$_.sideindicator -match "<="。 任何人都可以請進一步幫忙嗎?比較多個服務器上的補丁級別

這裏的代碼到現在的四臺服務器,如果有N個服務器的數量我想如何進行的邏輯。

[email protected]() 
$serd1 = Get-HotFix -ComputerName serd1 | select -ExpandProperty hotfixid 
$serd2 = Get-HotFix -ComputerName serd2 | select -ExpandProperty hotfixid 
$serd3 = Get-HotFix -ComputerName serd3 | select -ExpandProperty hotfixid 
$serd4 = Get-HotFix -ComputerName serd4 | select -ExpandProperty hotfixid 

$check1 = Compare-Object -ReferenceObject $serd1 -DifferenceObject $serd2 -IncludeEqual | ?{$_.sideindicator -notmatch '=='} 
$array += $check1 | ?{$_.sideindicator -match "=>"} | Select-Object @{l="HostName";e={"serd1"}},@{l="MissingPatches";e={$_.inputobject}} 
$array += $check1 | ?{$_.sideindicator -match "<="} | Select-Object @{l="HostName";e={"serd2"}},@{l="MissingPatches";e={$_.inputobject}} 

$check2 = Compare-Object -ReferenceObject $serd1 -DifferenceObject $serd3 -IncludeEqual | ?{$_.sideindicator -notmatch '=='} 
$array += $check2 | ?{$_.sideindicator -match "=>"} | Select-Object @{l="HostName";e={"serd1"}},@{l="MissingPatches";e={$_.inputobject}} 
$array += $check2 | ?{$_.sideindicator -match "<="} | Select-Object @{l="HostName";e={"serd3"}},@{l="MissingPatches";e={$_.inputobject}} 

$check3 = Compare-Object -ReferenceObject $serd1 -DifferenceObject $serd4 -IncludeEqual | ?{$_.sideindicator -notmatch '=='} 
$array += $check3 | ?{$_.sideindicator -match "=>"} | Select-Object @{l="HostName";e={"serd1"}},@{l="MissingPatches";e={$_.inputobject}} 
$array += $check3 | ?{$_.sideindicator -match "<="} | Select-Object @{l="HostName";e={"serd4"}},@{l="MissingPatches";e={$_.inputobject}} 

$check4 = Compare-Object -ReferenceObject $serd2 -DifferenceObject $serd3 -IncludeEqual | ?{$_.sideindicator -notmatch '=='} 
$array += $check4 | ?{$_.sideindicator -match "=>"} | Select-Object @{l="HostName";e={"serd2"}},@{l="MissingPatches";e={$_.inputobject}} 
$array += $check4 | ?{$_.sideindicator -match "<="} | Select-Object @{l="HostName";e={"serd3"}},@{l="MissingPatches";e={$_.inputobject}} 

$check5 = Compare-Object -ReferenceObject $serd2 -DifferenceObject $serd4 -IncludeEqual | ?{$_.sideindicator -notmatch '=='} 
$array += $check5 | ?{$_.sideindicator -match "=>"} | Select-Object @{l="HostName";e={"serd2"}},@{l="MissingPatches";e={$_.inputobject}} 
$array += $check5 | ?{$_.sideindicator -match "<="} | Select-Object @{l="HostName";e={"serd4"}},@{l="MissingPatches";e={$_.inputobject}} 

$check6 = Compare-Object -ReferenceObject $serd3 -DifferenceObject $serd4 -IncludeEqual | ?{$_.sideindicator -notmatch '=='} 
$array += $check6 | ?{$_.sideindicator -match "=>"} | Select-Object @{l="HostName";e={"serd3"}},@{l="MissingPatches";e={$_.inputobject}} 
$array += $check6 | ?{$_.sideindicator -match "<="} | Select-Object @{l="HostName";e={"serd4"}},@{l="MissingPatches";e={$_.inputobject}} 

$array 

的問題是如何使服務器

+1

發佈您到目前爲止所做的事情。關於你所做的更多細節有助於社區制定答案。 – 2012-03-14 07:30:47

+0

我看到你的問題縮小到2個你想比較的對象數組。只是發佈2個樣本數組和代碼,你試圖競選這些數組 – 2012-03-14 07:43:53

+0

是的,這是正確的,爲比較4個服務器我可以使用腳本,我想修改一次服務器的數量是模式,如果我有40個服務器,我想要一個邏輯自動處理,而不是複製粘貼代碼40次,以獲得服務器上缺少的補丁列表 – PowerShell 2012-03-14 10:23:38

回答

-1

的隨機數,這個腳本工作這應該工作:

$servers = "one", "two", "three" 

$array = @() 
for ($i = 0; $i -lt ($servers.Count - 1); $i++) 
{ 
    $serd1 = Get-HotFix -ComputerName $servers[$i] | select -ExpandProperty hotfixid 
    $serd2 = Get-HotFix -ComputerName $servers[$i+1] | select -ExpandProperty hotfixid 
    $array += $check1 | ?{$_.sideindicator -match "=>"} | Select-Object @{l="HostName";e={"serd1"}},@{l="MissingPatches";e={$_.inputobject}} 
    $array += $check1 | ?{$_.sideindicator -match "<="} | Select-Object @{l="HostName";e={"serd2"}},@{l="MissingPatches";e={$_.inputobject}} 
} 
$array 
0

這個問題是舊的,但認爲這可能幫助了,如果別人發現這並認爲這是他們想要放棄的東西。

總之,要滿足這裏規定的要求,看看這有助於 - 讓我知道,如果有人發現問題:

$Servers = "SERVER1", "SERVER2", "SERVER3", "SERVER4" 

[email protected]() 
$ServerPatches = New-Object 'object[]' $($Servers.Length) 
for($i=0; $i -lt $($Servers.Length); $i++) 
{ 
    $ServerPatches[$i] = Get-HotFix -ComputerName $Servers[$i] | select -ExpandProperty hotfixid 
} 
for($i=0; $i -lt $($Servers.Length); $i++) 
{ 
    for($j=($i+1); $j -lt $($Servers.Length); $j++) 
    { 
     $Compare = Compare-Object -ReferenceObject $ServerPatches[$i] -DifferenceObject $ServerPatches[$j] -IncludeEqual | ?{$_.sideindicator -notmatch '=='} 
     $MissingPatches += $Compare | ?{$_.sideindicator -match "=>"} | Select-Object @{l="HostName";e={$Servers[$i]}},@{l="MissingPatches";e={$_.inputobject}} 
     $MissingPatches += $Compare | ?{$_.sideindicator -match "<="} | Select-Object @{l="HostName";e={$Servers[$j]}},@{l="MissingPatches";e={$_.inputobject}} 
    } 
} 

$MissingPatches = foreach($MissingPatch in $MissingPatches) { 
    if($($MissingPatch.MissingPatches) -ne "File 1") 
     { 
      $MissingPatch 
     } 
    } 

$MissingPatches | ft -AutoSize 

走這條路,我開始的潛在的改進思想。因此,如果有時間,我可能會重寫並創建一個修補程序對象,該對象指示它存在於哪個服務器上,以及它缺少的位置,以瞭解修補程序爲何顯示爲「丟失」的更多詳細信息。

當在任何服務器上找到修補程序時,它將被添加到該陣列中。 PresentOn的一個屬性將被更新爲找到它的服務器名稱,並且到目前爲止不包含該修補程序的服務器可以被添加到MissingOn的屬性中,然後繼續向前檢查。然後,可以通過這些狀態顯示所有修補程序的表格,而只打印出「缺失」數組。可以採取一些行動,但可能值得這項工作。我也看到這是一個潛在的昂貴腳本,基於遊戲和服務器中的補丁數量。但是,如果其他人首先得到它(或在方法中發現缺陷),請讓我知道!