Select-Xml
XML 문자열 또는 문서에서 텍스트를 찾습니다.
구문
Xml (기본값)
Select-Xml
[-Xml] <XmlNode[]>
[-XPath] <String>
[-Namespace <Hashtable>]
[<CommonParameters>]
Path
Select-Xml
[-Path] <String[]>
[-XPath] <String>
[-Namespace <Hashtable>]
[<CommonParameters>]
LiteralPath
Select-Xml
[-XPath] <String>
-LiteralPath <String[]>
[-Namespace <Hashtable>]
[<CommonParameters>]
Content
Select-Xml
[-XPath] <String>
-Content <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 변수에 저장합니다.
두 번째 명령은 XML 경로를 AliasProperty 노드의 $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 노드의 경로를 지정합니다.
예제 3: PowerShell 도움말 파일 검색
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 문자열을 저장합니다. (여기 문자열에 대한 자세한 내용은 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입니다. 해시 테이블 키 이름인 스니프는 임의입니다. 예약되지 않은 이름은 사용할 수 있지만 xmlns를 사용할 수는 없습니다.
매개 변수
-Content
검색할 XML이 포함된 문자열을 지정합니다.
문자열을 Select-Xml
매개 변수 속성
| 형식: | String[] |
| Default value: | None |
| 와일드카드 지원: | False |
| DontShow: | False |
매개 변수 집합
Content
| Position: | Named |
| 필수: | True |
| 파이프라인의 값: | True |
| 속성 이름별 파이프라인의 값: | False |
| 나머지 인수의 값: | False |
-LiteralPath
검색할 XML 파일의 경로 및 파일 이름을 지정합니다. Path달리 LiteralPath 매개 변수의 값은 입력된 대로 정확하게 사용됩니다. 와일드카드로 해석되는 문자는 없습니다. 경로에 이스케이프 문자가 포함된 경우 작은따옴표로 묶습니다. 작은따옴표는 PowerShell에 문자를 이스케이프 시퀀스로 해석하지 않도록 지시합니다.
매개 변수 속성
| 형식: | String[] |
| Default value: | None |
| 와일드카드 지원: | False |
| DontShow: | False |
| 별칭: | PSPath |
매개 변수 집합
LiteralPath
| Position: | Named |
| 필수: | True |
| 파이프라인의 값: | False |
| 속성 이름별 파이프라인의 값: | True |
| 나머지 인수의 값: | False |
-Namespace
XML에 사용되는 네임스페이스의 해시 테이블을 지정합니다. @{<namespaceName> = <namespaceValue>}형식을 사용합니다.
XML에서 xmlns로 시작하는 기본 네임스페이스를 사용하는 경우 네임스페이스 이름에 임의의 키를 사용합니다. xmlns를 사용할 수 없습니다. XPath 문에서 각 노드 이름 앞에 네임스페이스 이름과 콜론(예: //namespaceName:Node)을 접두사로 지정합니다.
매개 변수 속성
| 형식: | Hashtable |
| Default value: | None |
| 와일드카드 지원: | False |
| DontShow: | False |
매개 변수 집합
(All)
| Position: | Named |
| 필수: | False |
| 파이프라인의 값: | False |
| 속성 이름별 파이프라인의 값: | False |
| 나머지 인수의 값: | False |
-Path
검색할 XML 파일의 경로 및 파일 이름을 지정합니다. 와일드카드 문자가 허용됩니다.
매개 변수 속성
| 형식: | String[] |
| Default value: | None |
| 와일드카드 지원: | True |
| DontShow: | False |
매개 변수 집합
Path
| Position: | 1 |
| 필수: | True |
| 파이프라인의 값: | False |
| 속성 이름별 파이프라인의 값: | True |
| 나머지 인수의 값: | False |
-Xml
하나 이상의 XML 노드를 지정합니다.
XML 문서는 XML 노드의 컬렉션으로 처리됩니다.
XML 문서를 Select-Xml
매개 변수 속성
| 형식: | XmlNode[] |
| Default value: | None |
| 와일드카드 지원: | False |
| DontShow: | False |
| 별칭: | 노드 |
매개 변수 집합
Xml
| Position: | 1 |
| 필수: | True |
| 파이프라인의 값: | True |
| 속성 이름별 파이프라인의 값: | True |
| 나머지 인수의 값: | False |
-XPath
XPath 검색 쿼리를 지정합니다. 쿼리 언어는 대/소문자를 구분합니다. 이 매개 변수는 필수입니다.
매개 변수 속성
| 형식: | String |
| Default value: | None |
| 와일드카드 지원: | False |
| DontShow: | False |
매개 변수 집합
(All)
| Position: | 0 |
| 필수: | True |
| 파이프라인의 값: | False |
| 속성 이름별 파이프라인의 값: | False |
| 나머지 인수의 값: | False |
CommonParameters
이 cmdlet은 일반적인 매개 변수인 -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction 및 -WarningVariable 매개 변수를 지원합니다. 자세한 내용은 about_CommonParameters를 참조하세요.
입력
System.String or System.Xml.XmlNode
경로 또는 XML 노드를 이 cmdlet으로 파이프할 수 있습니다.