When I use -ErrorVariable in a function I can’t get it to work.
I have this line:
$TestssFile = Get-AzSnapshot -ResourceGroupName $existingSnapshot -SnapshotName $existingSnapshotName -ErrorAction Stop -ErrorVariable errorMessage}
I will insert a value that I know is wrong, but it won’t write the error message. If I run the line by itself, I’ll get the -errorvariable without an issue but when it’s in a function, it won’t work
As a workaround, I’m using $Error.clear() to clear the automatic variable, and then I write $error to the screen and exit when I get an error, but I know that I shouldn’t. I’ve looked at plenty of examples, and any time I type at the terminal (using VScode) I can get the errorVariable. I’ve also tried using the built-in ISE, but it does not make a difference.