我有这个cmd脚本:
REM Environment-Variables which can be used for Programs
REM ----------------------------------------------------------------
set SDENVDataPool=13
set SDENVDataPoolPath=C:\ProgramData\必须更改值13。我已经尝试过这个解决方案:
$line = Get-Content .\script.cmd | Select-String -Pattern "set SDENVDataPool" | Select-Object -ExpandProperty Line
$content = Get-Content .\script.cmd
$content | ForEach-Object {$_ -replace $line, "set SDENVDataPool=14"} | Set-Content .\script.cmd问题是这一行没有被正确读取。我拿到了整个文件。
有谁可以帮我?
发布于 2021-01-26 00:33:30
在您的Select-String -Pattern "set SDENVDataPool"中,由于两个set SDENVDataPool=13 & set SDENVDataPoolPath=C:\ProgramData\都匹配该模式,因此将返回多行。如果将模式更改为Select-String -Pattern "set SDENVDataPool=",则应正确返回一个匹配项,然后可以在脚本的其余部分中使用该匹配项在运行此部分时设置内容
$content | ForEach-Object {$_ -replace $line, "set SDENVDataPool=14"} | Set-Content .\script.cmd
https://stackoverflow.com/questions/65888389
复制相似问题