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.