-ErrorAction in AzureRM cmdlet

It seems that -ErrorAction is not working properly when running this script as an Azure PowerShell Script step in a project deployment:

function Get-TargetVMSS {
param (
[string] $ResourceGroupName,
[string] $VMSSName

# Check if VMSS exists
Write-Verbose "$ErrorActionPreference"
$VMSS = Get-AzureRmVmss -ResourceGroupName $ResourceGroupName -VMScaleSetName $VMSSName

Write-Output $VMSS


$targetVMSS = Get-TargetVMSS -ResourceGroupName “myresourcegroupname” -VMSSName “myvmssname” -ErrorAction SilentlyContinue

The expected result is that I will not see the error messages and the step will not fail. But alas it does output the error messages and the step fails.

Any ideas?

I am on OD 3.16.2

Hi Robert,

Thanks for reaching out and deep apologies for the late reply.

I brought this one to the team and it left us scratching our heads for a couple of minutes, until we found the reason of the different behavior between cmdlets:

It seems that sometimes Powershell “simply returns an error code” (insert more tech words here), and sometimes it throws full exceptions. The latter is not supported by -ErrorAction and it simply bypasses it (thus Octopus as well). This is the case for this Get-AzureRmVmss cmdlet.

We found that wrapping the cmdlet in a try/catch helps containing the exception at runtime

try {
Get-TargetVMSS -ResourceGroupName "myresourcegroupname" -VMSSName "myvmssname"
} catch {}

Hope that (still) helps!