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
-
스크립트 경로
string
;
targetType = filePath
때 필요합니다.
실행할 스크립트의 경로를 지정합니다. 정규화된 경로이거나 $(System.DefaultWorkingDirectory)
상대 경로여야 합니다.
arguments
-
인수
string
; 선택 사항.
targetType = filePath
때 사용합니다.
PowerShell 스크립트에 전달된 인수를 지정합니다. 인수는 서수 매개 변수 또는 명명된 매개 변수일 수 있습니다. 예: -Name someName -Path -Value "Some long string value"
.
targetType
inline
설정되면 arguments
사용되지 않습니다.
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
-
WarningPreference
string
; 허용되는 값: default
, stop
, continue
silentlyContinue
. 기본값은 default
입니다.
Default
설정하지 않으면 스크립트 맨 위에 줄 $WarningPreference = 'VALUE'
앞에 추가합니다.
informationPreference
-
InformationPreference
string
; 허용되는 값: default
, stop
, continue
silentlyContinue
. 기본값은 default
입니다.
Default
설정하지 않으면 스크립트 맨 위에 줄 $InformationPreference = 'VALUE'
앞에 추가합니다.
verbosePreference
-
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
종료 코드로 전파됩니다. 그렇지 않으면 스크립트의 끝에 줄이 추가되지 않습니다.
PowerShell Corepwsh
- 사용
boolean
; 기본값은 false
입니다.
이 경우 Windows 에이전트에서 실행되는 작업은 powershell.exe
대신 경로의 pwsh.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
.NET Core를 기반으로 하는 PowerShell의 플랫폼 간 버전인 PowerShell Core를 실행합니다.
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>
이 문제를 방지하려면 스크립트 수준에서 이러한 예외를 처리하거나 오류 메시지 내의 소스 코드 줄에 파이프라인 비밀이 나타날 수 있는 경우를 방지할 수 있습니다.
예시
- 파일 스크립트 호출
- 경고 작성
- 오류 쓰기
- 여러 인수 사용하여 PowerShell 스크립트 호출
파일에서 스크립트 호출
다음은 리포지토리의 루트에 있는 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 이상 |
작업 범주 | 유틸리티 |
참고하십시오
- PowerShell 스크립트를 사용하여 파이프라인 사용자 지정 - ApplyVersionToAssemblies.ps1
- PowerShell 스크립트에 대해 자세히 알아보기
- Windows PowerShell 사용하여 스크립팅
- Microsoft 스크립트 센터(스크립팅 가이즈)
- PowerShell.org