问题描述
以下是示例xml文件:
Following is the sample xml file:
<configuration>
<environment id="Development">
<type>Dev</type>
<client>Arizona</client>
<dataSource>Local</dataSource>
<path>App_Data\%%type%%\%%client%%_%%dataSource%%</path>
<targetmachines>
<targetmachine>
<name>Any</name>
<remotedirectory>D:\Inetpub\Test</remotedirectory>
</targetmachine>
</targetmachines>
</environment>
</configuration>
我正在尝试使用以下脚本将上述xml文件转换为PowerShell字典.
I'm trying to convert the above xml file into a PowerShell dictionary using following script.
[System.Xml.XmlDocument] $configxml = Get-Content xmlfile
$environmentId = "Development"
$keyValuePairs = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$configxml.SelectNodes("//configuration/environment[@id='$($environmentId)']//*[not(*)]") | `
ForEach-Object {
if($_.ParentNode.ToString() -ne "targetmachine")
{
$keyValuePairs.Add($_.Name.ToLower(), $_.InnerText)
}
}
Write-Output $keyValuePairs
我正在得到预期的输出.
Im getting following output, which is expected.
Key Value
----- -----
type Dev
client Arizona
dataSource Local
path App_Data\%%type%%\%%client%%_%%dataSource%%
但是在将元素转换为键值对之后,我想用实际值替换占位符.换句话说,元素路径"具有3个占位符,它们是
But After converting the elements into Key value pairs, I want to replace the placeholders with actualy values. In other words, element "Path" has 3 placeholders, they are
1. type
2. client
3. dataSource
基本上,占位符是指以两个百分比(%% xyz %%)开头和结尾的字符串.因此,在将占位符替换为实际值之后,我需要以下输出:
Basically placeholders means the strings which starts and ends with two percents (%%xyz%%). So, I need following output after replacing placeholders with actual values:
Key Value
----- -----
type Dev
client Arizona
dataSource Local
path App_Data\Dev\Arizona_Local
有人可以帮助我如何使用PowerShell实现此目标.预先感谢.
Can someone please help me how can I achive this using PowerShell. Thanks in advance.
推荐答案
通常,在发布带有xml或csv数据的示例时,使用此处的字符串表示数据而不是引用其他人没有的文件会很有帮助.有.
In general, when posting an example with xml or csv data, it is helpful to use here strings to represent the data instead of referencing a file that others do not have.
类似的东西可以用来达到目的.
Something like this can be used to achieve your result.
$xmldata = @"
<configuration>
<environment id="Development">
<type>Dev</type>
<client>Arizona</client>
<dataSource>Local</dataSource>
<path>App_Data\%%type%%\%%client%%_%%dataSource%%</path>
<targetmachines>
<targetmachine>
<name>Any</name>
<remotedirectory>D:\Inetpub\Test</remotedirectory>
</targetmachine>
</targetmachines>
</environment>
</configuration>
"@
[System.Xml.XmlDocument] $configxml = [xml]$xmldata
$environmentId = "Development"
$keyValuePairs = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$configxml.SelectNodes("//configuration/environment[@id='$($environmentId)']//*[not(*)]") | `
ForEach-Object {
if($_.ParentNode.ToString() -ne "targetmachine")
{
$keyValuePairs.Add($_.Name, $_.InnerText)
}
}
"BEFORE---->"
Write-Output $keyValuePairs
# A static way...
#$keyValuePairs.path = $keyValuePairs.path -replace '%%type%%', $keyValuePairs.type
#$keyValuePairs.path = $keyValuePairs.path -replace '%%client%%', $keyValuePairs.client
#$keyValuePairs.path = $keyValuePairs.path -replace '%%datasource%%', $keyValuePairs.datasource
# Something more dynamic
$m = [Regex]::Matches($keyValuePairs.path,"%%(.+?)%%*")
$m | % {
$tag = $_.Groups[1].Value
$keyValuePairs.path = $keyValuePairs.path -replace "%%$tag%%", $($keyValuePairs.$tag)
}
"AFTER---->"
Write-Output $keyValuePairs
请注意,如果您想要本质上完全动态的东西,可以通过使用其他方法(例如带有捕获的正则表达式)获取所有占位符来完成,但是根据问题陈述,这似乎是不必要的.
Note, if you wanted something totally dynamic in nature, it can be done by getting all the placeholders by some other method, like a regex with capture, but that seemed unnecessary based on the problem statement.
这篇关于在PowerShell字典中用实际值替换占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!