以小powershell脚本开头的Chef Cookbook,需要将返回的整数值传递给Chef

Chef Cookbook that starts with a small powershell script, need to pass the returned integer value to Chef

我正在尝试完成某人的食谱,我需要能够让 Chef 从该脚本中获取返回的整数 0(一些细节已被删除)关于 Chef 中可能工作的内容有什么建议吗?我已经在 Chef 中添加了 Powershell 模块,具体来说,我想问的是 Chef 解析这些数据的顺序——就像在 action: item to hand this variable to... 谢谢。

include_recipe "chef_handler

powershell_script do
  code <<-EOH
  ###############
  # 
  # Description:
  # Returns 1 if any share or share path allow read/write by the 'Everyone' group (fail)
  # Returns 0 if this condition is not found (pass)


  Try
  {
    #get all shares
    $shares = e | Select-Object Name,Path
    if($shares)
    {
      Foreach ($share in $shares)
      {
        #check everyone permissions
        $shareAccounts = -Name $share.Name
        Foreach ($account in $shareAccounts)
        {
          If ($account.AName -eq 'Everyone')
          {
            return 1
          }
        }

        #check 
        $volumePerm = Get-ACL $share.Path
        if ($volume.Access.Where({$Reference -eq 'Everyone'}))
        {
          return 1
        }
      }
    }
    else
    return 0


    #loop through each share checking for 'Everyone'
    # pass the return values send to Chef or something else.
  }

  {
    #build error message
  }
  Finally
  {
    #return final message
  }
  EOH

  fail "Instance has failed the OpenShares check" if code == 1

end

Chef 资源没有输出值。如果 PowerShell 脚本失败,它将中止 Chef 运行,否则它将继续。

您还可以查看 powershell_script 块的 returns 参数。如果您指定 [0,1] 之类的值,脚本将不会失败。您也可以不指定任何内容并尝试使用 Ruby rescue.

捕获异常

也就是说,如果您确实需要该值,我的建议是将该值写入文件并在过程结束时读回,而不是返回它。

在Ruby.

中读取文件到String可以参考这个article for writing stuff to file using powershell and this