Skip to content

Instantly share code, notes, and snippets.

@keyrage
Created November 6, 2017 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyrage/efaab68cff30c98242225cd94c761214 to your computer and use it in GitHub Desktop.
Save keyrage/efaab68cff30c98242225cd94c761214 to your computer and use it in GitHub Desktop.
##### Check if an application is installed.
# In a lot of cases an MSI doesn't properly get detected by the Chef windows_package resource or is some pther package type. The best approach is to use a guard which checks the uninstaller regkey to check that the package and version exist as a Guard. Note: Replace the items inside the "<< >>" notation with the appropriate name and version. I'll usually use an attribute to allow for changes to be applied at an environment or role level.
#### X64 bit detection or native 32 bit
not_if <<-EOH
$installed=Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall*|where {$_.DisplayName -eq \"<<SOFTWARE DISPLAY NAME>>\"}
if ($installed) {
if([system.version]$installed.DisplayVersion -ge [system.version]\"<<SOFTWARE VERSION NUMBER>>\"){$true}else{$false}
}
else
{$false}
EOH
##### X86 bit detection on a X64 bit architecture
not_if <<-EOH
$installed=Get-ItemProperty HKLM:\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall*|where {$_.DisplayName -eq \"<<SOFTWARE DISPLAY NAME>>\"}
if ($installed) {
if([system.version]$installed.DisplayVersion -ge [system.version]\"<<SOFTWARE VERSION NUMBER>>\"){$true}else{$false}
}
else
{$false}
EOH
#Check if an ISO is mounted.
not_if "((gwmi -Class Win32_LogicalDisk | Where-Object {$_.VolumeName -eq \"<<VOLUME NAME>>\"}).VolumeName -eq \"<<VOLUME NAME>>\")"
@keyrage
Copy link
Author

keyrage commented Mar 5, 2018

Note Guards that use powershell code need the guard_interpreter set to :powershell_script otherwise they will silently fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment