Powershell tr​​y块不适用于VSTS构建 [英] Powershell try block not working on VSTS build

查看:40
本文介绍了Powershell tr​​y块不适用于VSTS构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过VSTS在Powershell中运行命令以影响Azure.该命令有效,但之后会出现错误.该组已从命令获得许可这一事实证明了这些参数和连接正在工作.我认为一种解决方法是将命令放入try块中,使其运行,然后在出现错误时转到catch块并结束而不抛出错误.

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.

运行此脚本时,仍然会遇到相同的错误,例如try块将被忽略.我的语法有误吗?

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"
}

我在try-block之前添加了 $ ErrorActionPreference ="Stop" ,它正确地捕获了错误.

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

推荐答案

由于错误的 $ ErrorActionPreference ,try块被忽略.将其从继续"更改为停止"即可解决.

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

这篇关于Powershell tr​​y块不适用于VSTS构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