Skip to content

Instantly share code, notes, and snippets.

@btm
Created October 27, 2014 18:49
Show Gist options
  • Save btm/99cd1cc6f843eb1f72f8 to your computer and use it in GitHub Desktop.
Save btm/99cd1cc6f843eb1f72f8 to your computer and use it in GitHub Desktop.
Determine if a KB is installed via win32ole on ruby
# Author:: Bryan McLellan <[email protected]>
# Copyright:: Copyright 2014 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# searches for a KB, but could give a false negative if a KB has been superseded, e.g. a rollup.
require 'rubygems'
require 'win32ole'
kbs = [ "KB2918614", "2966828", "2538242", "2538243", "2972103", "2990967", "3001237" ]
# Returns an IUpdateCollection
def get_updates(query_string)
update_session = WIN32OLE.new("Microsoft.Update.Session")
update_searcher = update_session.CreateUpdateSearcher
results = update_searcher.Search(query_string)
results.Updates
end
def kb_installed?(target_kb)
# Ensure we have just the KB number
target_kb.gsub(/^KB/, '')
update_collection = get_updates('IsInstalled=1')
update_collection.each do |item|
item.KBArticleIDs.each do |kb|
if kb == target_kb
item.SecurityBulletinIDs.each do |id|
puts "KB#{kb}: SecurityBulletinID: #{id}"
end
item.SupersededUpdateIDs.each do |id|
puts "KB#{kb}: SupersededUpdateID: #{id}"
end
return true
end
end
end
return false
end
kbs.each do |kb|
if kb_installed?(kb)
puts "Found #{kb} installed"
else
puts "#{kb} not installed"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment