NoPaste

CheckMK Installer/Updater in Powershell

von heisenberg

SNIPPET_TEXT:
  1. #
  2. # Install/Upgrade Check-MK-Agent from URL
  3. #
  4.  
  5. function get_installed_software_info {
  6.         param (
  7.                 [string]$pattern
  8.         )
  9.     $data = Get-ItemProperty $global:reg_key_sw_uninstall  |
  10.         Select-Object DisplayName, DisplayVersion          |
  11.         Where-Object{$_.DisplayName -match $pattern}
  12.     if ($data) {
  13.         return @{
  14.             name    = $data.DisplayName.Trim()
  15.             version = $data.DisplayVersion.Trim()
  16.             }
  17.     }
  18. }
  19.  
  20. function get_msi_db_property {
  21.     param (
  22.         $msidb,                       # opened msi database object System.__ComObject
  23.         [string]$wanted_property      # wanted property of the MSI file
  24.     )
  25.     $view=$null
  26.     $query = "SELECT Value FROM Property WHERE Property = '$($wanted_property)'"
  27.    
  28.     $view = $msidb.GetType().InvokeMember('OpenView', 'InvokeMethod', $null, $msidb, ($query))
  29.  
  30.     $view.GetType().InvokeMember('Execute', 'InvokeMethod', $null, $view, $null)
  31.  
  32.     $record = $view.
  33.         GetType().
  34.         InvokeMember('Fetch', 'InvokeMethod', $null, $view, $null)
  35.  
  36.     $value  = $record.
  37.         GetType().
  38.         InvokeMember('StringData', 'GetProperty', $null, $record, 1)
  39.  
  40.     return $value
  41.  
  42. }
  43.  
  44. function get_msi_product_info {
  45.     param (
  46.         [string]$msi_path
  47.     )
  48.  
  49.     $installer = New-Object -ComObject WindowsInstaller.Installer
  50.     $msidb= $installer.GetType().InvokeMember('OpenDatabase', 'InvokeMethod', $null, $installer, @($msi_path, 0))
  51.  
  52.     $product_version = get_msi_db_property -msidb $msidb -wanted_property "ProductVersion" | Out-String
  53.     $product_name    = get_msi_db_property -msidb $msidb -wanted_property "ProductName"    | Out-String
  54.    
  55.     return @{
  56.                 name=$product_name.Trim()
  57.              version=$product_version.Trim()
  58.              }
  59. }
  60.  
  61. function get_installed_checkmk_agent_info {
  62.     $c = get_installed_software_info -pattern "Check MK Agent.*"
  63.     return $c
  64. }
  65.  
  66. function uninstall_checkmk_agent {
  67.     $application = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -match "Check MK Agent.*"}
  68.     if ($application) {
  69.         $application.Uninstall()
  70.     }
  71. }
  72.  
  73. function install_msi_package {
  74.     param(
  75.         [string]$msi_path
  76.     )
  77.     msiexec /I $msi_path /qn
  78.  
  79. }
  80.  
  81. function mylog {
  82.     $logline = ( $(Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " " + $args[0] )
  83.     Write-Host $logline
  84.     Add-Content -Path $global:log_path -Value "$logline"
  85. }
  86.  
  87. function main {
  88.     param (
  89.         [string]$msi_path,
  90.         [string]$msi_url
  91.     )
  92.  
  93.     mylog "Check-MK Installer/Updater starting"
  94.     mylog "deleting $($msi_path)"
  95.     # delete and redownload msi
  96.     if (Remove-Item -Path $msi_path -ErrorAction SilentlyContinue) {
  97.         mylog "Can not delete file $($msi_path): Aborting!"
  98.         exit 1
  99.     }
  100.  
  101.     mylog "downloading $($msi_url) to $($msi_path)"
  102.     $global:progressPreference = 'silentlyContinue'
  103.     for($i=1;$i -le 10;$i++) {
  104.         mylog "trying download #$($i)"
  105.         try {
  106.             Invoke-WebRequest -Uri $msi_url -OutFile $msi_path
  107.             $res = $?
  108.         } catch {
  109.             mylog "Error while downloading: $_"
  110.             $res = $false
  111.         }
  112.         # mylog ('$?: ' + ">" + $res + "<")
  113.         if($res -eq $True) { break }
  114.         Start-Sleep -Seconds 10
  115.     }
  116.     if ($res -eq $false) {
  117.         mylog "Giving up download. Aborting!"
  118.         exit 1
  119.     }
  120.    
  121.     Start-Sleep -Seconds 2
  122.  
  123.     mylog "examining msi file $($msi_file)"
  124.     $msi_data       = get_msi_product_info -msi_path $msi_path
  125.  
  126.     mylog "checking installed version of check_mk"
  127.     $installed_data = get_installed_checkmk_agent_info
  128.  
  129.     if ( $msi_data -and $msi_data.version ) {
  130.        if ( $installed_data ) {
  131.           if ( $installed_data.version ) {
  132.             $installed_data_cversion = [System.Version] ($installed_data.version -replace 'p', '.')
  133.             $msi_data_cversion       = [System.Version] ($msi_data.version -replace 'p', '.')
  134.             if ( $installed_data_cversion -lt $msi_data_cversion ) {
  135.                 mylog "Installed Version $($installed_data.version) older than MSI-Version $($msi_data.version): Uninstalling old and Installing new"
  136.                 uninstall_checkmk_agent
  137.                 install_msi_package -msi_path $msi_path
  138.             } else {
  139.                 mylog "Installed Version ($($installed_data.version)) is at least the same Version as MSI-Version ($($msi_data.version)): Doing nothing"
  140.             }
  141.           }
  142.        } else {
  143.             mylog "Check-MK not installed: Installing MSI-Package"
  144.             install_msi_package -msi_path $msi_path
  145.        }
  146.     }
  147.     mylog "Check-MK Installer/Updater finished"
  148. }
  149.  
  150. # todo: implement check if download is really necessary (avoid bandwidth wasting)
  151.  
  152. # program starts here
  153.  
  154. $global:reg_key_sw_uninstall = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
  155. $base_path         = "$($env:LOCALAPPDATA)\check_mk_updater"
  156. $msi_path          = "$base_path\check_mk_agent.msi"
  157. $global:log_path   = "$base_path\install.log"
  158. $msi_url           = "https://download.mydomain.de/repo/check_mk_agent.msi"
  159.  
  160. New-Item -ItemType Directory -Path $base_path -Force | Out-Null
  161. main -msi_path $msi_path -msi_url $msi_url

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN