다음을 통해 공유


매개 변수 바인딩 시각화

매개 변수 바인딩은 PowerShell에서 사용 중인 매개 변수 집합을 확인하고 명령의 매개 변수에 값을 연결(바인딩)하는 데 사용하는 프로세스입니다. 이러한 값은 명령줄 및 파이프라인에서 가져올 수 있습니다.

매개 변수 바인딩 프로세스는 명명된 인수와 위치 명령줄 인수를 바인딩하여 시작합니다. 명령줄 인수를 바인딩한 후 PowerShell은 파이프라인 입력을 바인딩하려고 시도합니다. 파이프라인에서 값을 바인딩하는 방법에는 두 가지가 있습니다. 파이프라인 입력을 허용하는 매개 변수에는 다음 특성 중 하나 또는 둘 다 있습니다.

  • ValueFromPipeline - 파이프라인의 값은 해당 형식에 따라 매개 변수에 바인딩됩니다. 인수의 형식은 매개 변수의 형식과 일치해야 합니다.
  • ValueFromPipelineByPropertyName - 파이프라인의 값은 이름에 따라 매개 변수에 바인딩됩니다. 파이프라인의 개체에는 매개 변수 이름 또는 해당 별칭 중 하나와 일치하는 속성이 있어야 합니다. 속성 형식이 매개 변수 형식과 일치하거나 변환할 수 있어야 합니다.

매개 변수 바인딩에 대한 자세한 내용은 about_Parameter_Binding 참조하세요.

매개 변수 바인딩을 시각화하는 데 사용 Trace-Command

매개 변수 바인딩 문제 해결은 어려울 수 있습니다. Trace-Command cmdlet을 사용하여 매개 변수 바인딩 프로세스를 시각화할 수 있습니다.

다음 시나리오를 살펴 보십시오. 두 개의 텍스트 파일이 file1.txt 있는 디렉터리 및 [file2].txt.

PS> Get-ChildItem

    Directory: D:\temp\test\binding

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           5/17/2024 12:59 PM              0 [file2].txt
-a---           5/17/2024 12:59 PM              0 file1.txt

파이프라인을 통해 파일 이름을 cmdlet에 전달하여 파일을 삭제하려고 합니다 Remove-Item .

PS> 'file1.txt', '[file2].txt' | Remove-Item
PS> Get-ChildItem

    Directory: D:\temp\test\binding

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           5/17/2024 12:59 PM              0 [file2].txt

삭제되고 삭제되지 file1.txt 않음[file2].txtRemove-Item 확인합니다. 파일 이름에는 대괄호가 포함되어 있으며 이 대괄호는 wild카드 식으로 처리됩니다. 를 사용하면 Trace-Command파일 이름이 .의 Remove-ItemPath 매개 변수에 바인딩되는 것을 볼 수 있습니다.

Trace-Command -PSHost -Name ParameterBinding -Expression {
    '[file2].txt' | Remove-Item
}

출력 Trace-Command 은 자세한 정보 표시일 수 있습니다. 각 출력 줄에는 타임스탬프 및 추적 공급자 정보가 접두사로 지정됩니다. 이 예제의 출력에서는 더 쉽게 읽을 수 있도록 접두사 정보가 제거되었습니다.

BIND NAMED cmd line args [Remove-Item]
BIND POSITIONAL cmd line args [Remove-Item]
BIND cmd line args to DYNAMIC parameters.
    DYNAMIC parameter object: [Microsoft.PowerShell.Commands.FileSystemProviderRemoveItemDynamicParameters]
