Powershell尝试捕获继续 [英] Powershell Try Catch Continue

查看:72
本文介绍了Powershell尝试捕获继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行中的Powershell脚本,但是当它找不到主机名时,它将引发一个非终止的异常,并在屏幕上写入无法找到该主机名的信息.我想捕获该异常,只需将其写到屏幕上:未找到.然后,我希望脚本像往常一样进行.

I have a working powershell script, but when it cannot find a hostname, it throws a non-terminating exception and writes to the screen that that hostname was unable to be found. I want to catch that exception and simply write to the screen: Not Found. Then I want the script to carry on like normal.

这是脚本:

$listOfComputers = IMPORT-CSV test.txt
$b = "2013-09-11"
ForEach($computer in $listOfComputers){
$name = $computer.Name
Write-Host $name -NoNewLine
try{$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $name)}
catch [Exception] {write-host "  Not Found" -foreground blue}
$key = $reg.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\Results\\Install")
$a = $key.GetValue("LastSuccessTime")
$a = $a.Substring(0,10)
if($a -le $b){Write-Host " " $a -foreground magenta}
else{Write-Host " " $a} 
}

以下是输出:

PS C:\PowerShell Scripts> .\windowsUpdates.ps1
OLDBEAR  Not Found
You cannot call a method on a null-valued expression.
At C:\PowerShell Scripts\windowsUpdates.ps1:8 char:1
+ $key =
$reg.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpd

...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo:InvalidOperation:(:) [],RuntimeException+ FullyQualifiedErrorId:InvokeMethodOnNull

... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\PowerShell Scripts\windowsUpdates.ps1:9 char:1
+ $a = $key.GetValue("LastSuccessTime")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\PowerShell Scripts\windowsUpdates.ps1:10 char:1
+ $a = $a.Substring(0,10)
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

感谢您的帮助.

推荐答案

完全跳过try/catch,而是在尝试访问注册表之前 进行连通性测试.这样可以加快脚本的速度,因为在离线系统上您无需等待 OpenRemoteBaseKey 的超时.

Skip the try/catch altogether, and instead test for connectivity before attempting to access the registry. This should speed your script up, as you won't be waiting for a timeout from OpenRemoteBaseKey on systems that are offline.

$listOfComputers = IMPORT-CSV test.txt
$b = "2013-09-11"
ForEach($computer in $listOfComputers){
    $name = $computer.Name
    Write-Host $name -NoNewLine
    if (-not (Test-Connection -computername $name -count 1 -Quiet -ErrorAction SilentlyContinue) {
        write-host "  Not Found" -foreground blue;
        continue;
    }
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $name)

    $key = $reg.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\Results\\Install")
    $a = $key.GetValue("LastSuccessTime")
    $a = $a.Substring(0,10)
    if($a -le $b) {
        Write-Host " " $a -foreground magenta
    }
    else {
        Write-Host " " $a;
    } 
}

您还可以在进入主循环之前批量测试连接,过滤列表中无法访问的所有计算机.查看我的答案在此处.

You could also test connectivity in bulk before hitting the main loop, filtering any computers that aren't accessible out of your list. See my answer over here for a way to do that.

这篇关于Powershell尝试捕获继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