如何使用返回值 *.exe 将不完全是 XML 的文件内的 XML 替换为 PowerShell



我有一个特殊格式的配置文本文件,其中有 1 行以 wcfconfig,Text,<?xml version... 开头,我正在尝试使用 PowerShell 将整个 XML 块替换为从可执行文件返回的块。 文本文件(例如:C:\Temp\MyText.txt)如下所示:

Special Export File
Format: 1
Configuration: MyConfig
    startupcmd,Text,
    extracmdline,Text,
    wsdlport,Text,9500
    useserverprinters,Int,1
    aosencryption,Text,1
    dbserver,Text,Dev-SERVER
    wcfconfig,Text,<?xml version="1.0" encoding="utf-8"?><configuration>...
    hint,Text,

当使用服务器/端口调用时,我有一个可执行文件并返回一个 XML 字符串。

C:MyFile.exe Dev-SERVER 9500

其中Dev-Server来自文本文件中的dbserver9500来自文本文件中的wsdlport

它返回一个与上面wcfconfig完全相同的格式的 XML 字符串,我只想在 1 行上完全替换它:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <client>
    </client>
</configuration>

由于我没有你的环境,我不得不伪造一些东西。C:MyFile.exe Dev-SERVER 9500 的输出由此处字符串$return

表示
$return = @"
<?xml version="1.1" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <client>
    </client>
</configuration>
"@
(Get-Content "C:TempMyText.txt") -replace '^(s*?wcfconfig,Text,).*',"`$1$($replace -replace "`r`n")"

获取文件"C:\Temp\MyText.txt"的内容,并使用-replace找到您尝试更新的行。将其替换为<?xml之前的所有文本,然后在删除新行的情况下附加文件数据。

某些元素之间会有空白> <。如果这是一个问题,则应-replace处理该问题。 -replace ">s+?<","><"

最新更新