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:

$Errorstore all the errors that have occurred in a Powershell session? It would be much safer to user$error[0]to look at the latest error, then slowly move up the array if required.