The version variable only lives within the code block of your powershell_script resource. It will not be a global variable and you will not be able to reference it by other resources.
What you are looking for is the powershell_out mixin
require "chef/mixin/powershell_out"
include Chef::Mixin::PowershellOut
script = <<-EOH
# your_awsome_ps_script
EOH
result = powershell_out(script)
I believe using Ohai is the best way to do this however using PowerShell in this way like the OP wants to is not a bad idea for edge cases and something that I’ve done. In addition if you’re not great with Ruby this might be useful.
I am trying to find the version of python installed (manual) in windows server and compare with the one which i am going to install through chef. if versions are different then chef will uninstall old python and install specified version in cookbook.
I need to find out the old version of python and remove it from windows machine, as i mentioned in the above thread , i am not able to read the output (old python version) into variable from powershell and use that variable to remove package.
Below is my installation script , if possible please modify it and help.
powershell_script 'check-python-status' do
code "$true"
flags '-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass'
guard_interpreter :powershell_script
only_if 'If ((Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -eq "Python 2.7.12 (64-bit)"}) -eq $null) { Return $True } Return $False'
notifies :install, 'windows_package node['python2.7.12']['package_name']', :immediate
end
windows_package node['python2.7.12']['package_name'] do
source node['python2.7.12']['url']
checksum "#{node['python2.7.12']['checksum']}"
end
#Add python installation path to environment variable 'path'
env 'Path' do
delim ";"
value "#{node['python2.7.12']['path']}"
:modify
end
#add python scirpts path to environment variable 'path'
env 'Path' do
delim ";"
value "#{node['python2.7.12']['path']}\\Scripts"
:modify
end