Skip to content

Instantly share code, notes, and snippets.

@Emusp
Created February 15, 2019 13:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Emusp/f08811b691a3a40302e607a09181a2b9 to your computer and use it in GitHub Desktop.
Save Emusp/f08811b691a3a40302e607a09181a2b9 to your computer and use it in GitHub Desktop.
Puppet fact to collect software information on Windows computers.
Facter.add('software') do
confine :kernel => 'windows'
setcode do
require 'win32/registry'
# Generate empty array to store hashes
software_list = []
# Check if reg path exist, return true / false
def key_exists?(path, scope)
begin
Win32::Registry::scope.open(path, ::Win32::Registry::KEY_READ)
return true
rescue
return false
end
end
# Loop through all uninstall keys for 64bit applications.
Win32::Registry::HKEY_LOCAL_MACHINE.open('Software\Microsoft\Windows\CurrentVersion\Uninstall') do |reg|
reg.each_key do |key|
k = reg.open(key)
displayname = k["DisplayName"] rescue nil
version = k["DisplayVersion"] rescue nil
uninstallpath = k["UninstallString"] rescue nil
systemcomponent = k["SystemComponent"] rescue nil
if(displayname && uninstallpath)
unless(systemcomponent == 1)
unless(displayname.match(/[KB]{2}\d{7}/)) # excludes windows updates
software_list << {DisplayName: displayname, Version: version }
end
end
end
end
end
# Loop through all uninstall keys for 32bit applications.
Win32::Registry::HKEY_LOCAL_MACHINE.open('Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall') do |reg|
reg.each_key do |key|
k = reg.open(key)
displayname = k["DisplayName"] rescue nil
version = k["DisplayVersion"] rescue nil
uninstallpath = k["UninstallString"] rescue nil
systemcomponent = k["SystemComponent"] rescue nil
if(displayname && uninstallpath)
unless(systemcomponent == 1)
unless(displayname.match(/[KB]{2}\d{7}/)) # excludes windows updates
software_list << {DisplayName: displayname, Version: version }
end
end
end
end
end
# Loop through all uninstall keys for user applications.
Win32::Registry::HKEY_USERS.open('\\') do |reg|
reg.each_key do |sid|
unless(sid.include?("_Classes"))
path = "#{sid}\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
scope = 'HKEY_USERS'
if key_exists?(path, scope)
Win32::Registry::scope.open(path) do |userreg|
userreg.each_key do |key|
k = userreg.open(key)
displayname = k["DisplayName"] rescue nil
version = k["DisplayVersion"] rescue nil
uninstallpath = k["UninstallString"] rescue nil
if(displayname && uninstallpath)
software_list << {DisplayName: displayname, Version: version }
end
end
end
end
end
end
end
software_list
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment