Azure DevOps内联Azure PowerShell能否使用IF语句及报错咨询
报错原因分析
- 根本问题是Azure DevOps管道变量在注入内联PowerShell脚本时,没有被识别为字符串类型:你写的
$datadisk1required = $(datadisk1required)在管道运行时会被直接替换为$datadisk1required = n,PowerShell会把无包裹的n识别为待执行的命令/程序,而不是字符串值,所以直接抛出“术语'n'无法被识别”的报错。
修复方案
把赋值语句里的管道变量用双引号包裹,强制PowerShell将注入的变量值识别为字符串即可,修改后的脚本如下:
###Azure Devops pipeline variable is $(datadisk1required) this has the value 'n' $datadisk1required = "$(datadisk1required)" if ($datadisk1required -eq "n") { write-host "datadisk1 not required" write-host $datadisk1required }
验证说明
- 变量替换后最终执行的代码会变为
$datadisk1required = "n",符合PowerShell字符串赋值的语法要求,if分支判断也可以正常执行。
内容的提问来源于stack exchange,提问作者itye1970
相关产品推荐
相关产品推荐

