hello i have a config file with end like this on many server so i need to add # in the begin of the line
"C:\Documents and Settings\All Users\Application Data\McAfee\DesktopProtection\UpdateLog.txt" 8h warning * 0800 1800
"C:\EURNTCC\mcafee\sucessfull-update.log.txt" 8h warning * 0800 1800
i made a little script like this
$file="\\server\C$\osit\etc\act_mon.cfg"
$text1='"C:\DOCUMENTS AND SETTINGS\ALL USERS\APPLICATION DATA\MCAFEE\DESKTOPPROTECTION\UPDATELOG.TXT"'
$text2='"C:\EURNTCC\MCAFEE\SUCESSFULL-UPDATE.LOG.TXT"'
(gc $file) -replace $text2 ,("#" + $text1) |Set-Content $file
(gc $file) -replace $text2 ,("#" + $text2) |Set-Content $file
i have this error any help to understand this error , i have 650 server to update
The regular expression pattern "C:\EURNTCC\MCAFEE\SUCESSFULL-UPDATE.LOG.TXT" is not valid. At line:1 char:1
+ CategoryInfo : InvalidOperation: ("C:\EURNTCC\MCA...UPDATE.LOG.TXT":String) [], RuntimeException
+ FullyQualifiedErrorId : InvalidRegularExpression
-replace
uses Regex. The strings you want it to search for have characters that have special meaning in regex that need to be prefixed with a backslash. (most notably the dots and backslashes)
You don't have to do this manually but you can use the static method Escape()
from the [regex]
type accelerator:
$text1 = '"C:\DOCUMENTS AND SETTINGS\ALL USERS\APPLICATION DATA\MCAFEE\DESKTOPPROTECTION\UPDATELOG.TXT"'
$text2 = '"C:\EURNTCC\MCAFEE\SUCESSFULL-UPDATE.LOG.TXT"'
$search1 = [regex]::Escape($text1)
$search2 = [regex]::Escape($text2)
Also, you can chain replace
so you don't have to get the content twice:
(Get-Content $file) -replace $search1 ,("#" + $text1) -replace $search2 ,("#" + $text2) | Set-Content $file