我正在尝试使用批处理脚本编辑配置文件。我环顾四周,我相信PowerShell是去这里的路。我没有与PowerShell的经验,所以我猜测,语法是什么导致我的问题。使用powershell编辑Windows批处理文件中的文件
这里是什么文件看起来像现在(此部分位于文件的中间)
<!--add key="MinNumCycles" value="25"/-->
<!--add key="MaxNumCycles" value="40"/-->
这里就是我想它看起来像
<!--add key="MinNumCycles" value="25"/-->
<!--add key="MaxNumCycles" value="40"/-->
<!--RerunMode: 1 write to DB, 2 write to DB and add to RUN export/-->
<add key="RerunMode" value="0"/>
这里就是我m试图做我的批处理文件,我需要帮助
SET pattern=<!--add key="MaxNumCycles" value="40"/-->
SET textToAdd1=<!--RerunMode: 1 write to DB, 2 write to DB and add to RUN export/-->
SET textToAdd2=<add key="RerunMode" value="0"/>
SET filename=Software.exe.config
powershell -Command "(gc %filename%) -replace "%pattern%", "$&`n`n%textToAdd1%"'n"%textToAdd2%" | sc %filename%"
我得到了“方法调用失败,因为[System.Object []]没有包含名为'Replace'的方法。我只是在为'$ filename'执行'powershell.exe -ExecutionPolicy Bypass -File script.ps1' – user1984300
,你是否在使用你想要更新的文件的完整路径,偶然?你的调用运行脚本是好的,错误正在确认 –
我在我的机器上测试过它,一切正常......您使用的是什么版本的Powershell?您可以运行'$ PSVersionTable'在版本2.0上检查 –