steps.powershell 定义

powershell步骤使用 Windows) 上的Windows PowerShell (或 pwsh linux 和 macOS) (运行脚本。

steps:
- powershell: string # Required as first property. Inline PowerShell script.
  errorActionPreference: string # Unless otherwise specified, the error action preference defaults to the value stop. See the following section for more information.
  failOnStderr: string # Fail the task if output is sent to Stderr?
  ignoreLASTEXITCODE: string # Check the final exit code of the script to determine whether the step succeeded?
  workingDirectory: string # Start the script with this working directory.
  condition: string # Evaluate this condition expression to determine whether to run this task.
  continueOnError: boolean # Continue running even on failure?
  displayName: string # Human-readable name for the task.
  target: string | target # Environment in which to run this task.
  enabled: boolean # Run this task when the job runs?
  env: # Variables to map into the process's environment.
    string: string # Name/value pairs
  name: string # ID of the step.
  timeoutInMinutes: string # Time to wait for this task to complete before the server kills it.
  retryCountOnTaskFailure: string # Number of retries if the task fails.
steps:
- powershell: string # Required as first property. Inline PowerShell script.
  errorActionPreference: string # Unless otherwise specified, the error action preference defaults to the value stop. See the following section for more information.
  failOnStderr: string # Fail the task if output is sent to Stderr?
  ignoreLASTEXITCODE: string # Check the final exit code of the script to determine whether the step succeeded?
  workingDirectory: string # Start the script with this working directory.
  condition: string # Evaluate this condition expression to determine whether to run this task.
  continueOnError: boolean # Continue running even on failure?
  displayName: string # Human-readable name for the task.
  target: string | target # Environment in which to run this task.
  enabled: boolean # Run this task when the job runs?
  env: # Variables to map into the process's environment.
    string: string # Name/value pairs
  name: string # ID of the step.
  timeoutInMinutes: string # Time to wait for this task to complete before the server kills it.
steps:
- powershell: string # Required as first property. Inline PowerShell script.
  errorActionPreference: string # Unless otherwise specified, the error action preference defaults to the value stop. See the following section for more information.
  failOnStderr: string # Fail the task if output is sent to Stderr?
  ignoreLASTEXITCODE: string # Check the final exit code of the script to determine whether the step succeeded?
  workingDirectory: string # Start the script with this working directory.
  condition: string # Evaluate this condition expression to determine whether to run this task.
  continueOnError: boolean # Continue running even on failure?
  displayName: string # Human-readable name for the task.
  enabled: boolean # Run this task when the job runs?
  env: # Variables to map into the process's environment.
    string: string # Name/value pairs
  name: string # ID of the step.
  timeoutInMinutes: string # Time to wait for this task to complete before the server kills it.

引用此定义的定义: 步骤

属性

powershell 字符串。 作为第一个属性是必需的。
内联 PowerShell 脚本。

errorActionPreference 字符串。
除非另行指定,否则错误操作首选项默认为值停止。 有关详细信息,请参阅下一节。

failOnStderr 字符串。
如果输出发送到 Stderr,则任务失败?

ignoreLASTEXITCODE 字符串。
检查脚本的最终退出代码以确定步骤是否成功?

workingDirectory 字符串。
使用此工作目录启动脚本。

condition 字符串。
计算此条件表达式以确定是否运行此任务。

continueOnError布尔值
即使发生故障,仍要继续运行?

displayName 字符串。
任务的用户可读名称。

targettarget
要在其中运行此任务的环境。

enabled布尔值
在作业运行时运行此任务?

env 字符串字典。
要映射到进程的环境的变量。

name 字符串。
步骤的 ID。 可接受的值:[-_A-Za-z0-9]*。

timeoutInMinutes 字符串。
在服务器终止此任务之前等待此任务完成的时间。

注意

管道可以配置作业级别超时。 如果作业级别超时间隔在步骤完成之前已过,则正在运行的作业 (包括步骤) 也会终止,即使该步骤配置了较长 timeoutInMinutes 的间隔也是如此。 有关详细信息,请参阅 超时

retryCountOnTaskFailure 字符串。
如果任务失败,则重试次数。

注解

powershell 关键字是 PowerShell 任务的另一个快捷方式。 该任务在 Windows) 上使用Windows PowerShell (或 pwsh (Linux 和 macOS) 运行脚本。

每个 PowerShell 会话的持续时间就是其运行时所在的作业的持续时间。 依赖于被启动项的任务必须与启动位于同一作业中。

详细了解条件超时

错误操作首选项

除非另行指定,否则错误操作首选项默认为 值 stop,并且行 $ErrorActionPreference = 'stop' 位于脚本顶部。

当错误操作首选项设置为停止时,错误会导致 PowerShell 终止任务并返回非零退出代码。 该任务还标记为“失败”。

errorActionPreference: stop | continue | silentlyContinue
steps:
- powershell: |
    Write-Error 'Uh oh, an error occurred'
    Write-Host 'Trying again...'
  displayName: Error action preference
  errorActionPreference: continue

忽略最后一个退出代码

默认情况下,检查从脚本返回的最后一个退出代码。 非零代码表示步骤失败,在这种情况下,系统会将脚本追加到:

if ((Test-Path -LiteralPath variable:\LASTEXITCODE)) { exit $LASTEXITCODE }

如果不需要此行为,请指定 ignoreLASTEXITCODE: true

ignoreLASTEXITCODE: boolean
steps:
- powershell: git nosuchcommand
  displayName: Ignore last exit code
  ignoreLASTEXITCODE: true

详细了解条件超时

示例

steps:
- powershell: Write-Host Hello $(name)
  displayName: Say hello
  name: firstStep
  workingDirectory: $(build.sourcesDirectory)
  failOnStderr: true
  env:
    name: Microsoft

另请参阅