# Chef powershell\_script issue

**URL:** <https://discourse.chef.io/t/chef-powershell-script-issue/13288>\
**Category:** Chef Infra (archive)\
**Created:** [July 12, 2018, 6:40am UTC](https://discourse.chef.io/t/chef-powershell-script-issue/13288 "2018-07-12T06:40:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkeshark](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@bkeshark](https://discourse.chef.io/u/bkeshark)\
**Post date:** [July 12, 2018, 6:40am UTC](https://discourse.chef.io/t/chef-powershell-script-issue/13288/1 "2018-07-12T06:40:59Z")

</div>

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]'

## Mixlib::ShellOut::ShellCommandFailed

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" ----  
STDOUT:  
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: (🙂 [Write-Error], WriteErrorException  
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,chef-script20180712-10708-1bxzfeq.ps  
1  
---- 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/chef-script20180712-10708-1bxzfeq.ps  
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

ackage\_deploy.rb

8: powershell\_script 'Update Configuration' do  
9: code \<\<-EOH  
10: $path = "E:\Automation\_DevOps"  
11:  
12: $new\_hostname = "$env:computername"  
13:  
14: $files = (Get-ChildItem -Path $path -Recurse -Filter "\*.pcmd").fullname  
15:  
16: foreach ($file in $files) {  
17: echo $file  
18: $old\_hostname = (Get-Content $file | Select-String -Pattern "#{pattern}" | select -ExpandProperty line).  
Split("")[1]  
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  
end

---

<div class="post-metadata">

**Author:** ![Sydnguyen](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@Sydnguyen](https://discourse.chef.io/u/Sydnguyen)\
**Post date:** [July 12, 2018, 7:03am UTC](https://discourse.chef.io/t/chef-powershell-script-issue/13288/2 "2018-07-12T07:03:23Z")

</div>

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

$path = “E:\Automatuon\_DevOps”

---

<div class="post-metadata">

**Author:** ![Sydnguyen](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@Sydnguyen](https://discourse.chef.io/u/Sydnguyen)\
**Post date:** [July 12, 2018, 7:06am UTC](https://discourse.chef.io/t/chef-powershell-script-issue/13288/3 "2018-07-12T07:06:40Z")

</div>

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

$path = “E:\\Automations\_DevOps”
