Powershell try block not working on VSTS build

核能气质少年 提交于 2020-12-31 10:53:33

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/46060042/powershell-try-block-not-working-on-vsts-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!