Hi,
I am struggling to execute this powershell script within my recipe. I am trying to add a line to a settings file (only if it's not there already).
Below is what I have been running.
Chef-client runs successfully (it seems to execute the script but doesn't add the setting into the file) The powershell script runs fine manually so the conditions seem fine.
I must be missing something but cannot figure out what!
powershell_script 'update setting x' do
code <<-EOH
if ([bool]((Get-Content -Path "C\path\settings.properties") -contains 'new-setting xyz')) {
write-host "setting already in file. nothing to update"}
else {write-host "adding setting to setting.properties"
Add-Content -Path "C\path\settings.properties" -Value 'new-setting xyz'}
EOH
end
Would really appreciate some advice. I'm quite new to chef-powershell
When it comes to Window systems, you have to have have double backslashes as it thinks a single one is an 'escape' character.
As an example, try using:
C:\\path\\settings.properties
Hi - thanks for your reply. I am using double \ (had just forgotten to put that in when i amended the path name)... I wonder if the contents of the additional setting may be the issue? (have added it below)
powershell_script 'update setting x' do
code <<-EOH
if ([bool]((Get-Content -Path "C\path\settings.properties") -contains '/messageserver[@name\="newname"]/plugin[@name\="newuser\ Configuration"]/property[@name\="newSetting"]=2')) {
write-host "setting already in file. nothing to update"}
else {write-host "adding to setting.properties"
Add-Content -Path "C\path\settings.properties" -Value '/messageserver[@name\="newname"]/plugin[@name="new-user\ Configuration"]/property[@name\="newsetting"]=2'}
EOH
end
This additional settings row has lots of characters and a mixture of quotes - is it possible i'm not quoting it right for chef? - i've just encased it with '. It works fine directly from powershell this way.