MANDATORY PARAMETER CHECK on cmdlet [Remove-Item]
CALLING BeginProcessing
BIND PIPELINE object to parameters: [Remove-Item]
    PIPELINE object TYPE = [System.String]
    RESTORING pipeline parameter's original values
    Parameter [Path] PIPELINE INPUT ValueFromPipeline NO COERCION
    BIND arg [[file2].txt] to parameter [Path]
        Binding collection parameter Path: argument type [String], parameter type [System.String[]],
            collection type Array, element type [System.String], no coerceElementType
        Creating array with element type [System.String] and 1 elements
        Argument type String is not IList, treating this as scalar
        Adding scalar element of type String to array position 0
        BIND arg [System.String[]] to param [Path] SUCCESSFUL
    Parameter [Credential] PIPELINE INPUT ValueFromPipelineByPropertyName NO COERCION
    Parameter [Credential] PIPELINE INPUT ValueFromPipelineByPropertyName NO COERCION
    Parameter [Credential] PIPELINE INPUT ValueFromPipelineByPropertyName WITH COERCION
    Parameter [Credential] PIPELINE INPUT ValueFromPipelineByPropertyName WITH COERCION
MANDATORY PARAMETER CHECK on cmdlet [Remove-Item]
CALLING ProcessRecord
CALLING EndProcessing

를 사용하면 Get-Help경로 매개 변수가 파이프라인 ByValue 또는 ByPropertyName.에서 Remove-Item 문자열 개체를 수락하는 것을 볼 수 있습니다. LiteralPath 는 파이프라인 ByPropertyName의 문자열 개체를 허용합니다.

PS> Get-Help Remove-Item -Parameter Path, LiteralPath

-Path <System.String[]>
    Specifies a path of the items being removed. Wildcard characters are permitted.

    Required?                    true
    Position?                    0
    Default value                None
    Accept pipeline input?       True (ByPropertyName, ByValue)
    Accept wildcard characters?  true


-LiteralPath <System.String[]>
    Specifies a path to one or more locations. The value of LiteralPath is used exactly as it's
    typed. No characters are interpreted as wildcards. If the path includes escape characters,
    enclose it in single quotation marks. Single quotation marks tell PowerShell not to interpret
    any characters as escape sequences.

    Required?                    true
    Position?                    named
    Default value                None
    Accept pipeline input?       True (ByPropertyName)
    Accept wildcard characters?  false

출력 Trace-Command 은 매개 변수 바인딩이 명령줄 매개 변수와 파이프라인 입력을 바인딩하여 시작됨을 보여 줍니다. 파이프라인에서 문자열 개체를 수신하는 것을 Remove-Item 볼 수 있습니다. 해당 문자열 개체는 Path 매개 변수에 바인딩됩니다.

BIND PIPELINE object to parameters: [Remove-Item]
    PIPELINE object TYPE = [System.String]
    RESTORING pipeline parameter's original values
    Parameter [Path] PIPELINE INPUT ValueFromPipeline NO COERCION
    BIND arg [[file2].txt] to parameter [Path]
    ...
        BIND arg [System.String[]] to param [Path] SUCCESSFUL

Path 매개 변수는 wild카드 문자를 허용하므로 대괄호는 wild카드 식을 나타냅니다. 그러나 해당 식은 디렉터리의 파일과 일치하지 않습니다. LiteralPath 매개 변수를 사용하여 파일의 정확한 경로를 지정해야 합니다.

Get-Command는 LiteralPath 매개 변수가 파이프라인 ByPropertyName 또는 ByValue.의 입력을 허용한다는 것을 보여 줍니다. 그리고, 그것은 두 개의 별칭을 가지고, PSPath 그리고 LP.

PS> (Get-Command Remove-Item).Parameters.LiteralPath.Attributes |
>> Select-Object ValueFrom*, Alias* | Format-List

ValueFromPipeline               : False
ValueFromPipelineByPropertyName : True
ValueFromRemainingArguments     : False

AliasNames : {PSPath, LP}

다음 예제 Get-Item 에서는 FileInfo 개체를 검색하는 데 사용됩니다. 해당 개체에는 PSPath라는 속성이 있습니다.

PS> Get-Item *.txt | Select-Object PSPath

PSPath
------
Microsoft.PowerShell.Core\FileSystem::D:\temp\test\binding\[file2].txt

그런 다음 FileInfo 개체가 .에 Remove-Item전달됩니다.

Trace-Command -PSHost -Name ParameterBinding -Expression {
    Get-Item *.txt | Remove-Item
}

