I'm trying to replace string of text and I get The regular expression pattern is not valid.
This is to be able to change a policy using LGPO
$TextToBeReplaced="Computer`r`nSOFTWARE\Policies\Microsoft\Windows\DeviceGuard`r`nDeployConfigCIPolicy`r`nDWORD:1`r`n`r`nComputer`r`nSOFTWARE\Policies\Microsoft\Windows\DeviceGuard`r`nConfigCIPolicyFilePath`r`nSZ:C:\\WL\\politicas\\DeviceGuardPolicy.bin"
$NewText="Computer`r`nSOFTWARE\Policies\Microsoft\Windows\DeviceGuard`r`nDeployConfigCIPolicy`r`nDELETE`r`n`r`nComputer`r`nSOFTWARE\Policies\Microsoft\Windows\DeviceGuard`r`nConfigCIPolicyFilePath`r`nDELETE"
((Get-Content -path $LGPOTxt -Raw) -replace $TextToBeReplaced,$NewText) | Set-Content -Path $LGPOTxt
LGPO.txt contains this what I have to change is this string for the other in order to apply the new LGPO policy
; ----------------------------------------------------------------------
; PARSING Computer POLICY
; Source file: C:\WL\LGPO\LGPOBackUp\DomainSysvol\GPO\Machine\registry.pol
Computer
SOFTWARE\Policies\Microsoft\Windows\DeviceGuard
DeployConfigCIPolicy
DWORD:1
Computer
SOFTWARE\Policies\Microsoft\Windows\DeviceGuard
ConfigCIPolicyFilePath
SZ:C:\\WL\\politicas\\DeviceGuardPolicy.bin
; PARSING COMPLETED.
; ----------------------------------------------------------------------
Any way to do this?
CodePudding user response:
This is because -replace
operator treats replacement text as regular expression.
<input> -replace <regular-expression>, <substitute>
Instead, you could use replace
method on the string object.
(Get-Content -path $LGPOTxt -Raw).replace($TextToBeReplaced, $NewText) | Set-Content -Path $LGPOTxt