AzurePowerShell@5 - Azure PowerShell v5 工作

使用此工作在 Azure 環境中執行 PowerShell 腳本。 Azure 內容會使用提供的 Azure Resource Manager服務連線進行驗證。

注意

根據預設,Azure PowerShell v5 會使用適用于 Linux 的 PowerShell Core 代理程式和 Windows 代理程式Windows PowerShell。 若要在 Windows 代理程式上使用最新版本的 PowerShell,請將 pwsh 參數設定為 true 。 接著會改用 PowerShell Core。

Syntax

# 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 Resource Manager訂用帳戶。


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

如果有任何錯誤寫入錯誤管線,或將任何資料寫入標準錯誤資料流程,則此工作將會失敗。


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版本
輸入別名: CustomTargetAzurePsstring. 當 TargetAzurePs = OtherVersion 時為必要。

慣用Azure PowerShell版本必須是適當的語意版本,例如 1.2.3. 不支援類似 Regex 2.\*,2.3.\* 。 託管 VS2017 集區目前支援 Az 模組版本 1.0.01.6.02.3.22.6.03.1.0


pwsh - 使用 PowerShell Core
boolean. 預設值:false

如果這是 true,則 Windows 代理程式上執行的工作會從您的路徑使用 pwsh.exe ,而不是 powershell.exe


validateScriptSignature - 驗證腳本簽章
boolean. 選擇性。 使用 時 ScriptType = FilePath 。 預設值:false

如果這是 true,則工作會先檢查以確定指定的腳本已簽署且有效,再執行它。


workingDirectory - 工作目錄
string.

執行腳本的工作目錄。


工作控制選項

除了工作輸入之外,所有工作都有控制選項。 如需詳細資訊,請參閱 控制選項和一般工作屬性

輸出變數

無。

備註

疑難排解

腳本在本機運作,但在管線中失敗

這通常是在管線中使用的服務連線沒有足夠的許可權來執行腳本時發生。 在本機,腳本會使用您的認證執行,而且會成功,因為您可能具有必要的存取權。

若要解決此問題,請確定服務主體/驗證認證具有必要的許可權。 如需詳細資訊,請參閱使用Role-Based 存取控制來管理 Azure 訂用帳戶資源的存取權。

錯誤:找不到具有版本:'version > ' 的模組:' << module name > '。 如果模組最近已安裝,請在重新開機 Azure Pipelines 工作代理程式之後重試

Azure PowerShell工作會使用 Azure/AzureRM/Az PowerShell 模組與 Azure 訂用帳戶互動。 當裝載的代理程式上無法使用 PowerShell 模組時,就會發生此問題。 因此,針對特定工作版本,必須在可用版本的清單中,于Azure PowerShell版本選項中指定慣用Azure PowerShell版本。 您可以在Microsoft 裝載代理程式的[軟體] 資料表中找到已安裝的軟體。

服務連線問題

若要針對服務連線的相關問題進行疑難排解,請參閱 服務連線疑難排解

範例

下列範例示範如何從檔案叫用腳本,並將腳本引數傳遞給它。

- 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、傳統組建、傳統版本
在 上執行 Agent、DeploymentGroup
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 任意
Settable 變數 任意
代理程式版本 2.115.0 或更新版本
工作類別 部署