[英]can you use IF statement in inline Azure Powershell script in Azure Devops?
I have a task in Azure Devops which builds a VM我在 Azure Devops 中有一个任务是构建一个 VM
Task 1 builds a VM Task 2 adds datadisks if required任务 1 构建 VM 任务 2 根据需要添加数据磁盘
so in task 2 I have an inline powershell script but when i run it it gives an error " The term 'n' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Not sure why it is giving the error? "所以在任务 2 中我有一个内联 powershell 脚本但是当我运行它时它给出了一个错误“术语‘n’不被识别为 cmdlet、function、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。不确定为什么会出现错误?”
###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
}
The substitution of AzDO variables in the script happens before PowerShell runs, and that means that if the inline script definition is:脚本中 AzDO 变量的替换发生在 PowerShell 运行之前,这意味着如果内联脚本定义是:
$powerShellVariable = $(azVariable)
and the value of the azVariable
variable in the task is "string", the resulting script passed to PowerShell will be:并且任务中
azVariable
变量的值为“字符串”,传递给 PowerShell 的结果脚本将是:
$powerShellVariable = string
PowerShell will interpret the bare word string
as a command name, hence the error. PowerShell 会将裸词
string
解释为命令名称,因此会出现错误。
Put quotation marks in place around the AzDO variable macro and it'll work:在 AzDO 变量宏周围加上引号,它就会起作用:
$datadisk1required = '$(datadisk1required)'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.