大家好我正在研究一個腳本來比較多個服務器之間的補丁級別列表,並顯示在任一服務器上缺失的補丁列表。該腳本應陣列中的每個服務器之間的比較,並給輸出使用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
的問題是如何使服務器
發佈您到目前爲止所做的事情。關於你所做的更多細節有助於社區制定答案。 – 2012-03-14 07:30:47
我看到你的問題縮小到2個你想比較的對象數組。只是發佈2個樣本數組和代碼,你試圖競選這些數組 – 2012-03-14 07:43:53
是的,這是正確的,爲比較4個服務器我可以使用腳本,我想修改一次服務器的數量是模式,如果我有40個服務器,我想要一個邏輯自動處理,而不是複製粘貼代碼40次,以獲得服務器上缺少的補丁列表 – PowerShell 2012-03-14 10:23:38