PowerShell -在多个服务器上获取修复程序-错误处理

wecizke3  于 2023-03-30  发布在  Shell
关注(0)|答案(4)|浏览(138)

我有一个问题,我的脚本检查多个服务器上的多个修补程序。有时我没有一个rpc连接到一个服务器,在这种情况下,我想记录这个信息到同一个输出文件。有人能帮帮我吗?谢谢

$computers =  Get-Content -path C:\00-Scripts\printer\server.txt
$Patch = Get-Content -path C:\00-Scripts\printer\kb.txt
  
foreach ($computer in $computers) {
    foreach ($patch1 in $patch) {
        Try {
            if (get-hotfix -id $patch1 -ComputerName $computer -ErrorAction stop) {
                Add-content "$patch1 is Present in $computer" -path C:\00-Scripts\printer\Hotfix.txt
             }
             Else {
                 Add-content "$patch1 is not Present in $computer" -path C:\00-Scripts\printer\Hotfix.txt
             }
         }
         catch {
             Add-content "can not check $computer" -path C:\00-Scripts\printer\Hotfix.txt
         }
     }
}
x3naxklr

x3naxklr1#

在这种情况下,您需要首先检查计算机是否可以访问。如果可以,请循环查看补丁程序以报告是否可以找到它们。如果无法访问计算机,请在日志中仅写入一个故障行,然后继续下一台计算机。

$computers = Get-Content -path 'C:\00-Scripts\printer\server.txt'
$Patch     = Get-Content -path 'C:\00-Scripts\printer\kb.txt'
$logFile   = 'C:\00-Scripts\printer\Hotfix.txt'
foreach ($computer in $computers) {
    if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
        foreach ($patch1 in $patch) {
            # use -ErrorAction SilentlyContinue here so $hotfix will either become $null or an object
            $hotfix = Get-HotFix -Id $patch1 -ComputerName $computer -ErrorAction SilentlyContinue
            if ($hotfix) {
                "$patch1 is Present in $computer" | Add-Content -Path $logFile
            }
            else {
                "$patch1 is not Present in $computer" | Add-Content -Path $logFile
            }
        }
    }
    else {
        "Can not check $computer" | Add-Content -Path $logFile
    }
}
hpxqektj

hpxqektj2#

与上述答案,脚本是不是扫描下一台计算机和结果是不是得到生成的其余计算机
例如
Get-HotFix -ComputerName PC1,PC2 -ErrorAction SilentlyContinue
在上述命令中,如果PC 1不可用或访问被拒绝,则命令停止,不检查PC 2并生成输出

66bbxpm5

66bbxpm53#

我自己尝试了这个解决方案,它有点缺陷。我的目标是防止大错误消息显示在屏幕上。但是使用Get-Hotfix的选项总是会返回错误消息,即使你说默默地继续错误操作。至少它对我来说是这样的。特别是在尝试连接时。如果找到了操作系统,但补丁不在那里,那么静默继续按预期工作。相反,我以前对操作系统进行了检查,并且总是返回值或null。另外,上面使用$hotfix的方法只会工作一次,因为如果检查失败,该值不会被删除。我使用Write-Host $hotfix证明了这一点。这是我使用不同值的解决方案

$computers = Get-Content -path 'C:\00-Scripts\printer\server.txt'
$patch     = Get-Content -path 'C:\00-Scripts\printer\kb.txt'
$logFile   = 'C:\00-Scripts\printer\Hotfix.txt'
foreach ($computer in $computers) {
$ver = (Get-WmiObject -ComputerName $computer -class win32_OperatingSystem -ErrorAction SilentlyContinue).caption
if ($ver -ne $null) {
    foreach ($patch1 in $patch) {
        # use -ErrorAction SilentlyContinue here so $hotfix will either become $null or an object
        if (Get-HotFix -Id $patch1 -ComputerName $computer -ErrorAction SilentlyContinue) {
            "$patch1 is Present in $computer" | Add-Content -Path $logFile
        }
        else {
            "$patch1 is not Present in $computer" | Add-Content -Path $logFile
        }
    }
}
else {
    "Can not check $computer" | Add-Content -Path $logFile
}
}
ctehm74n

ctehm74n4#

我自己尝试了这个解决方案,它有点缺陷。我的目标是防止大错误消息显示在屏幕上。但是使用Get-Hotfix的选项总是会返回错误消息,即使你说默默地继续错误操作。至少它对我来说是这样的。特别是在尝试连接时。如果找到了操作系统,但补丁不在那里,那么静默继续按预期工作。相反,我以前对操作系统进行了检查,并且总是返回值或null。另外,上面使用$hotfix的方法只会工作一次,因为如果检查失败,该值不会被删除。我使用Write-Host $hotfix证明了这一点。这是我使用不同值的解决方案

$computers = Get-Content -path 'C:\00-Scripts\printer\server.txt'
$patch     = Get-Content -path 'C:\00-Scripts\printer\kb.txt'
$logFile   = 'C:\00-Scripts\printer\Hotfix.txt'
foreach ($computer in $computers) {
$ver = (Get-WmiObject -ComputerName $computer -class win32_OperatingSystem -ErrorAction SilentlyContinue).caption
if ($ver -ne $null) {
    foreach ($patch1 in $patch) {
        # use -ErrorAction SilentlyContinue here so $hotfix will either become $null or an object
        if (Get-HotFix -Id $patch1 -ComputerName $computer -ErrorAction SilentlyContinue) {
            "$patch1 is Present in $computer" | Add-Content -Path $logFile
        }
        else {
            "$patch1 is not Present in $computer" | Add-Content -Path $logFile
        }
    }
}
else {
    "Can not check $computer" | Add-Content -Path $logFile
}
}

相关问题