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

标签 azure powershell azure-devops

我在 Azure Devops 中有一个构建 VM 的任务

任务 1 构建虚拟机 任务 2 根据需要添加数据磁盘

所以在任务 2 中我有一个内联 powershell 脚本,但是当我运行它时它给出了一个错误 "术语“n”未被识别为 cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。不确定为什么会出现错误? ”

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

最佳答案

脚本中 AzDO 变量的替换发生在 PowerShell 运行之前,这意味着如果内联脚本定义是:

$powerShellVariable = $(azVariable)

并且任务中 azVariable 变量的值为“string”,传递到 PowerShell 的结果脚本将是:

$powerShellVariable = string

PowerShell 会将裸词 string 解释为命令名称,因此会出现错误。

在 AzDO 变量宏周围加上引号,它将起作用:

$datadisk1required = '$(datadisk1required)'

关于azure - 您可以在 Azure Devops 的内联 Azure Powershell 脚本中使用 IF 语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69467136/

相关文章:

azure-devops - 使用 Azure AD token 通过 Azure DevOps 进行身份验证

node.js - Azure Pipelines 生成任务测试失败,错误为 : "Must initialize"

azure - 阻止 Spark HiveContext 连接到 Hive

powershell - 导出 CSV 文件中的多个对象 PowerShell

Powershell tr​​y/catch/finally 执行不正确(或者我已经完全处理它)

powershell - 运行多个查询并导出到不同的 CSV 文件

sql-server - 使用 VNET 进行 Azure 数据库部署管道部署

azure - 将 Azure SQL 数据库的备份还原到另一个资源组的另一个 Azure SQL 数据库

azure - SQL Azure 与表存储 - 带有 "contains"的 queryig 实体参数

azure - 通过 REST API 获取 Azure 应用服务计划的错误 CPU 使用数据