2012-09-13 22 views
5

Quiero convertir XML:XML para la tabla de hash usando Powershell

<TEST> 
    <ipAddress value="10.2.1.90"/> 
    <gitDir value="C:\git\project"/> 
    <gitArchiveDir value="C:\git\archive"/> 
    <apacheDocroot value="/var/www"/> 
    <apacheUsername value="root"/> 
</TEST> 

en una tabla de hash:

Name    Value 

ipAddress  10.2.1.90 
gitDir   C:\git\project 
gitArchiveDir C:\git\archive 
apacheDocroot /var/www 
apacheUsername root 

Actualmente se utiliza este método de lectura:

$invocation = (Get-Variable MyInvocation).Value 
$directorypath = Split-Path $invocation.MyCommand.Path 
$File = $directorypath + '\config.xml' 
$CONFIG = "CONFIG" 
$CFG = [xml] (gc $File) 

$CFG.test.ipAddress 

Respuesta

15

Esto debería hacer el truco:

PS C:\> $CFG = [xml]@' 
>> <TEST> 
>>  <ipAddress value="10.2.1.90"/> 
>>  <gitDir value="C:\git\project"/> 
>>  <gitArchiveDir value="C:\git\archive"/> 
>>  <apacheDocroot value="/var/www"/> 
>>  <apacheUsername value="root"/> 
>> </TEST> 
>> '@ 
>> 
PS C:\> $ht = @{} 
PS C:\> $CFG.test.ChildNodes | Foreach {$ht[$_.Name] = $_.Value} 
PS C:\> $ht 

Name       Value 
----       ----- 
apacheUsername     root 
gitArchiveDir     C:\git\archive 
ipAddress      10.2.1.90 
apacheDocroot     /var/www 
gitDir       C:\git\project