使用此工作在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
-
文稿路徑
string
。
targetType = filePath
時為必要項。
指定要執行的文稿路徑。 必須是完整路徑或相對於 $(System.DefaultWorkingDirectory)
。
arguments
-
自變數
string
。 選擇性。
targetType = filePath
時使用 。
指定傳遞至 PowerShell 腳本的自變數。 自變數可以是序數參數或具名參數。 例如: -Name someName -Path -Value "Some long string value"
。
當 arguments
設定為 targetType
時,不會使用 inline
。
script
-
文稿
string
。
targetType = inline
時為必要項。 預設值:# Write your PowerShell commands here.\n\nWrite-Host "Hello World"
。
指定文稿的內容。 支援的內嵌腳本長度上限為 20000 個字元。 如果您想要使用較長的腳稿,請使用檔案中的腳本。
errorActionPreference
-
ErrorActionPreference
string
。 允許的值:default
、、、stop
continue
。silentlyContinue
預設值:stop
。
在腳本頂端加上行 $ErrorActionPreference = 'VALUE'
。
errorActionPreference
-
ErrorActionPreference
string
。 允許的值:stop
、continue
、silentlyContinue
。 預設值:stop
。
在腳本頂端加上行 $ErrorActionPreference = 'VALUE'
。
warningPreference
-
警告首選項
string
。 允許的值:default
、、、stop
continue
。silentlyContinue
預設值:default
。
當未設定為 Default
時,會在腳本頂端加上行 $WarningPreference = 'VALUE'
。
informationPreference
-
資訊首選項
string
。 允許的值:default
、、、stop
continue
。silentlyContinue
預設值:default
。
當未設定為 Default
時,會在腳本頂端加上行 $InformationPreference = 'VALUE'
。
verbosePreference
-
詳細首選項
string
。 允許的值:default
、、、stop
continue
。silentlyContinue
預設值:default
。
當未設定為 Default
時,會在腳本頂端加上行 $VerbosePreference = 'VALUE'
。
debugPreference
-
DebugPreference
string
。 允許的值:default
、、、stop
continue
。silentlyContinue
預設值:default
。
當未設定為 Default
時,會在腳本頂端加上行 $DebugPreference = 'VALUE'
。
progressPreference
-
ProgressPreference
string
。 允許的值:default
、、、stop
continue
。silentlyContinue
預設值: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.powershell 和 steps.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、傳統組建、傳統版本 |
執行於 | Agent、DeploymentGroup |
需求 | 沒有 |
功能 | 此工作不符合作業中後續工作的任何需求。 |
命令限制 | 任意 |
Settable 變數 | 任意 |
代理程式版本 | 2.115.0 或更新 |
工作類別 | 效用 |
另請參閱
- 使用 PowerShell 腳本自定義管線 - ApplyVersionToAssemblies.ps1
- 深入瞭解 PowerShell 腳本
- 使用 Windows PowerShell 腳本
- Microsoft腳本中心 (腳本傢伙)
- PowerShell.org