Warm tip: This article is reproduced from stackoverflow.com, please click
powershell

Replacing a block of text in a text file using powershell

发布于 2020-03-27 10:24:53

Ok before this is marked as duplicate, I have tried most of the suggestions in similar questions on StackOverflow but none of them worked in my case so here I am asking for some help.

First, here is the code extract:

$pathConf = "C:\Program Files (x86)\Zabbix Agent\zabbix_agentd.conf"

$searchedText = 
'### Option: Alias
#   Sets an alias for an item key. It can be used to substitute long and complex item key with a smaller and simpler one.
#   Multiple Alias parameters may be present. Multiple parameters with the same Alias key are not allowed.
#   Different Alias keys may reference the same item key.
#   For example, to retrieve paging file usage in percents from the server:
#   Alias=pg_usage:perf_counter[\Paging File(_Total)\% Usage]
#   Now shorthand key pg_usage may be used to retrieve data.
#   Aliases can be used in HostMetadataItem but not in HostnameItem or PerfCounter parameters.
#
# Mandatory: no
# Range:
# Default:'

$replacedText = 
'### Option: Alias
#   Sets an alias for an item key. It can be used to substitute long and complex item key with a smaller and simpler one.
#   Multiple Alias parameters may be present. Multiple parameters with the same Alias key are not allowed.
#   Different Alias keys may reference the same item key.
#   For example, to retrieve paging file usage in percents from the server:
#   Alias=pg_usage:perf_counter[\Paging File(_Total)\% Usage]
#   Now shorthand key pg_usage may be used to retrieve data.
#   Aliases can be used in HostMetadataItem but not in HostnameItem or PerfCounter parameters.
#
# Mandatory: no
# Range:
# Default:
Alias=my.service.discovery[*]:service.discovery'

((Get-Content -path $pathConf -raw) -replace $searchedText, $replacedText) | Set-Content -Path $pathConf

So here I'm looking for that paragraph of text to replace it by this other paragraph of text to basically add a line in the right place but for some reason this does not work. I've tried adding various EOL characters as suggested in other posts but to no avail. I've seen other post suggesting to loop each line of the file but that doesn't seem optimal in the context. Also, the Select-String $searchedText -Path $pathConf command doesn't return anything (not false, just nothing) so I'm guessing the problem is finding the match rather than replacing it. The file is UTF-8 under Win10.

Disclaimer for those familiar with zabbix, I know I can simply add the text at the EOF for the same results but I like my config files well organized so here I am breaking my head over this problem ;)

So what am I missing or doing wrong here in order to find and replace that paragraph of text? Thank you

Questioner
JulioQc
Viewed
111
AdminOfThings 2019-07-03 23:27

Since you are just replacing literal text with text, you can use the String.Replace method.

(Get-Content -path $pathConf -raw).Replace($searchedText,$replacedText)

The -replace operator uses regex matching to find text. This means you will have to consider special regex characters within your text and deal with them accordingly. You can either individually escape them with \ or use [regex]::Escape($searchedText) to escape your entire search string.