Chef PowerShell 日志记录/Write-Host

3

当使用powershell_script块时,是否有一种方法可以从chef记录到控制台。

一个过于简化的示例:

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

1
您想要混合使用 powershell_out,它以与 shell_out 读取其他 shell 相同的方式读取来自 powershell 的输出。根据 chef 更改日志,在客户端 12.4.0 中,powershell_out 现在位于核心 chef 中 https://github.com/chef/chef/blob/master/CHANGELOG.md 编辑:最终在我的环境中使其工作。请注意,我被锁定在 omnibus 版本 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']

这不是一个完整的教程,但为了获取这些信息,许多波森人付出了生命。我希望这是一个有用的起点。哦,使用此方法会出现以下警告:

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资源。

1
一个使用示例会改善这个答案。(并且一些格式化方法名称也要调整) - Tensibai
同意;但在我的环境中,我被困在12.3.0上,只有经过相当多的痛苦和挫折,我才发现12.4.0是它被合并到核心的版本。 - JackChance
据我所知,PowerShell Cookbook 可以将其带到任何高于 11.0 版本的 Chef(因为自 Chef 客户端 10.8 以来,许多 Windows 功能已从 Windows 和 PowerShell Cookbook 迁移到核心 Chef 客户端)。 - Tensibai
这不是批评,而是一个提高你回答的想法,让它成为真正的答案。我不想通过在http://docs.chef.io上搜索来窃取声望。 - Tensibai
Tensibai,你说得完全正确。现在编辑器上有关于如何实际使用它的信息了。希望能为大家节省一些时间! - JackChance

-4

由于 Ruby 代码可以写在 cookbook 中,您可以使用 "puts" 将日志记录到控制台。


你认为你可以在 PowerShell 脚本中调用 Ruby 方法吗?你可能误读了问题。 - Tensibai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接