为windows_feature指定厨师提供者

我最初在StackOverflow问这个问题,但没有收到任何工作的答案: https : //stackoverflow.com/questions/18648713/specify-chef-provider-for-windows-feature 。

我试图使用厨师厨师独奏)来pipe理我的Windows Server 2008 R2安装。 厨师提供windows_feature以将angular色/function添加到Windows服务器。 默认情况下, windows_feature使用DISM来安装angular色(如果可用)。 但是据我所知,并不是所有的angular色(如RDS-RD-Server)都可以通过DISM添加。

我大概可以使用Chef::Provider::WindowsFeature::ServerManagerCmd (Windows Cookbook自述文件中标识: https : //github.com/opscode-cookbooks/windows ),但它看起来不像是一个真正的类(浏览那里的源代码)。 此外,servermanagercmd已被弃用(尽pipe它会工作)。

我甚至不介意使用powershell块来添加angular色,但是我很难确保幂等性。 看起来not_if命令shell是一些奇怪的mingwin shell而不是CMD。

以下是我尝试使用powershell的一个示例(不起作用):

 powershell "install_rds_server" do code %Q{ Import-Module Servermanager Add-WindowsFeature RDS-RD-Server }.strip not_if %Q{ powershell "Import-Module Servermanager; $check = get-windowsfeature -name RDS-RD-Server; if ($check.Installed -ne \"True\") { exit 1 }" }.strip end 

我也尝试了以下内容:

 windows_feature 'RDS-RD-Server' do provider Chef::Provider::WindowsFeature::ServerManagerCmd end 

这会返回以下错误:

 FATAL: NameError: uninitialized constant Chef::Provider::WindowsFeature::ServerManagerCmd 

厨师推荐这个angular色的方式是什么?

基于LWRP的厨师文档,我认为在Windows Cookbook中LWRP的实际类名是

 Chef::Provider::WindowsFeatureServermanagercmd 

因此,你应该使用类似的东西

 windows_feature 'RDS-RD-Server' do provider Chef::Provider::WindowsFeatureServermanagercmd end 

Holger Just的解决scheme,或多或less,虽然servermanagercmd.exe弃用消息导致一些问题。 以下是我最终解决问题的方法:

 ps_64 = 'C:\Windows\sysnative\WindowsPowershell\v1.0\powershell.exe' powershell "install_rds_server" do code %Q{ Import-Module Servermanager Add-WindowsFeature RDS-RD-Server }.strip not_if %Q{ #{ps_64} "Import-Module Servermanager; $check = get-windowsfeature -name RDS-RD-Server; if ($check.Installed -ne 'True') { exit 1 }" }.strip end 

我最初的基于Powershell的解决scheme不起作用,因为generic powershell命令启动了32位Powershell。 这个解决scheme仍然非常不方便,但我更喜欢使用已弃用的servermanagercmd.exe