我有一个脚本,它将从文本文件中读取服务器名称,然后搜索特定的KB更新文件名,该文件工作正常。
但是如果我想在serverlist.txt
文件中搜索每个服务器搜索多个KB更新文件怎么办?我怎么能这样做?
$CheckComputers = get-content c:\temp\Path\serverlist.txt
# Define Hotfix to check
$CheckHotFixKB = "KB1234567";
foreach($CheckComputer in $CheckComputers)
{
$HotFixQuery = Get-HotFix -ComputerName $CheckComputer | Where-Object {$_.HotFixId -eq $CheckHotFixKB} | Select-Object -First 1;
if($HotFixQuery -eq $null)
{
Write-Host "Hotfix $CheckHotFixKB is not installed on $CheckComputer";
}
else
{
Write-Host "Hotfix $CheckHotFixKB was installed on $CheckComputer on by " $($HotFixQuery.InstalledBy);
}
}
答案 0 :(得分:1)
对于检查多个修补程序
,也许一个查询更好$NeededHotFixes = @('KB2670838','KB2726535','KB2729094','KB2786081','KB2834140')
Write-Host "Verify prerequisites hotfixes for IE11."
$InstalledHotFixes = (Get-HotFix).HotFixId
$NeededHotFixes | foreach {
if ($InstalledHotFixes -contains $_) {
Write-Host -fore Green "Hotfix $_ installed";
} else {
Write-Host -fore Red "Hotfix $_ missing";
}
}
享受; - )
答案 1 :(得分:0)
您需要在数组中设置KB:
$CheckHotFixKB = @(
"KB1234567"
"KB5555555"
"KB6666666"
"KB7777777"
)
然后做一个嵌套的foreach:
foreach($CheckComputer in $CheckComputers)
{
foreach ($hotfix in $CheckHotFixKB) {
$HotFixQuery = Get-HotFix -ComputerName $CheckComputer | Where-Object {$_.HotFixId -eq $hotfix} | Select-Object -First 1;
if($HotFixQuery -eq $null)
{
Write-Host "Hotfix $hotfix is not installed on $CheckComputer";
}
else
{
Write-Host "Hotfix $hotfix was installed on $CheckComputer on by " $($HotFixQuery.InstalledBy);
} }
}