# PingMon agent 0.5.0 - one-line installer for Windows. # # & ([scriptblock]::Create((irm https://pingmon.tech/downloads/install.ps1))) -Token # # Run from an elevated PowerShell prompt. Downloads the agent, checks its SHA-256, installs it to # Program Files and registers the "PingMonAgent" service. Re-running it upgrades in place; leave # out -Token on a machine that is already paired. param( [string] $Token, [string] $Server = 'https://pingmon.tech', [string] $InstallDir = "$env:ProgramFiles\PingMon" ) $ErrorActionPreference = 'Stop' # Invoke-WebRequest draws a progress bar per chunk on Windows PowerShell 5, which makes a 1 MB # download take a minute. $ProgressPreference = 'SilentlyContinue' # Windows Server 2012/2016 default to TLS 1.0, which the server refuses. [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 $principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Run this from an elevated (Administrator) PowerShell prompt.' } $zipUrl = 'https://pingmon.tech/dist/pingmon-agent-0.5.0-windows-x86_64.zip' $expected = '7E9B9B11082E9EA52828054DF9ED78463091A803AF2A2DA1D32D10694B3D0B36' $work = Join-Path $env:TEMP ('pingmon-agent-' + [guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $work | Out-Null try { $zip = Join-Path $work 'agent.zip' Write-Host "Downloading $zipUrl ..." Invoke-WebRequest -UseBasicParsing -Uri $zipUrl -OutFile $zip $actual = (Get-FileHash -Path $zip -Algorithm SHA256).Hash if ($actual -ne $expected) { throw "Checksum mismatch: expected $expected, got $actual. Nothing was installed." } if (Get-Command Expand-Archive -ErrorAction SilentlyContinue) { Expand-Archive -Path $zip -DestinationPath $work -Force } else { # PowerShell 4 (Windows Server 2012 R2) has no Expand-Archive; .NET 4.5 does the same. Add-Type -AssemblyName System.IO.Compression.FileSystem [IO.Compression.ZipFile]::ExtractToDirectory($zip, $work) } $exe = Get-ChildItem -Path $work -Recurse -Filter 'pingmon-agent.exe' | Select-Object -First 1 if (-not $exe) { throw 'pingmon-agent.exe was not found in the download.' } # An upgrade has to stop the running service before its executable can be replaced. $existing = Get-Service -Name 'PingMonAgent' -ErrorAction SilentlyContinue if ($existing -and $existing.Status -ne 'Stopped') { Write-Host 'Stopping the running agent ...' Stop-Service -Name 'PingMonAgent' -Force $existing.WaitForStatus('Stopped', '00:00:30') } # The tray icon holds its executable open; close it so the file can be replaced. Get-Process -Name 'pingmon-tray' -ErrorAction SilentlyContinue | Stop-Process -Force New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null $target = Join-Path $InstallDir 'pingmon-agent.exe' $tray = Join-Path $exe.DirectoryName 'pingmon-tray.exe' $trayTarget = Join-Path $InstallDir 'pingmon-tray.exe' # When the agent updates itself ("Update now" in the tray runs pingmon-agent.exe update), the # old executable is the process running this script, and Windows will not overwrite a running # file. It will rename one, though: move the old files aside, copy the new ones in, and tidy # the leftovers on the next run, once nothing is holding them. foreach ($old in @($target, $trayTarget)) { if (Test-Path "$old.old") { Remove-Item "$old.old" -Force -ErrorAction SilentlyContinue } if (Test-Path $old) { Rename-Item -Path $old -NewName ((Split-Path $old -Leaf) + '.old') -Force -ErrorAction SilentlyContinue } } Copy-Item -Path $exe.FullName -Destination $target -Force if (Test-Path $tray) { Copy-Item -Path $tray -Destination $trayTarget -Force } Write-Host "Installed $target" $installArgs = @('install') if ($Token) { $installArgs += @('--server', $Server, '--token', $Token) } & $target @installArgs if ($LASTEXITCODE -ne 0) { throw 'The agent could not be paired or the service could not be installed (see above).' } # Show the tray icon now, as the signed-in user rather than elevated. if (Test-Path $trayTarget) { Start-Process -FilePath 'explorer.exe' -ArgumentList "`"$trayTarget`"" } Write-Host '' Write-Host "Done. Log: $env:ProgramData\PingMon\agent.log" Write-Host " & '$target' status # configuration and ICMP check" Write-Host " & '$target' dhcp # on a DHCP server: the leases it will report" } finally { Remove-Item -Path $work -Recurse -Force -ErrorAction SilentlyContinue }