Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops Powershell尝试捕获继续_Loops_Powershell_Try Catch - Fatal编程技术网

Loops Powershell尝试捕获继续

Loops Powershell尝试捕获继续,loops,powershell,try-catch,Loops,Powershell,Try Catch,我有一个正在工作的powershell脚本,但当它找不到主机名时,它会抛出一个非终止异常,并向屏幕写入找不到该主机名的信息。我想捕捉这个异常,然后简单地在屏幕上写下:notfound。然后我想让脚本像平常一样继续 以下是脚本: $listOfComputers = IMPORT-CSV test.txt $b = "2013-09-11" ForEach($computer in $listOfComputers){ $name = $computer.Name Write-Host $name

我有一个正在工作的powershell脚本,但当它找不到主机名时,它会抛出一个非终止异常,并向屏幕写入找不到该主机名的信息。我想捕捉这个异常,然后简单地在屏幕上写下:notfound。然后我想让脚本像平常一样继续

以下是脚本:

$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} 
}
以下是输出:

。。。 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ +CategoryInfo:InvalidOperation:(:)[],运行时异常 +FullyQualifiedErrorId:InvokeMethodOnFull

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
的超时

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

您还可以在点击主循环之前批量测试连接性,从列表中筛选出任何无法访问的计算机。请参阅我的答案,以了解执行此操作的方法。

的可能重复项。有关
ErrorActionPreference
的答案将对您有所帮助。您可以为整个脚本或每个cmdlet设置首选项。我已尝试设置ErrorActionPreference。对我没有用,除非每次出错时我都能打印到屏幕上。使用ErrorActionPreference,我无法做到这一点……所以你做了一些类似于
尝试{get content“Herp”-ErrorAction stop}catch{“Derp”}
的事情,但没有给出你想要的行为?因为它绝对应该。你是个天才。谢谢你@alroc
$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;
    } 
}