Dan*_*nze 6 xml powershell hashtable
我想转换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>
Run Code Online (Sandbox Code Playgroud)
进入哈希表:
Name Value
ipAddress 10.2.1.90
gitDir C:\git\project
gitArchiveDir C:\git\archive
apacheDocroot /var/www
apacheUsername root
Run Code Online (Sandbox Code Playgroud)
目前使用此读取方法:
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$File = $directorypath + '\config.xml'
$CONFIG = "CONFIG"
$CFG = [xml] ( gc $File )
$CFG.test.ipAddress
Run Code Online (Sandbox Code Playgroud)
Kei*_*ill 17
这应该做的伎俩:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9241 次 |
| 最近记录: |