다음을 통해 공유


Select-Xml

XML 문자열 또는 문서에서 텍스트를 찾습니다.

Syntax

Select-Xml
      [-Xml] <XmlNode[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]
Select-Xml
      [-Path] <String[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]
Select-Xml
      -LiteralPath <String[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]
Select-Xml
      -Content <String[]>
      [-XPath] <String>
      [-Namespace <Hashtable>]
      [<CommonParameters>]

Description

Select-Xml cmdlet을 사용하면 XPath 쿼리를 사용하여 XML 문자열 및 문서에서 텍스트를 검색할 수 있습니다. XPath 쿼리를 입력하고 Content, Path 또는 Xml 매개 변수를 사용하여 검색할 XML을 지정합니다.

예제

예제 1: AliasProperty 노드 선택

PS C:\> $Path = "$Pshome\Types.ps1xml"
PS C:\> $XPath = "/Types/Type/Members/AliasProperty"
PS C:\> Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node

Name                 ReferencedMemberName
----                 --------------------
Count                Length
Name                 Key
Name                 ServiceName
RequiredServices     ServicesDependedOn
ProcessName          Name
Handles              Handlecount
VM                   VirtualSize
WS                   WorkingSetSize
Name                 ProcessName
Handles              Handlecount
VM                   VirtualMemorySize
WS                   WorkingSet
PM                   PagedMemorySize
NPM                  NonpagedSystemMemorySize
Name                 __Class
Namespace            ModuleName

이 예제에서는 Types.ps1xml에 있는 별칭 속성을 가져옵니다. 이 파일에 대한 자세한 내용은 about_Types.ps1xml을 참조하세요.

첫 번째 명령은 Types.ps1xml 파일의 경로를 $Path 변수에 저장합니다.

두 번째 명령은 AliasProperty 노드에 대한 XML 경로를 $XPath 변수에 저장합니다.

세 번째 명령은 Select-Xml cmdlet을 사용하여 Types.ps1xml 파일에서 XPath 문으로 식별된 AliasProperty 노드를 가져옵니다. 명령은 파이프라인 연산자를 사용하여 AliasProperty 노드를 Select-Object cmdlet으로 보냅니다. ExpandProperty 매개 변수는 Node 개체를 확장하고 Name 및 ReferencedMemberName 속성을 반환합니다.

결과는 Types.ps1xml 파일에 있는 각 별칭 속성의 Name 및 ReferencedMemberName을 보여 줍니다. 예를 들어 Length 속성의 별칭인 Count 속성이 있습니다.

예제 2: XML 문서 입력

PS C:\> [xml]$Types = Get-Content $Pshome\Types.ps1xml
PS C:\> Select-Xml -Xml $Types -XPath "//MethodName"

이 예제에서는 XML 매개 변수를 사용하여 Select-Xml cmdlet에 XML 문서를 제공하는 방법을 보여줍니다.

첫 번째 명령은 Get-Content cmdlet을 사용하여 Types.ps1xml 파일의 콘텐츠를 가져와서 $Types 변수에 저장합니다. [xml]은 변수를 XML 개체로 캐스팅합니다.

두 번째 명령은 Select-Xml cmdlet을 사용하여 Types.ps1xml 파일의 MethodName 노드를 가져옵니다. Xml 매개 변수를 사용하여 $Types 변수의 XML 콘텐츠를 지정하고 XPath 매개 변수를 사용하여 MethodName 노드의 경로를 지정합니다.

PS C:\> $Namespace = @{command = "https://schemas.microsoft.com/maml/dev/command/2004/10"; maml = "https://schemas.microsoft.com/maml/2004/10"; dev = "https://schemas.microsoft.com/maml/dev/2004/10"}

