Working with Powershell Output

Hey i am currently working with recipes and i encountered one thing.
I would like to work with the output of a Powershell Script and I do not really know what the best approach would be for this task.
Best thing would be to store the information in a variable , so I could use it in another recipe , is something like that possible?
I found the Chef::Mixin::PowershellOut but I could not figure out how I could use that optimally.

My Code right now :

require "chef/mixin/powershell_out"
::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut)

powershell_script 'execute_jfrog_cli' do
code <<-EOH
$JFrogPath = 'E:/'
Set-Location -Path $JFrogPath
if (Test-Path -Path $JFrogPath/jfrog.exe) {
$JFrogOutput = .\jfrog.exe rt search --user=user--password=password --url='https://someUrl --props "someProps=someProps" PATH/
}
EOH
end

I use PowerShell output for determining some conditionals in several of my recipes.

Here is an example of where I pull the startup type of a Windows service to use as a conditional. The use case is odd, so ignore that, but this should help with context:

::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut)

script = <<-EOF

(Get-Service -name "#{winservice}").StartType

EOF

startuptype = powershell_out(script)

Brian

1 Like

Does this method work for multiple lines of commands?
I tried to do that with a block of code but it seems not to work somehow , do you have any ideas why?
Thank you

You should be able to use as much PowerShell code within the EOF block as desired being a simple 1 liner like I posted or 20 lines of code. The key being that whatever you execute within the PowerShell has to have an output that can be captured and then used. And if what you are executing does not natively create an output to capture, you might consider using Write-Output in your PowerShell code to create an output.

1 Like