跨多个服务器比较补丁级别

时间:2012-03-14 06:56:43

标签: arrays powershell

大家好我正在编写一个脚本来比较多个服务器之间的补丁级别列表,并显示任一服务器上缺少的补丁列表。该脚本应该在数组中的每个服务器之间进行比较,并使用Get-Hotfix给出我正在尝试的输出,并使用compare-object来比较并获取评估$_.sideindicator -match "=>"$_.sideindicator -match "<="的服务器名称。 有谁可以请进一步帮助?

这里是四个服务器的代码,如果有n个服务器,我想要了解如何继续的逻辑。

$array=@()
$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

问题是如何使这个脚本适用于随机数量的服务器

2 个答案:

答案 0 :(得分:0)

这个问题已经过时了,但是如果其他人发现这一点并认为这是他们想要放手的话,这可能会有所帮助。

无论如何,要满足此处所述的要求,请查看是否有帮助 - 如果有人发现问题,请告诉我们:

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

$MissingPatches=@()
$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

沿着这条路走下去,我开始考虑潜在的改进。所以,如果我有时间,我可能会重写并创建一个补丁对象,该对象指示它存在于哪个服务器上以及丢失的位置,以了解为什么补丁被指示为&#34;缺少&#34;的详细信息。

当在任何服务器上找到补丁时,它会被添加到该阵列中。 PresentOn的属性将使用它找到的服务器名称进行更新,到目前为止,不包含补丁的服务器可以添加到MissingOn的属性中,然后继续检查。然后,可以通过这些状态显示所有补丁的表格,而不是仅打印出#34; Missing&#34;阵列。可以采取一些做但可能值得的工作。我也看到这是一个基于游戏和服务器补丁数量的潜在代价高昂的脚本。但是,如果其他人首先(或发现方法中存在缺陷)让我知道这里!

答案 1 :(得分:-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