Powershell_script execute failure on windows


   Need an help on powershell resource for windows, I am planning to execute the below powershell resource on windows and i not able to get the desired result.

powershell_script "To update a script" do
cwd 'C:\Program Files\Sas\Sas install 1.7.0'
code <<-EOH
$body = sapkg.exe installkey
$tango = $body | ForEach-Object { $_.split(":")[1] }

The above script tries to go to location of c: at sas and execute the sapkg with argument installkey, we will get the key. i am not able to see the key at all. further if we get the key it will split them based on colon.

But i cant see the powershell script getting executed, it does not even through error. when i go to specific location and execute them i can see the key. But executing via chef resource i cant see the key.

Any help highly apprecited.



Might I suggest if you wish to see output, that you write the script in a file and use Transcript logging to a file so you can see what is occurring on the console. Embedded code like this when the agent is run is copied and put into a script that Chef creates and executes, afterwards the powershell session is terminated and the script that was created is removed. The method that works best for us is using the block to execute the script instead of using embedded code.

powershell_script "label name" do
code “c:\path_to\my_script.ps1”

this executes a powershell script you wrote and you don’t have to worry about the embedded code and some characters getting misinterpreted by ruby like backslashes and special characters.

Tran scripting is a built in ability of PowerShell to actually pipe output to a log file, to output simply use “Write-Output” in stead of using “Write-Host”, also a good portion of the what is visible during the session is also written to the log.