0

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 %}
6
  • 1
    Are you able to modify the script to accept passed parameters rather than modify the file? Commented Oct 22 at 17:43
  • the script is deployed on many minions, so not unless I do that manually. which I am trying to avoid. Modifying the file on minions is exactly what I am trying to do. Commented Oct 22 at 22:22
  • Try making a regular text file without the BOM Commented Oct 23 at 1:09
  • 1
    @OndřejJanča they mean just use file.managed to replace the whole script. Commented Oct 26 at 12:03
  • Do note that 0x88 is not a valid UTF8 sequence. This file is likely corrupt to start with. Commented Oct 26 at 12:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.