AzurePowerShell@5 - Azure PowerShell v5 任务

使用此任务可在 Azure 环境中运行 PowerShell 脚本。 使用提供的 Azure 资源管理器服务连接对 Azure 上下文进行身份验证。

注释

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

语法

# Azure PowerShell v5
# Run a PowerShell script within an Azure environment.
- task: AzurePowerShell@5
  inputs:
    azureSubscription: # string. Alias: ConnectedServiceNameARM. Required. Azure Subscription. 
    #ScriptType: 'FilePath' # 'FilePath' | 'InlineScript'. Script Type. Default: FilePath.
    #ScriptPath: # string. Optional. Use when ScriptType = FilePath. Script Path. 
    #Inline: # string. Optional. Use when ScriptType = InlineScript. Inline Script. 
    #ScriptArguments: # string. Optional. Use when ScriptType = FilePath. Script Arguments. 
    #errorActionPreference: 'stop' # 'stop' | 'continue' | 'silentlyContinue'. ErrorActionPreference. Default: stop.
    #FailOnStandardError: false # boolean. Fail on Standard Error. Default: false.
  # Azure PowerShell version options
    #azurePowerShellVersion: 'OtherVersion' # 'LatestVersion' | 'OtherVersion'. Alias: TargetAzurePs. Azure PowerShell Version. Default: OtherVersion.
    preferredAzurePowerShellVersion: # string. Alias: CustomTargetAzurePs. Required when TargetAzurePs = OtherVersion. Preferred Azure PowerShell Version. 
  # Advanced
    #pwsh: false # boolean. Use PowerShell Core. Default: false.
    #validateScriptSignature: false # boolean. Optional. Use when ScriptType = FilePath. Validate script signature. Default: false.
    #workingDirectory: # string. Working Directory.
# Azure PowerShell v5
# Run a PowerShell script within an Azure environment.
- task: AzurePowerShell@5
  inputs:
    azureSubscription: # string. Alias: ConnectedServiceNameARM. Required. Azure Subscription. 
    #ScriptType: 'FilePath' # 'FilePath' | 'InlineScript'. Script Type. Default: FilePath.
    #ScriptPath: # string. Optional. Use when ScriptType = FilePath. Script Path. 
    #Inline: # string. Optional. Use when ScriptType = InlineScript. Inline Script. 
    #ScriptArguments: # string. Optional. Use when ScriptType = FilePath. Script Arguments. 
    #errorActionPreference: 'stop' # 'stop' | 'continue' | 'silentlyContinue'. ErrorActionPreference. Default: stop.
    #FailOnStandardError: false # boolean. Fail on Standard Error. Default: false.
  # Azure PowerShell version options
    #azurePowerShellVersion: 'OtherVersion' # 'LatestVersion' | 'OtherVersion'. Alias: TargetAzurePs. Azure PowerShell Version. Default: OtherVersion.
    preferredAzurePowerShellVersion: # string. Alias: CustomTargetAzurePs. Required when TargetAzurePs = OtherVersion. Preferred Azure PowerShell Version. 
  # Advanced
    #pwsh: false # boolean. Use PowerShell Core. Default: false.
    #workingDirectory: # string. Working Directory.

输入

azureSubscription - Azure 订阅
输入别名:ConnectedServiceNameARMstring。 必填。

运行 PowerShell 之前要配置的 Azure 资源管理器订阅。

可以使用模板表达式来指定服务连接输入。 在以下示例中,azureSubscription 使用格式字符串和基于 environmentName 变量的表达式创建。

pool:
  vmImage: ubuntu-latest

variables:
  # Format string for the service connection
  azureSubscriptionFormat: 'connectionString-{0}-001'

stages:
- stage: Prepare
  variables:
    environmentName: 'test'
    # Stage level variable with the service connection name
    # Evaluates to conenctionString-test-001
    azureSubscription: ${{ format(variables.azureSubscriptionFormat, variables.environmentName) }}

  jobs:
  - job: RunStuff
    steps:
    - task: AzureCLI@2
      inputs:
        # Set this input to the computed value
        azureSubscription: ${{ variables.azureSubscription }}
        scriptType: bash
        scriptLocation: inlineScript
        inlineScript: 'echo Hello ${{ variables.azureSubscription }}'

    - task: AzurePowerShell@5
      inputs:
        # Set this input to the computed value
        azureSubscription: ${{ variables.azureSubscription }}
        azurePowerShellVersion: 'LatestVersion'
        scriptType: 'InlineScript'
        inline: Write-Host "Hello ${{ variables.azureSubscription }}"

