# # Install/Upgrade Check-MK-Agent from URL # function get_installed_software_info { param ( [string]$pattern ) $data = Get-ItemProperty $global:reg_key_sw_uninstall | Select-Object DisplayName, DisplayVersion | Where-Object{$_.DisplayName -match $pattern} if ($data) { return @{ name = $data.DisplayName.Trim() version = $data.DisplayVersion.Trim() } } } function get_msi_db_property { param ( $msidb, # opened msi database object System.__ComObject [string]$wanted_property # wanted property of the MSI file ) $view=$null $query = "SELECT Value FROM Property WHERE Property = '$($wanted_property)'" $view = $msidb.GetType().InvokeMember('OpenView', 'InvokeMethod', $null, $msidb, ($query)) $view.GetType().InvokeMember('Execute', 'InvokeMethod', $null, $view, $null) $record = $view. GetType(). InvokeMember('Fetch', 'InvokeMethod', $null, $view, $null) $value = $record. GetType(). InvokeMember('StringData', 'GetProperty', $null, $record, 1) return $value } function get_msi_product_info { param ( [string]$msi_path ) $installer = New-Object -ComObject WindowsInstaller.Installer $msidb= $installer.GetType().InvokeMember('OpenDatabase', 'InvokeMethod', $null, $installer, @($msi_path, 0)) $product_version = get_msi_db_property -msidb $msidb -wanted_property "ProductVersion" | Out-String $product_name = get_msi_db_property -msidb $msidb -wanted_property "ProductName" | Out-String return @{ name=$product_name.Trim() version=$product_version.Trim() } } function get_installed_checkmk_agent_info { $c = get_installed_software_info -pattern "Check MK Agent.*" return $c } function uninstall_checkmk_agent { $application = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -match "Check MK Agent.*"} if ($application) { $application.Uninstall() } } function install_msi_package { param( [string]$msi_path ) msiexec /I $msi_path /qn } function mylog { $logline = ( $(Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " " + $args[0] ) Write-Host $logline Add-Content -Path $global:log_path -Value "$logline" } function main { param ( [string]$msi_path, [string]$msi_url ) mylog "Check-MK Installer/Updater starting" mylog "deleting $($msi_path)" # delete and redownload msi if (Remove-Item -Path $msi_path -ErrorAction SilentlyContinue) { mylog "Can not delete file $($msi_path): Aborting!" exit 1 } mylog "downloading $($msi_url) to $($msi_path)" $global:progressPreference = 'silentlyContinue' for($i=1;$i -le 10;$i++) { mylog "trying download #$($i)" try { Invoke-WebRequest -Uri $msi_url -OutFile $msi_path $res = $? } catch { mylog "Error while downloading: $_" $res = $false } # mylog ('$?: ' + ">" + $res + "<") if($res -eq $True) { break } Start-Sleep -Seconds 10 } if ($res -eq $false) { mylog "Giving up download. Aborting!" exit 1 } Start-Sleep -Seconds 2 mylog "examining msi file $($msi_file)" $msi_data = get_msi_product_info -msi_path $msi_path mylog "checking installed version of check_mk" $installed_data = get_installed_checkmk_agent_info if ( $msi_data -and $msi_data.version ) { if ( $installed_data ) { if ( $installed_data.version ) { $installed_data_cversion = [System.Version] ($installed_data.version -replace 'p', '.') $msi_data_cversion = [System.Version] ($msi_data.version -replace 'p', '.') if ( $installed_data_cversion -lt $msi_data_cversion ) { mylog "Installed Version $($installed_data.version) older than MSI-Version $($msi_data.version): Uninstalling old and Installing new" uninstall_checkmk_agent install_msi_package -msi_path $msi_path } else { mylog "Installed Version ($($installed_data.version)) is at least the same Version as MSI-Version ($($msi_data.version)): Doing nothing" } } } else { mylog "Check-MK not installed: Installing MSI-Package" install_msi_package -msi_path $msi_path } } mylog "Check-MK Installer/Updater finished" } # todo: implement check if download is really necessary (avoid bandwidth wasting) # program starts here $global:reg_key_sw_uninstall = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" $base_path = "$($env:LOCALAPPDATA)\check_mk_updater" $msi_path = "$base_path\check_mk_agent.msi" $global:log_path = "$base_path\install.log" $msi_url = "https://download.mydomain.de/repo/check_mk_agent.msi" New-Item -ItemType Directory -Path $base_path -Force | Out-Null main -msi_path $msi_path -msi_url $msi_url