【问题标题】:Powershell Try Catch ContinuePowershell Try Catch 继续
【发布时间】:2013-11-25 05:37:06
【问题描述】:

我有一个可用的 powershell 脚本,但是当它找不到主机名时,它会抛出一个非终止异常并在屏幕上写入无法找到该主机名的信息。我想捕获该异常并简单地写入屏幕:未找到。然后我希望脚本能正常运行。

这是脚本:

$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

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 does not seem to have an effect 的可能副本。有关ErrorActionPreference 的答案将对您有所帮助。您可以为整个脚本或每个 cmdlet 设置首选项。
  • 我已经尝试过设置 ErrorActionPreference。对我没用,除非每次发生错误时我都可以打印到屏幕上。使用 ErrorActionPreference,我无法做到这一点。
  • ...所以你做了类似try { get-content "Herp" -ErrorAction stop } catch { "Derp" } 的事情,但没有给出你想要的行为?因为它绝对应该。

标签: loops powershell try-catch


【解决方案1】:

完全跳过try/catch,而是在尝试访问注册表之前测试连接性。这应该会加快您的脚本速度,因为您不会在离线系统上等待来自OpenRemoteBaseKey 的超时。

$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;
    } 
}

您还可以在进入主循环之前批量测试连接性,从您的列表中过滤出任何无法访问的计算机。请参阅我的回答 over here 了解如何做到这一点。

【讨论】:

  • 你是个天才。谢谢@alroc
猜你喜欢
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
  • 2021-01-22
  • 2020-02-28
  • 1970-01-01
  • 1970-01-01
  • 2012-11-16
相关资源
最近更新 更多