# Get return value from powershell into attribute

**URL:** <https://discourse.chef.io/t/get-return-value-from-powershell-into-attribute/5613>\
**Category:** Chef Infra (archive)\
**Created:** [August 10, 2014, 7:14am UTC](https://discourse.chef.io/t/get-return-value-from-powershell-into-attribute/5613 "2014-08-10T07:14:14Z")\
**Posts on this page:** 2\
**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:** [August 10, 2014, 7:14am UTC](https://discourse.chef.io/t/get-return-value-from-powershell-into-attribute/5613/1 "2014-08-10T07:14:14Z")

</div>

Hi guys

I have been bashing my head against this for hours, and I’m sure there is something simple and obvious that I just don’t know…

I want to run a powershell script to get the FileVersion attribute of a file and save that into a chef attribute on the server.

Here is the recipe I’m struggling with. Please help?

::Chef::Resource::PowershellScript.send(:include, Chef::Mixin::PowershellOut)

powershell\_script “read-version” do  
cwd Chef::Config[:file\_cache\_path]  
code \<\<-EOH  
sleep 5  
[System.Diagnostics.FileVersionInfo]::GetVersionInfo(“C:\Program Files\xxx\yyy\zzz.dll”).FileVersion | fl  
Return  
EOH  
node.default[:gac\_integration][:version] = powershell\_out(code).stdout  
node.default[:gac\_integration][:version] = powershell\_out(code).stderr  
end

Here is the error I am getting…

At line:1 char:58 + [System.Diagnostics.FileVersionInfo]::GetVersionInfo(C:\Program Files\Gulf A … + ~ Missing ‘)’ in method call. At line:1 char:58 + [System.Diagnostics.FileVersionInfo]::GetVersionInfo(C:\Program Files\Gulf A … + ~~~~~~~~~~ Unexpected token ‘C:\Program’ in expression or statement. At line:1 char:130 + … Integration.dll).FileVersion | fl + ~ Unexpected token ‘)’ in expression or statement. + CategoryInfo : ParserError: (🙂 [], ParentContainsErrorRecordEx ception + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

Many regards  
Iain

---

<div class="post-metadata">

**Author:** ![Adam\_Edwards1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/adam_edwards1/32/27_2.png) [@Adam\_Edwards1](https://discourse.chef.io/u/Adam_Edwards1)\
**Post date:** [August 10, 2014, 3:10pm UTC](https://discourse.chef.io/t/get-return-value-from-powershell-into-attribute/5613/2 "2014-08-10T15:10:16Z")

</div>

Hey, no monkeypatching PowershellScript. J

Can you try this without powershell\_out and see if you can get the same  
failure? I had no issues with the following fragment run via chef-apply – I  
just piped the output to a file in my recipe and after running chef-apply I  
verified the contents of the output file:

powershell\_script ‘filever’ do

code \<\<-EOH

Files\Internet Explorer\iexplore.exe").FileVersion \> ~\fileversion.txt

EOH

End

Chef-apply returned successfully when running a file with those contents  
and produced the following results:

11.00.9600.16384 (winblue\_rtm.130821-1623)

Thanks Iain, I’m definitely curious as to what’s causing this and if  
there’s something we can do to prevent it.

-Adam

_From:_ Iain Prior [[mailto:iain.prior@gac.com](mailto:iain.prior@gac.com)]  
_Sent:_ Sunday, August 10, 2014 12:14 AM  
_To:_ [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
_Subject:_ [chef] Get return value from powershell into attribute

Hi guys

I have been bashing my head against this for hours, and I’m sure there is  
something simple and obvious that I just don’t know…

I want to run a powershell script to get the FileVersion attribute of a  
file and save that into a chef attribute on the server.

Here is the recipe I’m struggling with. Please help?

::Chef::Resource::PowershellScript.send(:include,  
Chef::Mixin::PowershellOut)

powershell\_script “read-version” do

cwd Chef::Config[:file\_cache\_path]

code \<\<-EOH

```
sleep 5

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\\Program

```

Files\xxx\yyy\zzz.dll").FileVersion | fl

Return

EOH

node.default[:gac\_integration][:version] = powershell\_out(code).stdout

node.default[:gac\_integration][:version] = powershell\_out(code).stderr

end

Here is the error I am getting…

At line:1 char:58 +  
System.Diagnostics.FileVersionInfo::GetVersionInfo(C:\Program Files\Gulf  
A … + ~ Missing ‘)’ in method call. At line:1 char:58 +  
System.Diagnostics.FileVersionInfo::GetVersionInfo(C:\Program Files\Gulf  
A … + ~~~~~~~~~~ Unexpected token ‘C:\Program’ in expression or  
statement. At line:1 char:130 + … Integration.dll).FileVersion | fl + ~  
Unexpected token ‘)’ in expression or statement. + CategoryInfo :  
ParserError: (🙂 [], ParentContainsErrorRecordEx ception +  
FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

Many regards

Iain
