2014-10-27 67 views
0

好吧,所以我卡住了。我試圖讀取.Net文件的版本屬性並將其保存到節點的屬性中。所以我可以在節點的屬性中看到服務器端的安裝程序的版本。廚師PowerShell輸出捕獲到最新的廚師的屬性12

我可以運行PowerShell腳本是這樣的:

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和窗戶食譜現在廚師的基本安裝的一部分,但它似乎powershell_out丟失。我試圖像這樣包括它,但沒有喜悅。

我擁有所有的小角色,但我似乎無法讓他們正確地凝膠。

+0

這已經跟進廚師郵件列表之前需要廚師::密新:: PowershellOut。請不要無理由交叉提問。 – coderanger 2014-10-27 17:42:15

+1

確實,我確實發過帖子,但我認爲它與廣泛的受衆有關,因此我提出了一個問題。 – user230910 2014-10-28 04:47:21

+0

然後只要在這裏問一下,讓兩個小組並行處理這個問題就浪費了50他們的%。 – coderanger 2014-10-28 04:50:23

回答

1

好的,所以問題是file_read在配方的早期階段運行,而不是在它應該的時候結束。以下是任何可能需要將文件讀入屬性並將其發送回服務器的工作版本。

#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 
+1

感謝您跟進!我試圖解決同樣的問題,並且解決了同樣的問題,但是在文本文件中讀回問題(讀入了額外的無效字符)。這解釋瞭如何擺脫它們。 – james 2016-06-24 18:11:26

0

這不是廚師的工作原理。資源沒有這種方式的返回值,大多數不能設置屬性。你將不得不使用ruby_block資源或LWRP,但是這樣做的複雜性很難解釋。我不認爲powershell_out是Chef中的一個實際的東西,你是否認爲這是因爲shell_out而存在,或者你是否在文檔中看到過它?

+0

在互聯網上有很多這方面的參考,這就是爲什麼我認爲它存在:例如:http://kapilshardha.blogspot.ae/2013/12/using-powershellout-mixin-in-chef.html – user230910 2014-10-28 04:39:25

+0

好的,所以這裏的基本想法是使配方報告成功到服務器。這樣做的適當機制是什麼? – user230910 2014-10-28 04:47:59

+0

噢可愛,它確實存在,因爲Windows Cookbook與核心Chef命名空間歪曲https://github.com/opscode-cookbooks/windows/blob/master/libraries/powershell_out.rb :-( – coderanger 2014-10-28 04:49:41