简体   繁体   English

Azure DevOps 条件无法评估 Powershell 设置变量

[英]Azure DevOps conditional can't evaluate Powershell set-variable

I have a powershell script, which is not in a job, task, or stage, it's on its own.我有一个 powershell 脚本,它不在工作、任务或阶段,它是独立的。 running in my DevOps build yaml like this:在我的 DevOps 构建 yaml 中运行,如下所示:

- powershell: |
   if (//something that's irrelevant) {
    Write-Host "##vso[task.setvariable variable=myCustomVar;isOutput=true]true"
   } else {
    Write-Host "##vso[task.setvariable variable=myCustomVar;isOutput=true]false"
   }

After this powershell script, I have another script to echo out myCustomVar to see what the value is.在这个 powershell 脚本之后,我有另一个脚本来回显 myCustomVar 以查看值是什么。 Like this:像这样:

- script: |
    echo "What is my custom variable?"
    echo $(myCustomVar)

When the build runs, in the devops logs, it echos literally "$(myCustomVar)" and not either True/False当构建运行时,在 devops 日志中,它会逐字地回显“$(myCustomVar)”,而不是 True/False

After that, I have a task which sends an email, but we only want to send an email if myCustomVar is true.之后,我有一个发送 email 的任务,但如果myCustomVar为真,我们只想发送 email。 So I use a conditional.所以我使用条件。

- task: SendEmail@1
  displayName: "Send email"
  condition: and(succeeded(), eq(variables.myCustomVar, true))

however, this is breaking.然而,这是打破。 I've tried a few other ways of doing it.我已经尝试了其他几种方法。 myCustomVar , on the task condition, always returns NULL. myCustomVar ,在任务条件下,总是返回 NULL。 Any help on syntax?语法上有什么帮助吗?

在此处输入图像描述

You could use ##vso[task.setvariable]value to set variables from a script in pipeline.您可以使用##vso[task.setvariable]value从管道中的脚本设置变量。 The first task can set a variable, and following tasks are able to use the variable.第一个任务可以设置一个变量,后面的任务可以使用这个变量。 The variable is exposed to the following tasks as an environment variable.该变量作为环境变量公开给以下任务。

Check following useful links:检查以下有用的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Azure API 管理中访问设置变量策略中的请求正文? - How can I access request body in set-variable policy in Azure API Management? 如何通过 PowerShell 脚本正确设置变量,然后使用 Azure DevOps 在另一个脚本中使用它? - How can I properly set a variable through a PowerShell script and then consume it in another using Azure DevOps? 如何将 Azure DevOps 的变量替换为 PowerShell 的变量? - How can instead of the variable of Azure DevOps to variable of PowerShell? 如何在 Azure Devops 发布管道中使用 PowerShell 设置环境变量? - How to set an environment variable with PowerShell in Azure Devops release pipeline? Azure devops 变量组在 powershell - Azure devops variable groups in powershell 无法在 Azure DevOps 管道“解析错误”中运行 PowerShell 脚本 - Can't run PowerShell script in Azure DevOps Pipeline 'parse error' 无法在 DevOps Release Pipeline 中使用 Azure CLI 设置变量 - Can't set variable using Azure CLI within DevOps Release Pipeline 将 Azure Powershell 变量分配给 DevOps 管道变量 - Assign Azure Powershell variable to DevOps Pipeline variable Azure Devops 构建管道中的条件变量分配 - Conditional Variable Assignment in Azure Devops Build Pipelines Azure DevOps 发布管道 Azure CLI Powershell 任务找不到 Z2F2D399F0EA80FE47B3459 - Azure DevOps Release pipeline Azure CLI Powershell task can't find powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM