主厨 PowerShell Logging/Write-Host

Chef PowerShell Logging/Write-Host

有没有办法在使用 powershell_script 块时从 Chef 登录到控制台。

一个过于简单的例子:

powershell_script "Something Cool" do
  ignore_failure true
  code <<-EOH
    write-host "Hello World"
  EOH
end

您想混合 powershell_out,它从 powershell 读取输出的方式与 shell_out 从其他 shell 读取的方式相同。根据客户端 12.4.0 的 Chef 变更日志 powershell_out 现在住在核心 Chef https://github.com/chef/chef/blob/master/CHANGELOG.md

编辑:终于让它在我的环境中工作了。请注意,我已锁定综合版本 12.3.0,因此您的体验可能会有所不同。

为了公开 powershell_out 你需要做几件事。

metadata.rb
...
depends 'windows'

<recipe that will be using powershell_out>.rb
...
::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut)
#example usage
should_exist = powershell_out('$true').stdout #=> ['true']

不完全是一个完整的教程,但很多 Bothans 都为向您提供这些信息而付出了代价,我希望这是一个有用的起点。哦,执行此方法会引发此警告:

The powershell_out library in the windows cookbook is deprecated.
Please upgrade to Chef 12.4.0 or later where it is built-in to core chef.

所以在 12.4.0+ 中你应该可以直接使用 powershell_out 资源。

由于ruby代码可以写在cookbooks里面,你可以使用"puts"登录到控制台。