Custom Ohai plugin using powershell script

Hi I hope someone can point me in the right direction. I have a custom ohai plugin that I am using to gather some information about our windows servers. I have been directed to have ohai use a powershell script to gather the information. I am struggling to figure out how to get the local powershell script to return the value back to ohai for reporting purposes

Here is what I have so far:

Ohai.plugin(:HyperVServer) do
require 'json'
provides 'hyperv_networks'

collect_data do
  powershell_script 'Get HyperV Networks' do
  hypervnetworks = code '. c:/scripts/Get-HyperVNetworks.ps1 | ConvertTo-Json'
  end   
  hyperv_server[:hypervnetworks] = hypervnetworks
end

end

but I don't think this will work. Thoughts?

ohai doesn't use the chef resources you'd have to include them by class to make them available in your ohai plugin. Here's a couple of docs on how to write ohai plugin and a powershell example. Writing Ohai Custom Plugins
ohai/powershell.rb at de7b4f4349c767120f223b0569687187082ad73c · chef/ohai · GitHub

Thank you so much for this information. I really appreciate it. This makes sense. So in line with this is it possible for ohai to read a json file on the local system?

yes very common to parse a json file either from local filesystem or from an api call return from other systems.

1 Like