There is a powershell script file on windows minions which has some parameters set the same on all minions and some parameters set individually per-minion. I want to create a state for modifying some global parameters. I cannot replace the whole file, as that would remove the individual parameters.
I thought of using file.line or file.replace, but both run into a wall giving an error related to encoding of the file:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 73: character maps to <undefined>
I checked the file encoding and it is UTF-8 with BOM:
Format-Hex -Path 'C:\Program Files\the_app_name\the_script.ps1' -Count 3
Label: C:\Program Files\the_app_name\the_script.ps1
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 EF BB BF 
I thought it could be solved by simply adding argument "encoding", but according to the documentation, neither file.line nor file.replace accept that parameter. I had no luck reading documentation, searching the internet or consulting LLM.
Does anyone have this experience and did you manage to resolve it?
Here is the salt state:
{% if (grains['os_family'] == 'Windows') %}
update_defaultRetryCount:
file.line:
- name: 'C:/Program Files/the_app_name/the_script.ps1'
- match: '^\$defaultRetryCount'
- mode: replace
- content: '$defaultRetryCount = 6 #Sets the global number of retries for certain situations'
update_defaultRetryTimeout:
file.line:
- name: 'C:/Program Files/the_app_name/the_script.ps1'
- match: '^\$defaultRetryTimeout'
- mode: replace
- content: '$defaultRetryTimeout = 300 #Sets the time in Seconds the script is paused for another retry.'
{% elif grains['id'] == 'SaltMaster'%}
Warning:
event.send:
- data: "Skipping SaltMaster."
{% endif %}
file.managedto replace the whole script.0x88is not a valid UTF8 sequence. This file is likely corrupt to start with.