简体   繁体   中英

How do I prevent PowerShell from adding exception data to $error

I am invoking a lot of REST calls in a function. I know that some of them will fail, but that is expected.

My question is:

How do I prevent powershell from adding entries to the global $error variable?

foreach:

$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Ignore"
try {
    $response = Invoke-RestMethod -Uri $Uri -ea Ignore
} catch {
    Write-Verbose "$_"
} finally {
    $ErrorActionPreference = $oldErrorActionPreference
}

$error variable after invoking:

在此处输入图片说明

Invoke-RestMethod cmdlet always fails with terminating error , which can't be Ignored — it will always end up inside $Error list.

You can clear it: $Error.Clear()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM