PowerShell@2 - PowerShell v2 任务

使用此任务在 Linux、macOS 或 Windows 上运行 PowerShell 脚本。

注释

默认情况下,PowerShell v2 使用适用于 Linux 代理的 PowerShell Core 和适用于 Windows 代理的 Windows PowerShell。 若要在 Windows 代理上使用最新版本的 PowerShell,请将 pwsh 参数设置为 true。 然后,将改用 PowerShell Core。

语法

# PowerShell v2
# Run a PowerShell script on Linux, macOS, or Windows.
- task: PowerShell@2
  inputs:
    #targetType: 'filePath' # 'filePath' | 'inline'. Type. Default: filePath.
    filePath: # string. Required when targetType = filePath. Script Path. 
    #arguments: # string. Optional. Use when targetType = filePath. Arguments. 
    #script: # string. Required when targetType = inline. Script. 
  # Preference Variables
    #errorActionPreference: 'stop' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. ErrorActionPreference. Default: stop.
    #warningPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. WarningPreference. Default: default.
    #informationPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. InformationPreference. Default: default.
    #verbosePreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. VerbosePreference. Default: default.
    #debugPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. DebugPreference. Default: default.
    #progressPreference: 'silentlyContinue' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. ProgressPreference. Default: silentlyContinue.
  # Advanced
    #failOnStderr: false # boolean. Fail on Standard Error. Default: false.
    #showWarnings: false # boolean. Show warnings as Azure DevOps warnings. Default: false.
    #ignoreLASTEXITCODE: false # boolean. Ignore $LASTEXITCODE. Default: false.
    #pwsh: false # boolean. Use PowerShell Core. Default: false.
    #workingDirectory: # string. Working Directory. 
    #runScriptInSeparateScope: false # boolean. Run script in the separate scope. Default: false.
# PowerShell v2
# Run a PowerShell script on Linux, macOS, or Windows.
- task: PowerShell@2
  inputs:
    #targetType: 'filePath' # 'filePath' | 'inline'. Type. Default: filePath.
    filePath: # string. Required when targetType = filePath. Script Path. 
    #arguments: # string. Optional. Use when targetType = filePath. Arguments. 
    #script: # string. Required when targetType = inline. Script. 
  # Preference Variables
    #errorActionPreference: 'stop' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. ErrorActionPreference. Default: stop.
    #warningPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. WarningPreference. Default: default.
    #informationPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. InformationPreference. Default: default.
    #verbosePreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. VerbosePreference. Default: default.
    #debugPreference: 'default' # 'default' | 'stop' | 'continue' | 'silentlyContinue'. DebugPreference. Default: default.
  # Advanced
    #failOnStderr: false # boolean. Fail on Standard Error. Default: false.
    #showWarnings: false # boolean. Show warnings as Azure DevOps warnings. Default: false.
    #ignoreLASTEXITCODE: false # boolean. Ignore $LASTEXITCODE. Default: false.
    #pwsh: false # boolean. Use PowerShell Core. Default: false.
    #workingDirectory: # string. Working Directory. 
    #runScriptInSeparateScope: false # boolean. Run script in the separate scope. Default: false.
# PowerShell v2
# Run a PowerShell script on Linux, macOS, or Windows.
- task: PowerShell@2
  inputs:
    #targetType: 'filePath' # 'filePath' | 'inline'. Type. Default: filePath.
    filePath: # string. Required when targetType = filePath. Script Path. 
    #arguments: # string. Optional. Use when targetType = filePath. Arguments. 
    #script: # string. Required when targetType = inline. Script. 
    #errorActionPreference: 'stop' # 'stop' | 'continue' | 'silentlyContinue'. ErrorActionPreference. Default: stop.
  # Advanced
    #failOnStderr: false # boolean. Fail on Standard Error. Default: false.
    #showWarnings: false # boolean. Show warnings as Azure DevOps warnings. Default: false.
    #ignoreLASTEXITCODE: false # boolean. Ignore $LASTEXITCODE. Default: false.
    #pwsh: false # boolean. Use PowerShell Core. Default: false.
    #workingDirectory: # string. Working Directory.
