3

I'm running a command in Powershell through VSTS to affect Azure. The command works, but it gives an error after. The params and connection are working as evidenced by the fact that the group gets permission from the command. I figure a workaround is to put the command in a try block, have it run, and then when the error comes up go to the catch block and end without throwing the error.

When I run this script, I still get the same error, like the try block is ignored. Do I have the syntax wrong?

Try
{
    New-AzureRmRoleAssignment -ObjectId "xxxx" -RoleDefinitionName $roleName -ResourceGroupName pentest-$featureName
}
Catch 
{
    Write-Output "Whoops"
}

Edit: I added $ErrorActionPreference = "Stop" before the try-block and it caught the error properly.

user3364161
  • 365
  • 5
  • 21
  • 3
    Check the value of `$ErrorActionPreference`. If it's not `Stop`, the `catch` won't run. Let us know if that's the issue, and I'll write up a more complete Answer. There is another case for some cmdlets that won't `catch` without more extreme steps. Explicitly adding the argument `-ErrorAction Stop` will achieve the desired result on a command by command basis. – Matthew Wetmore Sep 05 '17 at 17:21
  • I checked on the build server, it's continue – user3364161 Sep 05 '17 at 17:33

1 Answers1

0

The try block was ignored because of the wrong $ErrorActionPreference. Changing it from "Continue" to "Stop" fixed it.

user3364161
  • 365
  • 5
  • 21