The second command saves the path to the help files in the $Path variable.If there are no help files in this path on your computer, use the Update-Help cmdlet to download the help files. For more information about Updatable Help, see about_Updatable_Help (https://go.microsoft.com/fwlink/?LinkId=235801).
PS C:\> $Path = "$Pshome\en-us\*dll-Help.xml"

The third command uses the **Select-Xml** cmdlet to search the XML for cmdlet names by finding Command:Name element anywhere in the files. It saves the results in the $Xml variable.**Select-Xml** returns a **SelectXmlInfo** object that has a Node property, which is a **System.Xml.XmlElement** object. The Node property has an InnerXML property, which contains the actual XML that is retrieved.
PS C:\> $Xml = Select-Xml -Path $Path -Namespace $Namespace -XPath "//command:name"

The fourth command sends the XML in the $Xml variable to the Format-Table cmdlet. The **Format-Table** command uses a calculated property to get the Node.InnerXML property of each object in the $Xml variable, trim the white space before and after the text, and display it in the table, along with the path to the source file.
PS C:\> $Xml | Format-Table @{Label="Name"; Expression= {($_.node.innerxml).trim()}}, Path -AutoSize

Name                    Path
----                    ----
Export-Counter          C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Get-Counter             C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Get-WinEvent            C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Import-Counter          C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Add-Computer            C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml
Add-Content             C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml
Checkpoint-Computer     C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml
...

이 예제에서는 Select-Xml cmdlet을 사용하여 PowerShell XML 기반 cmdlet 도움말 파일을 검색하는 방법을 보여줍니다. 각 도움말 파일의 제목 역할을 하는 cmdlet 이름 및 도움말 파일의 경로를 검색합니다.

첫 번째 명령은 도움말 파일에 사용되는 XML 네임스페이스를 나타내는 해시 테이블을 만든 다음 $Namespace 변수에 저장합니다.

예제 4: XML을 입력하는 다양한 방법

PS C:\> $Xml = @"
<?xml version="1.0" encoding="utf-8"?>
<Book>
  <projects>
    <project name="Book1" date="2009-01-20">
      <editions>
        <edition language="English">En.Book1.com</edition>
        <edition language="German">Ge.Book1.Com</edition>
        <edition language="French">Fr.Book1.com</edition>
        <edition language="Polish">Pl.Book1.com</edition>
      </editions>
    </project>
  </projects>
</Book>
"@

The second command uses the *Content* parameter of **Select-Xml** to specify the XML in the $Xml variable.
PS C:\> Select-Xml -Content $Xml -XPath "//edition" | foreach {$_.node.InnerXML}

En.Book1.com
Ge.Book1.Com
Fr.Book1.com
Pl.Book1.com

The third command is equivalent to the second. It uses a pipeline operator (|) to send the XML in the $Xml variable to the **Select-Xml** cmdlet.
PS C:\> $Xml | Select-Xml -XPath "//edition" | foreach {$_.node.InnerXML}

En.Book1.com
Ge.Book1.Com
Fr.Book1.com
Pl.Book1.com

이 예제에서는 Select-Xml cmdlet에 XML을 보내는 두 가지 방법을 보여줍니다.

첫 번째 명령은 $Xml 변수에 XML이 포함된 here-string을 저장합니다. here-string에 대한 자세한 내용은 about_Quoting_Rules를 참조하세요.

예제 5: 기본 xmlns 네임스페이스 사용

PS C:\> $SnippetNamespace = @{snip = "https://schemas.microsoft.com/PowerShell/Snippets"}

The second command uses the **Select-Xml** cmdlet to get the content of the Title element of each snippet. It uses the *Path* parameter to specify the Snippets directory and the *Namespace* parameter to specify the namespace in the $SnippetNamespace variable. The value of the *XPath* parameter is the "snip" namespace key, a colon (:), and the name of the Title element.The command uses a pipeline operator (|) to send each **Node** property that **Select-Xml** returns to the ForEach-Object cmdlet, which gets the title in the value of the **InnerXml** property of the node.
PS C:\> Select-Xml -Path $Home\Documents\WindowsPowerShell\Snippets -Namespace $SnippetNamespace -XPath "//snip:Title" | foreach {$_.Node.Innerxml}

이 예제에서는 기본 xmlns 네임스페이스를 사용하는 XML 문서와 함께 Select-Xml cmdlet을 사용하는 방법을 보여 줍니다. Windows PowerShell ISE 사용자 생성 조각 파일의 제목을 가져옵니다. 조각에 대한 자세한 내용은 New-IseSnippet을 참조하세요.

첫 번째 명령은 XML 파일 조각이 사용하는 기본 네임스페이스에 대한 해시 테이블을 만들고 $SnippetNamespace 변수에 할당합니다. 해시 테이블 값은 조각 XML의 XMLNS 스키마 URI입니다. 해시 테이블 키 이름인 캡처는 임의입니다. 예약되지 않은 이름은 사용할 수 있지만 xmln은 사용할 수 없습니다.

매개 변수

-Content

검색할 XML이 포함된 문자열을 지정합니다. 문자열을 Select-Xml로 파이프할 수도 있습니다.

Type:String[]
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-LiteralPath

검색할 XML 파일의 경로 및 파일 이름을 지정합니다. Path와 달리 LiteralPath 매개 변수 값은 입력한 대로 사용됩니다. 어떠한 문자도 와일드카드로 해석되지 않습니다. 이스케이프 문자가 포함된 경로는 작은따옴표로 묶으세요. 작은따옴표는 PowerShell에 문자를 이스케이프 시퀀스로 해석하지 않도록 지시합니다.

Type:String[]
Aliases:PSPath
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Namespace

XML에서 사용되는 네임스페이스의 해시 테이블을 지정합니다. @{<namespaceName> = <namespaceValue} 형식을> 사용합니다.

XML에서 xmlns로 시작하는 기본 네임스페이스를 사용하는 경우 네임스페이스 이름에 임의의 키를 사용합니다. xmlns를 사용할 수 없습니다. XPath 문에서 각 노드 이름 앞에 네임스페이스 이름과 콜론(예: //namespaceName:Node)을 접두사로 지정합니다.

Type:Hashtable
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Path

검색할 XML 파일의 경로 및 파일 이름을 지정합니다. 와일드카드 문자를 사용할 수 있습니다.

Type:String[]
Position:1
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

-Xml

하나 이상의 XML 노드를 지정합니다.

XML 문서는 XML 노드의 컬렉션으로 처리됩니다. XML 문서를 Select-Xml로 파이프하면 파이프라인을 통해 제공되는 각 문서 노드가 별도로 검색됩니다.

Type:XmlNode[]
Aliases:Node
Position:1
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-XPath

XPath 검색 쿼리를 지정합니다. 쿼리 언어는 대/소문자를 구분합니다. 이 매개 변수는 필수입니다.

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

입력

System.String or System.Xml.XmlNode

경로 또는 XML 노드를 이 cmdlet으로 파이프할 수 있습니다.

출력

SelectXmlInfo

참고

  • XPath는 XML 문서의 부분을 식별하도록 설계된 표준 언어입니다. XPath 언어에 대한 자세한 내용은 MSDN 라이브러리의 이벤트 선택 영역의 XPath 참조 및 선택 필터 섹션을 참조하세요.