Windows配方中的厨师NOT_IF和ONLY_IFvalidation问题

我正在运行这个简单的配方块来在IIS中创建一个Web应用程序

powershell_script "create_site_my_site" do code "New-webapppool -name 'My_Web_App'; New-Website -Name 'My_Web_App' -applicationpool 'My_Web_App' -Port '80' -IPAddress * -PhysicalPath 'c:\webs\My_Web_App' " action :run not_if "get-website | where-object { $_.name -eq 'My_Web_App' }" end 

这里的问题是NOT_IF部分它总是真的

 PS C:\Users\carlos> PS C:\Users\carlos> get-website | where-object { $_.name -eq 'asdfasdfasdf' } PS C:\Users\carlos> echo $lastexitcode 1 PS C:\Users\carlos> get-website | where-object { $_.name -eq 'My_Web_App' } Name ID State Physical Path Bindings ---- -- ----- ------------- -------- My_Web_App 6 Stopped c:\webs\My_Web_App http *:80: https *:443: PS C:\Users\carlos.camacho> echo $lastexitcode 1 

现在,我的问题是如何根据我的代码返回值在NOT_IF中返回True或False?

您应该利用Windows cookbook中的powershell_out资源。

如果输出为空,您可以检查命令的错误或不检查。

在你的情况下,这应该做:

 only_if powershell_out("get-website | where-object { $_.name -eq 'My_Web_App' }").stdout.empty? 
 powershell_script "create_site_my_site" do guard_interpreter :powershell_script code "New-webapppool -name 'My_Web_App'; New-Website -Name 'My_Web_App' -applicationpool 'My_Web_App' -Port '80' -IPAddress * -PhysicalPath 'c:\webs\My_Web_App' " action :run not_if "get-website | where-object { $_.name -eq 'My_Web_App' }" end 

您需要指定guard_interpreter :powershell_script 。 注意CHEF-1943并确保guard_interpreter在防守之前并且不要将防守放在一个区块中。

powershell_script文档

这个video是关于Chef powershell提供商的一个很好的参考。

只需使用IIS cookbook就是另一种选择。

我想这是因为你没有告诉not_if 后卫使用powershell_script interperter。

这有用吗?

 powershell_script "create_site_my_site" do code "New-webapppool -name 'My_Web_App'; New-Website -Name 'My_Web_App' -applicationpool 'My_Web_App' -Port '80' -IPAddress * -PhysicalPath 'c:\webs\My_Web_App' " action :run not_if "$(get-website | where-object { $_.name -eq 'My_Web_App' }).count -gt 0" guard_interpreter :powershell_script end