다음을 통해 공유


PSItem에 대한 정보

간단한 설명

파이프라인 개체의 현재 개체를 포함하는 자동 변수입니다.

긴 설명

PowerShell에는 파이프라인과 같이 현재 개체를 처리하는 scriptblock의 자동 변수로 변수와 해당 별칭$PSItem이 포함됩니다$_. 이 문서에서는 예제를 사용 $PSItem 하지만 $PSItem 모든 예제에서 바꿀 $_ 수 있습니다.

파이프라인의 모든 개체에 대해 작업을 수행하는 명령에서 이 변수를 사용할 수 있습니다.

다음과 같은 몇 가지 일반적인 사용 사례가 있습니다.$PSItem

  • cmdlet의 ForEach-Object 매개 변수에 관련된 스크립트 블록에서
  • cmdlet의 Where-Object 매개 변수에 있는 스크립트 블록에서
  • 내장 메서드 ForEachWhere 에서는.
  • delay-bind scriptblock 매개 변수 사용
  • switch 구문의 조건부 값 및 연관된 스크립트 블록들에서
  • 함수의 process 블록에서
  • filter 정의에 따르면
  • ValidateScript 특성의 스크립트 블록에서
  • catch 문장의 scriptblock에서
  • -replace 연산자의 대체 피연산자 스크립트 블록에서

이 문서의 나머지 부분에는 이러한 사용 사례에 사용하는 $PSItem 예제가 포함되어 있습니다.

ForEach-Object 프로세스

ForEach-Object cmdlet은 파이프라인의 개체에서 작동하도록 설계되었으며 파이프라인의 모든 개체에 대해 Process 매개 변수의 scriptblock을 한 번 실행합니다.

Process$PSItem사용할 수 있지만 Begin 또는 End 매개 변수 scriptblock에는 사용할 수 없습니다. Begin 또는 End 매개 변수 scriptblock에서 $PSItem

$parameters = @{
    Begin   = { Write-Host "PSItem in Begin is: $PSItem" }
    Process = {
        Write-Host "PSItem in Process is: $PSItem"
        $PSItem + 1
    }
    End     = { Write-Host "PSItem in End is: $PSItem" }
}

$result = 1, 2, 3 | ForEach-Object @parameters

Write-Host "Result is: $result"
PSItem in Begin is:
PSItem in Process is: 1
PSItem in Process is: 2
PSItem in Process is: 3
PSItem in End is:
Result is: 2 3 4

filterScript Where-Object

Where-Object cmdlet은 파이프라인의 개체를 필터링하도록 설계되었습니다.

파이프라인의 각 입력 개체에 대해 한 번씩 실행되는 FilterScript$PSItemscriptblock에서 사용할 수 있습니다.

1, 2, 3 | Where-Object -FilterScript { ($PSItem % 2) -eq 0 }
2

이 예제 에서 FilterScript 는 현재 개체가 짝수인지 확인하고 홀수 값을 필터링하며 원래 목록에서만 2 반환합니다.

ForEach 및 Where 메서드

배열에 대한 ForEachWhere 내장 메서드는 모두 scriptblock을 입력 매개 변수로 사용합니다. 이러한 scriptblock에서 현재 개체에 액세스하는 데 사용할 $PSItem 수 있습니다.

@('a', 'b', 'c').ForEach({ $PSItem.ToUpper() }).Where({ $PSItem -ceq 'B' })
B

이 예제에서는 ForEach 메서드의 scriptblock이 현재 개체의 위쪽에 있습니다. 그런 다음 Where.

Delay-bind ScriptBlock 매개 변수

지연 바인딩 scriptblock을 사용하면 파이프라인된 cmdlet을 실행하기 전에 매개 변수를 정의하는 데 사용할 $PSItem 수 있습니다.

dir config.log | Rename-Item -NewName { "old_$($_.Name)" }

Switch 문 ScriptBlocks

switch 문에서는 작업 scriptblock과 문 조건 scriptblock 모두에서 사용할 $PSItem 수 있습니다.

$numbers = 1, 2, 3

switch ($numbers) {
    { ($PSItem % 2) -eq 0 } { "$PSItem is even" }
    default { "$PSItem is odd" }
}
1 is odd
2 is even
3 is odd

이 예제에서 문 조건 scriptblock은 현재 개체가 짝수인지 여부를 확인합니다. 짝수인 경우 연결된 작업 scriptblock은 현재 개체가 짝수임을 나타내는 메시지를 출력합니다.

조건에 대한 default 작업 스크립트 블록은 현재 개체가 홀수임을 나타내는 메시지를 출력합니다.

함수 프로세스 블록

함수를 없습니다. 또는 $PSItem 블록에서 begin 참조 end 하는 경우 해당 블록이 파이프라인의 각 개체에서 작동하지 않기 때문입니다$null.

블록 정의에서 사용하는 $PSItem 경우 함수가 파이프라인에서 process 호출되고 그렇지 않으면 $null값이 현재 개체입니다.

function Add-One {
    process { $PSItem + 1 }
}

1, 2, 3 | Add-One
2
3
4

고급 함수$PSItem사용할 수 있지만 그렇게 할 이유가 거의 없습니다. 파이프라인에서 입력을 받으려는 경우 매개 변수 특성에 대한 인수 중 하나를 사용하여 매개 변수ValueFromPipeline*정의하는 것이 가장 좋습니다.

고급 함수에 매개 변수 특성 및 cmdlet 바인딩을 사용하면 필요한 값을 가져오기 위해 현재 개체를 처리하는 것보다 구현이 더 명시적이고 예측 가능합니다.