ScriptType - 脚本类型
string。 允许的值:FilePath(脚本文件路径)、InlineScript(内联脚本)。 默认值:FilePath

脚本的类型:文件路径或内联。


ScriptPath - 脚本路径
string。 可选。 当 ScriptType = FilePath时使用。

脚本的路径。 这应该是完全限定的路径或相对于默认工作目录的路径。


Inline - 内联脚本
string。 可选。 当 ScriptType = InlineScript时使用。 默认值:# You can write your azure powershell scripts inline here. \n# You can also pass predefined and custom variables to this script using arguments

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


ScriptArguments - 脚本参数
string。 可选。 当 ScriptType = FilePath时使用。

要传递给 PowerShell 的其他参数。 这些参数可以是序号参数,也可以是命名参数。 不适用于内联脚本选项。


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

选择用于执行脚本的 ErrorActionPreference 变量的值。


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

如果为 true,则如果向错误管道写入任何错误,或者将任何数据写入标准错误流,则此任务将失败。


azurePowerShellVersion - Azure PowerShell 版本
输入别名:TargetAzurePsstring。 允许的值:LatestVersion(最新版本),OtherVersion(指定其他版本)。 默认值:OtherVersion

对于托管代理,支持的 Azure PowerShell 版本 1.0.01.6.02.3.22.6.03.1.0(托管 VS2017 队列)。 若要选择代理上可用的最新版本,请选择 LatestVersion(最新安装的版本)。

对于专用代理,可以使用 OtherVersion(指定其他版本)指定首选版本的 Azure PowerShell。


preferredAzurePowerShellVersion - 首选 Azure PowerShell 版本
输入别名:CustomTargetAzurePsstringTargetAzurePs = OtherVersion时是必需的。

首选的 Azure PowerShell 版本需要是适当的语义版本,例如。 1.2.3。 不支持正则表达式(如 2.\*,2.3.\*)。 托管 VS2017 池目前支持 Az 模块版本 1.0.01.6.02.3.22.6.03.1.0


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

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


validateScriptSignature - 验证脚本签名
boolean。 可选。 当 ScriptType = FilePath时使用。 默认值:false

如果为 true,则任务将首先检查以确保指定的脚本已签名并有效,然后再执行它。


workingDirectory - 工作目录
string

运行脚本的工作目录。


任务控制选项

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

输出变量

没有。

注解

故障排除

脚本在本地工作,但在管道中失败

通常,当管道中使用的服务连接没有足够的权限来运行脚本时,就会发生这种情况。 脚本在本地使用凭据运行时会成功,因为你可能具有所需的访问权限。

若要解决此问题,请确保服务主体/身份验证凭据具有所需的权限。 有关详细信息,请参阅使用基于角色的访问控制来管理对 Azure 订阅资源的访问

错误:找不到模块:“<模块名称>”,版本:“<版本>”。 如果模块最近安装,请在重启 Azure Pipelines 任务代理后重试

Azure PowerShell 任务使用 Azure/AzureRM/Az PowerShell 模块与 Azure 订阅交互。 当 PowerShell 模块在托管代理上不可用时,会出现此问题。 因此,对于特定任务版本,首选 Azure PowerShell 版本 必须在可用版本的列表中 Azure PowerShell 版本选项 指定。 可以在 Microsoft 托管代理Software 表中找到已安装的软件。

服务连接问题

若要排查与服务连接相关的问题,请参阅 服务连接故障排除

例子

以下示例演示如何从文件调用脚本并将脚本参数传递给该文件。

- task: AzurePowerShell@5
  inputs:
    azureSubscription: my-arm-service-connection
    scriptType: filePath
    scriptPath: $(Build.SourcesDirectory)\myscript.ps1
    scriptArguments:
      -Arg1 val1 `
      -Arg2 val2 `
      -Arg3 val3
    azurePowerShellVersion: latestVersion
    pwsh: true

以下参数演示如何调用内联脚本。

- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'Azure subscription connection placeholder'
    azurePowerShellVersion: LatestVersion
    ScriptType: 'InlineScript'
    Inline: |
      # You can write your azure powershell scripts inline here. 
      # You can also pass predefined and custom variables to this script using arguments
      Write-Host 'Hello'
      Write-Host 'World!'

要求

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