# Windows and powershell\_out

**URL:** <https://discourse.chef.io/t/windows-and-powershell-out/5854>\
**Category:** Chef Infra (archive)\
**Created:** [October 27, 2014, 9:06am UTC](https://discourse.chef.io/t/windows-and-powershell-out/5854 "2014-10-27T09:06:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iain\_Prior](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@Iain\_Prior](https://discourse.chef.io/u/Iain_Prior)\
**Post date:** [October 27, 2014, 9:06am UTC](https://discourse.chef.io/t/windows-and-powershell-out/5854/1 "2014-10-27T09:06:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Iain\_Prior](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@Iain\_Prior](https://discourse.chef.io/u/Iain_Prior)\
**Post date:** [October 27, 2014, 11:01am UTC](https://discourse.chef.io/t/windows-and-powershell-out/5854/2 "2014-10-27T11:01:52Z")

</div>

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](mailto:iain.prior@gac.com)]  
Sent: Monday, October 27, 2014 1:07 PM  
To: [chef@lists.opscode.com](mailto: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

---

<div class="post-metadata">

**Author:** ![Tensibai](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tensibai/32/29_2.png) [@Tensibai](https://discourse.chef.io/u/Tensibai)\
**Post date:** [October 27, 2014, 11:43am UTC](https://discourse.chef.io/t/windows-and-powershell-out/5854/3 "2014-10-27T11:43:17Z")

</div>

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](mailto:iain.prior@gac.com)]  
> Sent: Monday, October 27, 2014 1:07 PM  
> To: [chef@lists.opscode.com](mailto: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

---

<div class="post-metadata">

**Author:** ![jdunn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jdunn/32/1800_2.png) [@jdunn](https://discourse.chef.io/u/jdunn)\
**Post date:** [October 28, 2014, 3:42am UTC](https://discourse.chef.io/t/windows-and-powershell-out/5854/4 "2014-10-28T03:42:14Z")

</div>

On Mon, Oct 27, 2014 at 7:43 AM, Tensibai Zhaoying [tensibai@iabis.net](mailto: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](mailto:jdunn@aquezada.com) \* Sorry, I'm ]  
[WWW: [http://www.aquezada.com/staff/julian](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]

---

<div class="post-metadata">

**Author:** ![Iain\_Prior](https://avatars.discourse-cdn.com/v4/letter/i/9d8465/32.png) [@Iain\_Prior](https://discourse.chef.io/u/Iain_Prior)\
**Post date:** [October 28, 2014, 4:46am UTC](https://discourse.chef.io/t/windows-and-powershell-out/5854/5 "2014-10-28T04:46:32Z")

</div>

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](mailto:jdunn@aquezada.com)]  
Sent: Tuesday, October 28, 2014 7:42 AM  
To: [chef@lists.opscode.com](mailto: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](mailto: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](mailto:jdunn@aquezada.com) \* Sorry, I'm ]  
[WWW: [http://www.aquezada.com/staff/julian](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]

---

<div class="post-metadata">

**Author:** ![jdunn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jdunn/32/1800_2.png) [@jdunn](https://discourse.chef.io/u/jdunn)\
**Post date:** [October 28, 2014, 3:19pm UTC](https://discourse.chef.io/t/windows-and-powershell-out/5854/6 "2014-10-28T15:19:23Z")

</div>

On Tue, Oct 28, 2014 at 12:46 AM, Iain Prior [iain.prior@gac.com](mailto: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](mailto:jdunn@aquezada.com) \* Sorry, I'm ]  
[WWW: [http://www.aquezada.com/staff/julian](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]