고급 함수에서 사용하는 한 가지 좋은 용도 $PSItem 는 함수에 파이프라인의 입력을 사용하는 여러 매개 변수가 있는 경우 현재 개체 자체에서 디버깅 또는 로깅을 검사하는 것입니다.

function Write-JsonLog {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipelineByPropertyName)]
        [string]$Message
    )
    begin {
        $entries = @()
    }
    process {
        $entries += [pscustomobject]@{
            Message   = $Message
            TimeStamp = [datetime]::Now
        }

        if ($PSItem) {
            $props  = $PSItem | ConvertTo-Json
            $number = $entries.Length
            Write-Verbose "Input object $number is:`n$props"
        }
    }
    end {
        ConvertTo-Json -InputObject $entries
    }
}

이 예제 함수는 메시지와 타임스탬프를 사용하여 JSON 개체의 배열을 출력합니다. 파이프라인에서 호출되는 경우 각 항목에 대해 현재 개체의 Message 속성을 사용합니다. 또한 출력 로그에 비해 실제 입력을 볼 수 있도록 현재 개체 자체의 JSON 표현을 자세한 정보 표시 스트림에 씁니다.

$Items = @(
    [pscustomobject]@{
        Name    = 'First Item'
        Message = 'A simple note'
    }
    [pscustomobject]@{
        Name    = 'Item with extra properties'
        Message = 'Missing message, has info instead'
        Info    = 'Some metadata'
        Source  = 'Where this came from'
    }
    [pscustomobject]@{
        Name    = 'Last Item'
        Message = 'This also gets logged'
    }
)

$Items | Write-JsonLog -Verbose
VERBOSE: Input object 1 is:
{
    "Name":  "First Item",
    "Message":  "A simple note"
}
VERBOSE: Input object 2 is:
{
    "Name":  "Item with extra properties",
    "Message":  "Missing message, has info instead",
    "Info":  "Some metadata",
    "Source":  "Where this came from"
}
VERBOSE: Input object 3 is:
{
    "Name":  "Last Item",
    "Message":  "This also gets logged"
}
[
    {
        "Message":  "A simple note",
        "TimeStamp":  "\/Date(1670344068257)\/"
    },
    {
        "Message":  "Missing message, has info instead",
        "TimeStamp":  "\/Date(1670344068259)\/"
    },
    {
        "Message":  "This also gets logged",
        "TimeStamp":  "\/Date(1670344068261)\/"
    }
]

필터 정의

필터$PSItem문 목록에서 사용할 수 있습니다.

정의에서 사용하는 $PSItem 경우 필터가 파이프라인에서 filter 호출되고 그렇지 않으면 $null값이 현재 개체입니다.

filter Test-IsEven { ($PSItem % 2) -eq 0 }

1, 2, 3 | Test-IsEven
False
True
False

이 예제에서 필터는 Test-IsEven 현재 개체가 짝수이고 $true 그렇지 않은 경우 출력 $false 합니다.

ValidateScript 특성 ScriptBlock

ValidateScript$PSItem사용할 수 있습니다. ValidateScript사용할 경우 유효성을 검사할 현재 개체의 값입니다. 변수 또는 매개 변수 값이 배열인 경우 현재 개체로 배열의 각 개체에 $PSItem 대해 scriptblock이 한 번 호출됩니다.

function Add-EvenNumber {
    param(
        [ValidateScript({ 0 -eq ($PSItem % 2) })]
        [int[]]$Number
    )

    begin {
        [int]$total = 0
    }

    process {
        foreach ($n in $Number) {
            $total += $n
        }
    }

    end {
        $total
    }
}

Add-EvenNumber -Number 2, 4, 6

Add-EvenNumber -Number 1, 2
12

Add-EvenNumber:
Line |
  24 |  Add-EvenNumber -Number 1, 2
     |                         ~~~~
     | Cannot validate argument on parameter 'Number'. The
" 0 -eq ($PSItem % 2) " validation script for the argument
with value "1" did not return a result of True. Determine
why the validation script failed, and then try the command
again.

이 예제에서는 ValidateScript 특성에 대한 scriptblock이 Number 매개 변수에 전달된 각 값에 대해 한 번 실행되며 값이 짝수인 경우 오류를 반환합니다.

함수는 Add-EvenNumber 유효한 입력 번호를 추가하고 합계를 반환합니다.

ScriptBlock catch

catch 블록 내에서 $PSItem사용하여 현재 오류에 액세스할 수 있습니다. 개체가 ErrorRecord 형식입니다.

try { NonsenseString }
catch {
    Write-Host "An error occurred:"
    Write-Host $_
}

이 스크립트를 실행하면 다음 결과가 반환됩니다.

An Error occurred:
The term 'NonsenseString' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.

자세한 예제는 about_Try_Catch_Finally예외 정보 액세스 섹션을 참조하세요.

-replace 연산자의 대체 ScriptBlock

PowerShell 6부터 $PSItem 연산자를 호출하고 대체 scriptblock정의할 때 사용할 수 있습니다. 이렇게 하면 값 $PSItem 은 현재 일치 항목의 값입니다.

$datePattern = '\d{4}-\d{2}-\d{2}'
'Today is 1999-12-31' -replace $datePattern, { [datetime]$PSItem.Value }
Today is 12/31/1999 00:00:00

이 예제에서 대체 스크립트 블록은 값을 datetime으로 캐스팅하여 원래 날짜 문자열을 현재 문화권의 기본 형식으로 바꿉니다.

참고하십시오