替換實際值的佔位符下面是示例XML文件:在PowerShell中詞典
<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的字典。
[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
我得到了預期的輸出。
Key Value
----- -----
type Dev
client Arizona
dataSource Local
path App_Data\%%type%%\%%client%%_%%dataSource%%
但將元素轉換爲鍵值對後,我想用actualy值替換佔位符。換句話說,元件的「路徑」有3個佔位符,他們是
1. type
2. client
3. dataSource
基本上佔位符是指開始的字符串和具有兩個百分比(%% XYZ %%)結束。所以,我需要用實際值替換佔位符之後,下面的輸出:
Key Value
----- -----
type Dev
client Arizona
dataSource Local
path App_Data\Dev\Arizona_Local
能有人幫我,我怎麼能實現這一目標使用PowerShell。提前致謝。
你有什麼麻煩?匹配和替換字符串,或更改字典對象中的值? –
我遇到匹配和替換字符串的問題。 – mahesh