[转]Powershell中替换文件内容的方法
经某度搜索,好像全世界的方法是:
1 | Get-Content $zabbix_agent | Foreach-Object {$_ -replace "ServerActive=127\.0\.0\.1" , "ServerActive=100\.100\.100\.100" } | Out-File $zabbix_agent -Verbos -Force |
如果你用了就大错特错了,这条命令永远会报错:
Set-Content : 文件“”正由另一进程使用,因此该进程无法访问此文件。
正确并实际使用的方法是:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Function sed ($Filename, $Oldvalue, $Newvalue) { if (Test-Path $Filename) { $content = get-content $Filename clear -content $Filename foreach($line in $content) { $liner=$line.Replace($Oldvalue, $Newvalue) Add-content $Filename -Value $liner } } } sed "c:\zabbix.conf" "Server=127.0.0.1" "Server=100.100.100.100" |
————————————————
原文链接:https://blog.csdn.net/d9394952/article/details/118329737
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步