이 예제의 출력에서는 두 명령에 대한 매개 변수 바인딩을 표시하기 위해 접두사 정보가 제거되고 구분되었습니다.

이 출력에서는 위치 매개 변수 값을 *.txt Path 매개 변수에 바인딩하는 것을 Get-Item 수 있습니다.

BIND NAMED cmd line args [Get-Item]
BIND POSITIONAL cmd line args [Get-Item]
    BIND arg [*.txt] to parameter [Path]
        Binding collection parameter Path: argument type [String], parameter type [System.String[]],
            collection type Array, element type [System.String], no coerceElementType
        Creating array with element type [System.String] and 1 elements
        Argument type String is not IList, treating this as scalar
        Adding scalar element of type String to array position 0
        BIND arg [System.String[]] to param [Path] SUCCESSFUL
BIND cmd line args to DYNAMIC parameters.
    DYNAMIC parameter object: [Microsoft.PowerShell.Commands.FileSystemProviderGetItemDynamicParameters]
MANDATORY PARAMETER CHECK on cmdlet [Get-Item]

매개 변수 바인딩에 대한 추적 출력에서 파이프라인에서 FileInfo 개체를 수신하는 것을 Remove-Item 볼 수 있습니다. FileInfo 개체는 String 개체가 아니므로 Path 매개 변수에 바인딩할 수 없습니다.

FileInfo 개체의 PSPath 속성은 LiteralPath 매개 변수의 별칭과 일치합니다. 또한 PSPathString 개체이므로 형식 강제 변환 없이 LiteralPath 매개 변수에 바인딩할 수 있습니다.

BIND NAMED cmd line args [Remove-Item]
BIND POSITIONAL cmd line args [Remove-Item]
BIND cmd line args to DYNAMIC parameters.
    DYNAMIC parameter object: [Microsoft.PowerShell.Commands.FileSystemProviderRemoveItemDynamicParameters]
MANDATORY PARAMETER CHECK on cmdlet [Remove-Item]
CALLING BeginProcessing
CALLING BeginProcessing
CALLING ProcessRecord
    BIND PIPELINE object to parameters: [Remove-Item]
        PIPELINE object TYPE = [System.IO.FileInfo]
        RESTORING pipeline parameter's original values
        Parameter [Path] PIPELINE INPUT ValueFromPipeline NO COERCION
        BIND arg [D:\temp\test\binding\[file2].txt] to parameter [Path]
            Binding collection parameter Path: argument type [FileInfo], parameter type [System.String[]],
                collection type Array, element type [System.String], no coerceElementType
            Creating array with element type [System.String] and 1 elements
            Argument type FileInfo is not IList, treating this as scalar
            BIND arg [D:\temp\test\binding\[file2].txt] to param [Path] SKIPPED
        Parameter [Credential] PIPELINE INPUT ValueFromPipelineByPropertyName NO COERCION
        Parameter [Path] PIPELINE INPUT ValueFromPipelineByPropertyName NO COERCION
        Parameter [Credential] PIPELINE INPUT ValueFromPipelineByPropertyName NO COERCION
        Parameter [LiteralPath] PIPELINE INPUT ValueFromPipelineByPropertyName NO COERCION
        BIND arg [Microsoft.PowerShell.Core\FileSystem::D:\temp\test\binding\[file2].txt] to parameter [LiteralPath]
            Binding collection parameter LiteralPath: argument type [String], parameter type [System.String[]],
                collection type Array, element type [System.String], no coerceElementType
            Creating array with element type [System.String] and 1 elements
            Argument type String is not IList, treating this as scalar
            Adding scalar element of type String to array position 0
            BIND arg [System.String[]] to param [LiteralPath] SUCCESSFUL
        Parameter [Credential] PIPELINE INPUT ValueFromPipelineByPropertyName WITH COERCION
    MANDATORY PARAMETER CHECK on cmdlet [Remove-Item]
    CALLING ProcessRecord
CALLING EndProcessing
CALLING EndProcessing