简体   繁体   English

您可以在 Azure Azure Devops 的内联脚本中使用 IF 语句吗?

[英]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.

相关问题 如何在 Azure DevOps 上运行的 PowerShell 上登录 azure - How can I login azure on PowerShell that run on Azure DevOps 如何从 Powershell 脚本文件访问 Azure DevOps 预定义变量 - How to access Azure DevOps Predefined variables from Powershell script file Azure DevOps REST API PowerShell 脚本中的顶部参数不起作用 - Azure DevOps REST API top parameter in PowerShell script is not working 在 Azure Devops YML 管道脚本中使用 boolean 变量作为小写字符串 - Use boolean variable as lowercase string in Azure Devops YML pipeline script 在 Azure Devops 中使用 powershell 连接 AzureAD - Connect-AzureAD using powershell in Azure Devops 如何使用 PowerShell 创建 Azure Devops Wiki 子页面 - How to create Azure Devops Wiki SubPage with PowerShell 如何使用 Azure DevOps CI/CD 部署多个 Azure 函数 - How can I use Azure DevOps CI/CD to deploy multiple Azure Functions Azure DevOps 仪表板以内联运行 python 并将 output 显示为小部件 - Azure DevOps Dashboard to run inline python and show output as widget Azure devops - 在模板中使用运行时变量 - Azure devops - use runtime variables in template 在 Azure Powershell 任务中使用 Azure CLI - Use Azure CLI within Azure Powershell Task
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM