Windows and powershell_out

Hi guys

I have managed to properly confuse myself well and truly!

Scenario: Basic Chef installation, server is Ubuntu 12.04, client is windows 2012 server.

What I want to do: Read the “version” property of a file into an attribute of a node.

What I’ve tried: You can see my attempt below. It seems that the PowerShellOut “mixin” is not available, but I’m too scared to add the windows and powershell cookbooks to my collection, because I understand that they are already included in the latest chef. So now I’m stuck! Help!!!

The powershell code below runs and creates the text file fine, but how do I get the value from the text file into the attribute?

The Code:

powershell_script ‘filever’ do
code <<-EOH
[System.Diagnostics.FileVersionInfo]::GetVersionInfo(“C:\Program Files\Internet Explorer\iexplore.exe”).FileVersion > C:\windows\temp\fileversion.txt
get-content -Path C:\windows\temp\fileversion.txt
EOH
end

powershell_script ‘filever2’ do
::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut)
#include Chef::Mixin::PowershellOut
node.normal[:ixserver][:Hello] = powershell_out(“echo hello”)
end

I’m hoping you guys can help…

Many regards
Iain

Ok I made a little more progress…

But the problem is that the order is not correct, the file doesn’t exist when reportback runs…?

powershell_script ‘filever’ do
code <<-EOH
[System.Diagnostics.FileVersionInfo]::GetVersionInfo(“C:\Program Files\Internet Explorer\iexplore.exe”).FileVersion > C:\windows\temp\fileversion.txt
EOH
end

execute “reportback” do
command "echo 1"
node.normal[:ixserver][:Version_Installed] = File.read(“C:\windows\temp\fileversion.txt”)
only_if { ::File.exists?(“C:\windows\temp\fileversion.txt”) }
end

-----Original Message-----
From: Iain Prior [mailto:iain.prior@gac.com]
Sent: Monday, October 27, 2014 1:07 PM
To: chef@lists.opscode.com
Subject: [chef] Windows and powershell_out

Hi guys

I have managed to properly confuse myself well and truly!

Scenario: Basic Chef installation, server is Ubuntu 12.04, client is windows 2012 server.

What I want to do: Read the “version” property of a file into an attribute of a node.

What I’ve tried: You can see my attempt below. It seems that the PowerShellOut “mixin” is not available, but I’m too scared to add the windows and powershell cookbooks to my collection, because I understand that they are already included in the latest chef. So now I’m stuck! Help!!!

The powershell code below runs and creates the text file fine, but how do I get the value from the text file into the attribute?

The Code:

powershell_script ‘filever’ do
code <<-EOH
[System.Diagnostics.FileVersionInfo]::GetVersionInfo(“C:\Program Files\Internet Explorer\iexplore.exe”).FileVersion > C:\windows\temp\fileversion.txt
get-content -Path C:\windows\temp\fileversion.txt
EOH
end

powershell_script ‘filever2’ do
::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut)
#include Chef::Mixin::PowershellOut
node.normal[:ixserver][:Hello] = powershell_out(“echo hello”) end

I’m hoping you guys can help…

Many regards
Iain

The file don't exist when the only_if run at compile time. Run your powershell resource at compile time with end.run_action(:create) (see the default action I'm unsure)

At all it sounds you may be on the wrong way to achieve your goal, but without more details it's hard to give advices.

---- Iain Prior a écrit ----

Ok I made a little more progress..

But the problem is that the order is not correct, the file doesn’t exist when reportback runs..?

powershell_script 'filever' do
code <<-EOH
[System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Program Files\Internet Explorer\iexplore.exe").FileVersion > C:\windows\temp\fileversion.txt
EOH
end

execute "reportback" do
command "echo 1"
node.normal[:ixserver][:Version_Installed] = File.read("C:\windows\temp\fileversion.txt")
only_if { ::File.exists?("C:\windows\temp\fileversion.txt") }
end

-----Original Message-----
From: Iain Prior [mailto:iain.prior@gac.com]
Sent: Monday, October 27, 2014 1:07 PM
To: chef@lists.opscode.com
Subject: [chef] Windows and powershell_out

Hi guys

I have managed to properly confuse myself well and truly!

Scenario: Basic Chef installation, server is Ubuntu 12.04, client is windows 2012 server.

What I want to do: Read the "version" property of a file into an attribute of a node.

What I've tried: You can see my attempt below. It seems that the PowerShellOut "mixin" is not available, but I'm too scared to add the windows and powershell cookbooks to my collection, because I understand that they are already included in the latest chef. So now I'm stuck! Help!!!

The powershell code below runs and creates the text file fine, but how do I get the value from the text file into the attribute?

The Code:

powershell_script 'filever' do
code <<-EOH
[System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Program Files\Internet Explorer\iexplore.exe").FileVersion > C:\windows\temp\fileversion.txt
get-content -Path C:\windows\temp\fileversion.txt
EOH
end

powershell_script 'filever2' do
::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut)
#include Chef::Mixin::PowershellOut
node.normal[:ixserver][:Hello] = powershell_out("echo hello") end

I'm hoping you guys can help..

Many regards
Iain

On Mon, Oct 27, 2014 at 7:43 AM, Tensibai Zhaoying tensibai@iabis.net wrote:

The file don't exist when the only_if run at compile time. Run your
powershell resource at compile time with end.run_action(:create) (see the
default action I'm unsure)

Not sure what the problem is here exactly but I just wanted to clarify
that guards don't run at compile time; they run at execute time, so
that can't be the problem.

Could you try using "| Out-File C:\windows\temp\fileversion.txt"
rather than the ">" redirection?

  • Julian

--
[ Julian C. Dunn jdunn@aquezada.com * Sorry, I'm ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]

Hi all

Thanks for the help and advice so far. I managed to get it working, but I understand that I'm missing some important concept here. I am trying to set an attribute on the node that I can see in the website that will show me that the process has completed correctly. Hence getting the version of the installed file and reporting back.

Now what is the "right" way to do this? How do I tell the chef-server that the recipe has completed correctly?

Here is the working version of the code if anyone needs it in future. The problem was that the File.read was running too early in the process, I think just after compile and before the "convergence" step that runs all the code. Putting it in a ruby_block makes it wait until it's turn to run.

#Check the installed version
powershell_script 'filever' do
code <<-EOH
[System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Program Files\Internet Explorer\iexplore.exe").FileVersion > c://chef//fileversion.txt
EOH
end

#report back after all is done
ruby_block "reportback" do
block do
encoding_options = {
:invalid => :replace, # Replace invalid byte sequences
:undef => :replace, # Replace anything not defined in ASCII
:replace => '', # Use a blank for those replacements
:universal_newline => true # Always break lines with \n
}
node.normal[:ixserver][:Version_Installed] = File.read("C:\chef\fileversion.txt").encode(Encoding.find('ASCII'), encoding_options)
end
end

Many thanks
Iain

-----Original Message-----
From: Julian C. Dunn [mailto:jdunn@aquezada.com]
Sent: Tuesday, October 28, 2014 7:42 AM
To: chef@lists.opscode.com
Subject: [chef] Re: Re: RE: Windows and powershell_out

On Mon, Oct 27, 2014 at 7:43 AM, Tensibai Zhaoying tensibai@iabis.net wrote:

The file don't exist when the only_if run at compile time. Run your
powershell resource at compile time with end.run_action(:create) (see
the default action I'm unsure)

Not sure what the problem is here exactly but I just wanted to clarify that guards don't run at compile time; they run at execute time, so that can't be the problem.

Could you try using "| Out-File C:\windows\temp\fileversion.txt"
rather than the ">" redirection?

  • Julian

--
[ Julian C. Dunn jdunn@aquezada.com * Sorry, I'm ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]

On Tue, Oct 28, 2014 at 12:46 AM, Iain Prior iain.prior@gac.com wrote:

Thanks for the help and advice so far. I managed to get it working, but I understand that I'm missing some important concept here. I am trying to set an attribute on the node that I can see in the website that will show me that the process has completed correctly. Hence getting the version of the installed file and reporting back.

By "website" do you mean the Chef Server UI?

Now what is the "right" way to do this? How do I tell the chef-server that the recipe has completed correctly?

I'm trying to understand what you're trying to accomplish in order to
guide you better. As long as a recipe runs through to completion, that
fact is reported to the Chef server by default; and if a Chef run
completes, all the recipes were run correctly. So those are all things
you can get from the Chef Server without having to write any code.

Also, is the storing of the Internet Explorer version literally what
you are trying to accomplish? Or is that just an example?

  • Julian

--
[ Julian C. Dunn jdunn@aquezada.com * Sorry, I'm ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]