に実際の値を持つプレースホルダを交換後は、サンプルXMLファイルです:私は次のスクリプトを使用してPowerShellの辞書に上記の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>
。
[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%%
しかし、要素をキー値のペアに変換した後、プレースホルダを実際の値に置き換えたいとします。換言すれば、要素「パス」は3つのプレースホルダを有し、それらは基本的に、プレースホルダ
1. type
2. client
3. dataSource
あり始まり二パーセント(%% XYZ %%)で終わる文字列を意味します。だから、私は実際の値でプレースホルダを交換した後、次の出力が必要になります。
Key Value
----- -----
type Dev
client Arizona
dataSource Local
path App_Data\Dev\Arizona_Local
誰かが、私はPowerShellを使用して、これをachiveすることができますどのように私を助けてください。前もって感謝します。
どの部分で問題がありますか?文字列の照合と置換、またはディクショナリオブジェクトの値の変更 –
文字列のマッチングと置き換えに問題があります。 – mahesh