Chef powershell_script issue


Hi all,
i am using below powershell script to replace node name in bunch of files.

$path = “E:\Automation_DevOps”
$pattern = “NODE \”
$new_hostname = “$env:computername”

$files = (Get-ChildItem -Path $path -Recurse -Filter “*.pcmd”).fullname
foreach ($file in $files) {
echo $file
#$string = (Get-Content $file | Select-String -AllMatches $change_from)[0]
$old_hostname = (Get-Content $file | Select-String -Pattern $pattern | select -ExpandProperty line).Split("")[1]
Write-Host “old_hostname $old_hostname”
Write-Host “new_hostname $new_hostname”
(Get-Content $file).replace($old_hostname,$new_hostname) | Set-Content $file

if i run powershell script it works well but when i use same code in powershell_script resource in chef it throws below error

Error executing action run on resource ‘powershell_script[Update Configuration]’


Expected process to exit with [0], but received ‘1’
---- Begin output of “C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe” -NoLogo -NonInteractive -NoProfile
-ExecutionPolicy Bypass -InputFormat None -File “C:/Users/SRV-HP~2/AppData/Local/Temp/chef-script20180712-10708-1bxzfeq.
ps1” ----
STDERR: C:\Users\srv-hpaas-chef-admin\AppData\Local\Temp\chef-script20180712-10708-1bxzfeq.ps1 : The string NODE \ is not a valid regular expression: parsing “NODE \” - Illegal \ at end of pattern.
+ CategoryInfo : NotSpecified: (:slight_smile: [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,
---- End output of “C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe” -NoLogo -NonInteractive -NoProfile -E
xecutionPolicy Bypass -InputFormat None -File “C:/Users/SRV-HP~2/AppData/Local/Temp/
1” ----
Ran “C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe” -NoLogo -NonInteractive -NoProfile -ExecutionPolicy
Bypass -InputFormat None -File “C:/Users/SRV-HP~2/AppData/Local/Temp/chef-script20180712-10708-1bxzfeq.ps1” returned 1

Resource Declaration:

In C:/Users/srv-hpaas-chef-admin/.chef/local-mode-cache/cache/cookbooks/wbg_merchantmonitoring/recipes/prognosis_p


8: powershell_script ‘Update Configuration’ do
9: code <<-EOH
10: $path = “E:\Automation_DevOps”
12: $new_hostname = “$env:computername”
14: $files = (Get-ChildItem -Path $path -Recurse -Filter “*.pcmd”).fullname
16: foreach ($file in $files) {
17: echo $file
18: $old_hostname = (Get-Content $file | Select-String -Pattern “#{pattern}” | select -ExpandProperty line).
19: }
20: EOH
21: end

Compiled Resource:

Declared in C:/Users/srv-hpaas-chef-admin/.chef/local-mode-cache/cache/cookbooks/wbg_merchantmonitoring/recipes/pr

ognosis_package_deploy.rb:8:in `from_file’

powershell_script(“Update Configuration”) do
action [:run]
default_guard_interpreter :powershell_script
command nil
backup 5
returns 0
user nil
interpreter “powershell.exe”
declared_type :powershell_script
cookbook_name “wbg_merchantmonitoring”
recipe_name “prognosis_package_deploy”
code “$path = “E:\Automation_DevOps”\n\n$new_hostname = “$env:computername”\n\n$files = (Get-ChildItem -Path
$path -Recurse -Filter “*.pcmd”).fullname\n\nforeach ($file in $files) {\n\techo $file\n\t$old_hostname = (Get-Conten
t $file | Select-String -Pattern “NODE \” | select -ExpandProperty line).Split(”")[1]\n}\n"
domain nil


When using EOH it’s like using double quotes, try adding \ in the path.

$path = “E:\Automatuon_DevOps”


Sorry don’t know why it didn’t show up

$path = “E:\\Automations_DevOps”