你好,我试图在PowerShell中运行一个脚本来替换.ini文件中的一行文本。
(Get-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini) | ForEach-Object { $_ -replace "Default NAS Address=","Default NAS Address=BHPAPPDGN01V" } | Set-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini这个脚本可以工作,但是当我多次运行它时,.ini文本一直被添加到行的末尾,给了我一堆垃圾。
示例:运行脚本4次“默认NAS Address=BHPAPPDGN01VBHPAPPDGN01VBHPAPPDGN01VBHPAPPDGN01V”“
发布于 2015-10-30 14:50:47
试试这个:
(Get-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini) | ForEach-Object { $_ -replace 'Default NAS Address=.*','Default NAS Address=BHPAPPDGN01V' } | Set-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini我只是尝试使用regex来选择=后面的所有内容。
发布于 2015-10-30 19:06:28
替换
"-replace“默认NAS Address=
至
"-replace“默认NAS地址=A-Z0-9*”
发布于 2020-02-26 11:39:05
我不喜欢上面的建议,所以这是我的解决方案,以防有人从谷歌跌跌撞撞,发现它有用。
test.ini含量
[Other Parameter] = Something
[My Parameter] = What I wouldnt give for a change of heart
[Other Parameter] = Nothing我的解决方案:
$MyString = Get-Content "C:\Test_Script\test.ini"
$NewString = $MyString.replace("[My Parameter] = What I wouldnt give for a change of heart","[My Parameter] = I gave my heart for a piece of string")
Set-Content -Path "C:\Test_Script\test.ini" -Value $NewStringtest.ini含量变化
[Other Parameter] = Something
[My Parameter] = I gave my heart for a piece of string
[Other Parameter] = Nothinghttps://stackoverflow.com/questions/33438600
复制相似问题