# Powershell not running/not capturing error

**URL:** <https://discourse.chef.io/t/powershell-not-running-not-capturing-error/17154>\
**Category:** Chef Infra (archive)\
**Created:** [May 22, 2020, 2:27pm UTC](https://discourse.chef.io/t/powershell-not-running-not-capturing-error/17154 "2020-05-22T14:27:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![c\_gryga](https://avatars.discourse-cdn.com/v4/letter/c/c4cdca/32.png) [@c\_gryga](https://discourse.chef.io/u/c_gryga)\
**Post date:** [May 22, 2020, 2:27pm UTC](https://discourse.chef.io/t/powershell-not-running-not-capturing-error/17154/1 "2020-05-22T14:27:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Swampcritter](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@Swampcritter](https://discourse.chef.io/u/Swampcritter)\
**Post date:** [May 22, 2020, 2:31pm UTC](https://discourse.chef.io/t/powershell-not-running-not-capturing-error/17154/2 "2020-05-22T14:31:22Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![c\_gryga](https://avatars.discourse-cdn.com/v4/letter/c/c4cdca/32.png) [@c\_gryga](https://discourse.chef.io/u/c_gryga)\
**Post date:** [May 22, 2020, 4:20pm UTC](https://discourse.chef.io/t/powershell-not-running-not-capturing-error/17154/3 "2020-05-22T16:20:32Z")

</div>

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.
