Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: windows_updates plugin (code in description) #1706

Open
chef-davin opened this issue Oct 31, 2021 · 1 comment
Open

Request: windows_updates plugin (code in description) #1706

chef-davin opened this issue Oct 31, 2021 · 1 comment
Labels
Status: Untriaged An issue that has yet to be triaged.

Comments

@chef-davin
Copy link

chef-davin commented Oct 31, 2021

Describe the Enhancement

I would like to get a list of windows updates either on the system or available to the system.

Describe the Need

I'm working on a resource for installing specific updates on a windows system and it would be helpful if ohai had some info gathering capability so that it didn't have to be a part of the resource and other people could use it for their own resources and reporting.

Current Alternative

Sure, I can put this code into a helper library or into a custom resource in a cookbook. It's not essential that this go in ohai, but I thought it would be a nice to have.

Can We Help You Implement This?

I have the code for the plugin already. I just have never setup tests for ohai PRs and don't have the time to learn it right this minute, but can work on that later if people think this is a good idea to add into the plugin list.

Ohai.plugin(:WindowsUpdates) do
  provides 'windows_updates'
  def get_windows_updates
    cmd = [
    'powershell.exe -NoLogo -NonInteractive -NoProfile -command',
    '"$session = New-Object -ComObject \"Microsoft.Update.Session\";',
    '$updatesearcher = $session.CreateUpdateSearcher();',
    '$searchresult = $updatesearcher.Search(\"IsInstalled=0 or IsInstalled=1\");',
    '$system_updates = @{};',
    'foreach ($update in $searchresult.Updates) {',
        '$this_update = @{',
            '\"$($update.Title)\" = New-Object -Type PSCustomObject -Property @{',
                '\'uuid\' = $($update.Identity.UpdateID);',
                '\'kbid\'   = \"KB$($update.KBArticleIDs)\";',
                '\'isinstalled\' = $($update.IsInstalled);',
                '\'rebootrequired\' = $($update.RebootRequired);',
            '}',
        '};',
        '$system_updates += $this_update',
    '};',
    '$system_updates | ConvertTo-Json"'].join(' ')
    so = shell_out(cmd)

    if so.exitstatus == 0
      JSON.parse(so.stdout)
    end
  end

  collect_data(:windows) do
    require 'json' unless defined?(JSON)

    windows_updates Mash.new
    windows_updates get_windows_updates
  end
end

I've tested this out on windows 10 systems, and it should work as far back as windows 2008r2 I believe.

The only issue here is that using the Microsoft.Update.Session ComObject is a slow thing. If added, we might want to consider making it one of those plugins that's not enabled by default. Just because it will definitely add several seconds to the ohai run.

@chef-davin chef-davin added the Status: Untriaged An issue that has yet to be triaged. label Oct 31, 2021
@tas50
Copy link
Contributor

tas50 commented Jan 7, 2022

@chef-davin Any chance you want to open a PR for this with some tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Untriaged An issue that has yet to be triaged.
Projects
None yet
Development

No branches or pull requests

2 participants