# PowerShell v2
# Run a PowerShell script on Linux, macOS, or Windows.
- task: PowerShell@2
  inputs:
    #targetType: 'filePath' # 'filePath' | 'inline'. Type. Default: filePath.
    filePath: # string. Required when targetType = filePath. Script Path. 
    #arguments: # string. Optional. Use when targetType = filePath. Arguments. 
    #script: # string. Required when targetType = inline. Script. 
    #errorActionPreference: 'stop' # 'stop' | 'continue' | 'silentlyContinue'. ErrorActionPreference. Default: stop.
  # Advanced
    #failOnStderr: false # boolean. Fail on Standard Error. Default: false.
    #ignoreLASTEXITCODE: false # boolean. Ignore $LASTEXITCODE. Default: false.
    #pwsh: false # boolean. Use PowerShell Core. Default: false.
    #workingDirectory: # string. Working Directory.

输入

targetType - 类型
string。 允许的值:filePath(文件路径),inline。 默认值:filePath

指定要运行的任务的脚本类型:内联脚本或 .ps1 文件的路径。


filePath - 脚本路径
stringtargetType = filePath时是必需的。

指定要执行的脚本的路径。 必须是完全限定的路径或相对于 $(System.DefaultWorkingDirectory)


arguments - 参数
string。 可选。 当 targetType = filePath时使用。

指定传递给 PowerShell 脚本的参数。 参数可以是序号参数或命名参数。 例如,-Name someName -Path -Value "Some long string value"

targetType 设置为 inline时,不使用 arguments


script - 脚本
stringtargetType = inline时是必需的。 默认值:# Write your PowerShell commands here.\n\nWrite-Host "Hello World"

指定脚本的内容。 支持的最大内联脚本长度为 20000 个字符。 如果要使用较长的脚本,请使用文件中的脚本。


errorActionPreference - ErrorActionPreference
string。 允许的值:defaultstopcontinuesilentlyContinue。 默认值:stop

将脚本顶部 $ErrorActionPreference = 'VALUE' 行前面。


errorActionPreference - ErrorActionPreference
string。 允许的值:stopcontinuesilentlyContinue。 默认值:stop

将脚本顶部 $ErrorActionPreference = 'VALUE' 行前面。


warningPreference - WarningPreference
string。 允许的值:defaultstopcontinuesilentlyContinue。 默认值:default

如果未设置为 Default,请在脚本顶部 $WarningPreference = 'VALUE' 前面添加行。


informationPreference - InformationPreference
string。 允许的值:defaultstopcontinuesilentlyContinue。 默认值:default

如果未设置为 Default,请在脚本顶部 $InformationPreference = 'VALUE' 前面添加行。


verbosePreference - VerbosePreference
string。 允许的值:defaultstopcontinuesilentlyContinue。 默认值:default

如果未设置为 Default,请在脚本顶部 $VerbosePreference = 'VALUE' 前面添加行。


debugPreference - DebugPreference
string。 允许的值:defaultstopcontinuesilentlyContinue。 默认值:default

如果未设置为 Default,请在脚本顶部 $DebugPreference = 'VALUE' 前面添加行。


progressPreference - ProgressPreference
string。 允许的值:defaultstopcontinuesilentlyContinue。 默认值:silentlyContinue

如果未设置为 Default,请在脚本顶部 $ProgressPreference = 'VALUE' 前面添加行。


标准错误failOnStderr - 失败
boolean。 默认值:false

如果此布尔值 true,则任何错误写入错误管道或写入标准错误流的任何数据时,任务将失败。 否则,任务依赖于退出代码来确定失败。


showWarnings - 将警告显示为 Azure DevOps 警告
boolean。 默认值:false

如果该值设置为 true,并且脚本将写入警告,则警告将在管道日志中显示为警告。


ignoreLASTEXITCODE - 忽略$LASTEXITCODE
boolean。 默认值:false

如果该值设置为 false,则 if ((Test-Path -LiteralPath variable:\LASTEXITCODE)) { exit $LASTEXITCODE } 行将追加到脚本的末尾。 这将导致外部命令中的最后一个退出代码作为 powershell的退出代码传播。 否则,该行不会追加到脚本的末尾。


pwsh - 使用 PowerShell Core
boolean。 默认值:false

如果这是真的,则 Windows 代理上运行的任务将使用路径中的 pwsh.exe 而不是 powershell.exe


workingDirectory - 工作目录
string

