PowerShell@2 - PowerShell v2 작업

이 작업을 사용하여 Linux, macOS 또는 Windows에서 PowerShell 스크립트를 실행합니다.

참고

기본적으로 PowerShell v2는 Linux 에이전트용 PowerShell Core를 사용하고 Windows 에이전트에는 Windows PowerShell 사용합니다. Windows 에이전트에서 최신 버전의 PowerShell을 사용하려면 매개 변수를 pwshtrue설정합니다. 그러면 PowerShell Core가 대신 사용됩니다.

Syntax

# 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.
# PowerShell v2
# Run a PowerShell script on Windows, macOS, or Linux.
- 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가 로 설정된 inline경우 targetType 가 사용되지 않습니다.


script - 스크립트
string. 필요한 경우 targetType = inline입니다. 기본값은 # Write your PowerShell commands here.\n\nWrite-Host "Hello World"입니다.

스크립트의 내용을 지정합니다. 지원되는 최대 인라인 스크립트 길이는 20000자입니다. 더 긴 스크립트를 사용하려면 파일의 스크립트를 사용합니다.


script - 스크립트
string. 필요한 경우 targetType = inline입니다. 기본값은 # Write your powershell commands here.\n\nWrite-Host "Hello World"\n\n# Use the environment variables input below to pass secret variables to this script.입니다.

스크립트의 내용을 지정합니다. 지원되는 최대 인라인 스크립트 길이는 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종료 코드로 전파됩니다. 그렇지 않으면 스크립트의 끝에 줄이 추가되지 않습니다.


pwsh - PowerShell Core 사용
boolean. 기본값은 false입니다.

이 경우 Windows 에이전트에서 실행되는 작업은 대신 powershell.exe경로에서 를 사용합니다pwsh.exe.


workingDirectory - 작업 디렉터리
string.

스크립트가 실행되는 작업 디렉터리를 지정합니다. 값을 지정하지 않으면 작업 디렉터리가 입니다 $(Build.SourcesDirectory).


runScriptInSeparateScope - 별도의 scope 스크립트 실행
boolean. 기본값은 false입니다.

이 입력을 사용하면 기본 .대신 연산자를 사용하여 & PowerShell 스크립트를 실행할 수 있습니다. 이 입력이 로 true설정된 경우 스크립트는 별도의 scope 실행되고 전역적으로 범위가 지정된 PowerShell 변수는 업데이트되지 않습니다.


작업 제어 옵션

모든 작업에는 작업 입력 외에 제어 옵션이 있습니다. 자세한 내용은 컨트롤 옵션 및 일반적인 작업 속성을 참조하세요.

출력 변수

없음

설명

각 PowerShell 세션은 실행되는 작업 기간 동안만 지속됩니다. 부트스트랩된 작업에 의존하는 작업은 부트스트랩과 동일한 작업에 있어야 합니다.

작업 바로 가기

PowerShell@2 YAML에는 steps.powershellsteps.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>

이 문제를 방지하려면 스크립트 수준에서 이러한 예외를 처리하거나 파이프라인 비밀이 오류 메시지 내의 소스 코드 줄에 나타날 수 있는 경우를 방지할 수 있습니다.

예제

파일에서 스크립트 호출

다음은 리포지토리의 루트에 있는 라는 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'

요구 사항

요구 사항 Description
파이프라인 유형 YAML, 클래식 빌드, 클래식 릴리스
실행 중 에이전트, DeploymentGroup
요청 없음
Capabilities 이 작업은 작업의 후속 작업에 대한 요구를 충족하지 않습니다.
명령 제한 사항 모두
설정 가능한 변수 모두
에이전트 버전 2.115.0 이상
작업 범주 유틸리티

추가 정보