如何将 powershell 参数传递给 Chef

How to pass powershell parameters into Chef

出于某种原因,当我通过选项部分传递参数时,它不喜欢我的 powershell 参数。我不想打扰你们,但是你们有什么想法可能是什么问题吗?提前致谢!

基本上我想用 powershell 做的就是这个 -

Start-Process -FilePath "T:\QualysCloudAgent.exe" -argumentlist "CustomerId={etc..etc..etc..} ActivationId={etc..etc..etc..}"

when 'windows'
  windows_package 'QualysCloudAgent' do
    source 'T:\QualysCloudAgent.exe'
    options '-argumentlist "CustomerId={etc...etc...etc..} ActivationId={etc...etc..etc...}"'
    installer_type :custom
    action :install
  end
end

您不是 运行 powershell.exe - 除非 QualysCloudAgent.exe 接受 -ArgumentList 参数,否则您的可执行文件可能无法理解该参数。只需将您的选项传递给资源,如下所示:

options %Q(CustomerId={etc...etc...etc..} ActivationId={etc...etc..etc...})

您可能还想考虑使用 guard,以确保在收敛期间保持幂等性(例如,如果已经安装则不要安装)。例如,在您的资源中,您可以在上面的块中执行类似的操作,因为您设置了 :custom installer_type

not_if %Q(# PowerShell code to check if the program is installed)
guard_interpreter :powershell_script

在资源上设置 guard_interpreter 告诉 Chef 将资源下的 not_ifonly_if 字符串评估为该语言。请参阅链接页面,了解 Chef 在此上下文中支持的不同运行时。


旁注:%Q() 是 Ruby 中用于双引号字符串文字的快捷方式,还有一个额外的好处 - 您不必再转义双引号(尽管您将不得不引用未打开的右括号,例如 %Q(My name is Bender\(),但 %Q(My name is (Bender)) 不需要),这通常对构建外部命令参数有很大帮助,尤其是在 Chef 中。 This section of the Ruby wikibook 解释了更多关于不同文字的替代符号,您可能会在其他情况下找到 useful/graceful。