指定运行脚本的工作目录。 如果未指定值,则工作目录 $(Build.SourcesDirectory)


runScriptInSeparateScope - 在单独的作用域中运行脚本
boolean。 默认值:false

此输入允许使用 & 运算符而不是默认 .执行 PowerShell 脚本。 如果此输入设置为 true,脚本将在单独的作用域中执行,并且不会更新全局范围的 PowerShell 变量。


任务控制选项

除任务输入之外,所有任务都具有控制选项。 有关详细信息,请参阅 控件选项和常见任务属性

输出变量

没有。

注解

每个 PowerShell 会话仅在运行作业的持续时间内持续。 依赖于已启动的任务必须与启动作业位于同一个作业中。

任务快捷方式

PowerShell@2 YAML 中具有两个快捷方式:steps.powershellsteps.pwsh

  • powershell 使用 Windows PowerShell(在 Windows 上)或 pwsh(Linux 和 macOS)运行。
  • pwsh 运行 PowerShell Core,这是基于 .NET Core 构建的跨平台版本的 PowerShell。
steps:
- powershell: # Run a script in Windows PowerShell on Windows, and pwsh on Linux and macOS.
- pwsh: # Run a script in PowerShell Core on Windows, macOS, and Linux.

设置变量,以便可由后续脚本和任务读取它

若要详细了解如何在脚本中定义生成变量,请参阅 定义和修改脚本中的生成变量

若要详细了解如何在脚本中定义发布变量,请参阅 定义和修改脚本中的发布变量。

在脚本中传递管道机密,但管道日志中未屏蔽机密

请注意,PowerShell 会切断错误消息,因此,如果在脚本中使用管道机密,则可以剪裁和公开机密。 例如,在下面的内联脚本中:

./script.ps1 --arg1 value1 --arg2 <some_secret_which_will_be_masked_here>

可能存在如下异常:At <path_to_temp_script_file>:4 char:3

+   ./script.ps1 --arg1 value1 --arg2 <unmasked_part_of_original_secret> ...
+   ~~~~~~~~~~
    + <Additional exception details>

若要避免此问题,可以在脚本级别处理这些异常,或者避免在错误消息中的源代码行中出现管道机密时出现的情况。

例子

从文件调用脚本

下面是一个名为 test.ps1 的示例 PowerShell 文件,该文件位于存储库的根目录中。

Write-Host "Hello World from $Env:AGENT_NAME."
Write-Host "My ID is $Env:AGENT_ID."
Write-Host "AGENT_WORKFOLDER contents:"
gci $Env:AGENT_WORKFOLDER
Write-Host "AGENT_BUILDDIRECTORY contents:"
gci $Env:AGENT_BUILDDIRECTORY
Write-Host "BUILD_SOURCESDIRECTORY contents:"
gci $Env:BUILD_SOURCESDIRECTORY
Write-Host "Over and out."

可以在管道中调用此脚本,如下所示。

steps:
- task: PowerShell@2
  inputs:
    targetType: 'filePath'
    filePath: 'test.ps1'

编写警告

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: Write-Host "##vso[task.LogIssue type=warning;]This is the warning"
    # Writes a warning to build summary and to log in yellow text

编写错误

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: Write-Host "##vso[task.LogIssue type=error;]This is the error"
    # Writes an error to build summary and to log in red text

如果希望此错误无法生成,请将 exit 1 添加到脚本。

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Write-Host "##vso[task.LogIssue type=error;]This is the error"
      exit 1
    # Writes an error to build summary and to log in red text

使用多个参数调用 PowerShell 脚本

创建 PowerShell 脚本 test2.ps1

param ($input1, $input2)
Write-Host "$input1 $input2"

在 YAML 管道中,调用:

- task: PowerShell@2
  inputs:
    targetType: 'filePath'
    filePath: $(System.DefaultWorkingDirectory)\test2.ps1
    arguments: > # Use this to avoid newline characters in multiline string
      -input1 "Hello"
      -input2 "World"
  displayName: 'Print Hello World'

要求

要求 说明
管道类型 YAML,经典版本,经典版本
运行时间 代理,DeploymentGroup
需求 没有
功能 此任务不满足作业中后续任务的任何要求。
命令限制 任何
Settable 变量 任何
代理版本 2.115.0 或更高版本
任务类别 效用

另请参阅