-ErrorVariable in functions is not working

When I use -ErrorVariable in a function I can’t get it to work.

I have this line:

Try {

$TestssFile = Get-AzSnapshot -ResourceGroupName $existingSnapshot -SnapshotName $existingSnapshotName -ErrorAction Stop -ErrorVariable errorMessage}

Catch {

Write $errorMessage

exit

}

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.

By default, variables created in a function are scoped to the function. They only last as long as the function hasn’t completed. Therefore, your variable is likely being created and assigned a value, it’s just that the variable no longer exists after the function is complete. The proof here, is to put Global: in front of the errorMessage variable (as Global:errorMessage), in order to see if the variable is populated after the function is complete. Do your best to not use globally scoped variables. If it were me, I would probably allow the error message to display; that’s kind of the idea with error messages. Alert now, not later…

Thank you for your response. I didn’t include everything, just to keep my question short. I previously tested using -ErrorVariable in a function and it worked fine. One test was just to try and get the process notepad, since it wasn’t running, and it would produce an error with the -ErrorVariable. I may have found my issue.

Specifically, I’m writing a function in Azure, and I’m using the new ‘Az’ module. I’m noticing that some cmdlets don’t produce an error, even when I’m entering a value that is false. I ran (Get-AzVirtualNetwork | where { $_.Name -eq “dumb” }) and the terminal, and nothing comes back. If I run the same command and -eq it to a real virtualNetwork it comes back with the information. So it seems as if some Az cmdlets are blank (for me), even when the input data is false. I don’t understand that at all.

 

 

The cmdlet isn’t blank. It’s just how you’re choosing to filter it. Where-Object doesn’t throw errors when it doesn’t find something; it just looks for that something and passes it along if it finds it.

If instead you used the Az cmdlet itself to do the filtering, many such cmdlets will error out if you ask for something that doesn’t exist. But once you pass its output off to Where-Object, it’s out of that cmdlet’s hands. You’ve retrieved all available data, and then Where-Object will act on the data it has. It’s a filter, not an assertion, so if you give it a whole bunch of things from the cmdlet that don’t match your query, it won’t give you anything back.

The problem that I’m having is that some Az cmdlets, i.e., Get-AzLocation. It does not have a good way to filter for the correct location (that I know of). There isn’t a -Location or -Name parameter that corresponds to a location, so the only way that I can think of to filter for a correct location is to use (Get-Azlocation | where { $_.Location -eq ‘westus’ }). If I use ‘westus3’ Get-AzLocation won’t error, so in essence isn’t the value empty/blank/$null?

I’m not saying the cmdlet is blank, if that’s how I wrote it.

I tried to make my question short, I didn’t want a long drawn out question that nobody would want to read, but over all, I’m placing a variable in front of everything, so $location = Get-AzLocation | where { $_.location -eq “somelocation” }. $location will not have a value, whether it’s a valid location or not, and my mistake was that I thought if the cmdlet doesn’t find a value, it would/should return an error, but that’s not the case. So I’m trying to filter for $null variables to validate everything before the script runs.

The script will create VMs, but I want it to stop if there is an error or a value is empty because it’s using a csv file, and if someone puts an extra space, then the VM won’t be created correctly, so I don’t want anything to run unless all values are valid.

For my previous example Get-AzVirtualNetwork, I would think that if I enter an invalid network name using where-object, I thought it would error, which is definitely a mistake on my part.

Thank you for all the input.

 

After looking again at what I did, I have no clue why I would think filtering with where-object would produce an error if it didn’t return any results. I feel foolish even thinking that.