if ("$string" -match "$key")
{
Write-Host "Attempting to replace $string with $value in $sourcefilename"
(Get-Content $sourcefilename).Replace("{{$string}}",$value) | set-content $destinationfilename
}
}
有人可以告诉我如何用字符串替换各个值。
最佳答案
下次您提问题时,请确定您的问题。
您要替换字典的键值对中的文本。
您的错误是,您总是阅读$sourcefilename
,替换了文本并写入了destinationfilename
。可能只有字典的最后一个条目被替换了。
因此,您只需阅读一次内容,然后遍历字典并替换值:
$templatecontent = Get-Content $sourcefilename
$Dictionary.Keys | % { $templatecontent = $templatecontent -replace "{{$_}}", ($Dictionary[$_]) }
templatecontent | set-content $destinationfilename
关于xml - 无法替换PowerShell中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36973464/