Powershell替换文件中的文本

时间:2017-03-22 18:04:28

标签: powershell

我试图用脚本替换文本文件中的文本。我想替换

SSLCertificateFile "D:/folder/location/file.cer"

SSLCertificateFile "D:/folder/location/NewFile.cer"

我写道:

(Get-Content C:\foler\location\httpd-ssl.conf) -replace ('[SSLCertificateFile "D:/folder/location/file.cer"]', 'SSLCertificateFile "D:/folder/location/NewFile.cer"') | Set-Content C:\foler\location\httpd-ssl.conf

这是用SSLCertificateFile替换整个httpd-ssl.conf" D:/folder/location/NewFile.cer"

帮助

1 个答案:

答案 0 :(得分:0)

-replace使用正则表达式,你必须在正则表达式中转义具有特殊含义的字符 ?模式中的方括号是什么?

$Search = [RegEx]::escape('SSLCertificateFile "D:/folder/location/file.cer"')
$Replace= 'SSLCertificateFile "D:/folder/location/NewFile.cer"'
$File =  'C:\foler\location\httpd-ssl.conf'
(Get-Content $File) -replace $Search,$Replace|
 Set-Content $File