您好我正在尝试在PowerShell ISE中运行脚本来替换.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文本不断添加到行的末尾给我一堆垃圾。
示例:Ran脚本4次"默认NAS 地址= BHPAPPDGN01VBHPAPPDGN01VBHPAPPDGN01VBHPAPPDGN01V"
答案 0 :(得分:2)
试试这个:
(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
我只是尝试使用正则表达式来选择=
背后的evrything。
答案 1 :(得分:0)
替换
" -replace"默认NAS地址="
到
" -replace"默认NAS地址= [A-Z0-9] *"
答案 2 :(得分:0)
我不喜欢上面的建议,所以这是我的解决方案,以防有人从Google偶然发现它有用。
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 $NewString
更改了test.ini的内容
[Other Parameter] = Something
[My Parameter] = I gave my heart for a piece of string
[Other Parameter] = Nothing