# ShastaMSP - Tactical RMM agent installer for Windows # # Usage, in PowerShell: # irm get.shastamsp.net | iex # # Unattended (GPO, Intune, another RMM), already running as admin: # $env:TRMM_CODE = ''; irm get.shastamsp.net | iex # # This file is public on purpose and holds no secrets. The deployment code is # the secret: it comes from TRMM > Agents > Manage Deployments, is tied to one # client/site, expires, and can be deleted in TRMM at any time. & { $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' # Invoke-WebRequest is very slow with the progress bar on # ---- Settings --------------------------------------------------------- $ApiHost = 'api.shastamsp.net' $SelfUrl = 'https://get.shastamsp.net' $RequireSignature = $false # set to $true once TRMM code signing (sponsorship) is enabled $AddDefenderExclusions = $true # exclusions recommended by the TRMM docs # ----------------------------------------------------------------------- function Write-Step($msg) { Write-Host "==> $msg" -ForegroundColor Cyan } function Write-Fail($msg) { Write-Host "ERROR: $msg" -ForegroundColor Red } # PowerShell 5.1 does not always offer TLS 1.2 by default. [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 Write-Host '' Write-Host 'ShastaMSP - Remote Management Agent Installer' -ForegroundColor White Write-Host '' # ---- Admin check (re-launch elevated if needed) ----------------------- $principal = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Host 'Administrator rights are needed. Opening an elevated PowerShell window...' -ForegroundColor Yellow try { Start-Process -FilePath 'powershell.exe' -Verb RunAs -ArgumentList @( '-NoProfile', '-ExecutionPolicy', 'Bypass', '-NoExit', '-Command', "irm $SelfUrl | iex" ) } catch { Write-Fail 'Elevation was cancelled. Right-click PowerShell, choose "Run as administrator", and run the command again.' } return } # ---- Already installed? ----------------------------------------------- if (Get-Service -Name 'tacticalrmm' -ErrorAction SilentlyContinue) { Write-Host 'The agent is already installed on this computer.' -ForegroundColor Yellow if (-not $env:TRMM_CODE) { $answer = Read-Host 'Reinstall it anyway? (y/N)' if ($answer -notmatch '^(y|yes)$') { Write-Host 'Nothing changed.'; return } } else { Write-Host 'Unattended mode: skipping.'; return } } # ---- Deployment code -------------------------------------------------- $code = $env:TRMM_CODE if (-not $code) { $code = Read-Host 'Enter your deployment code (or paste the full deployment link)' } $uuidPattern = '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}' if ("$code" -notmatch $uuidPattern) { Write-Fail "That doesn't look like a deployment code. It should look like 1b4e28ba-2fa1-4d3b-a3f5-ef19b5a7633b." return } $uid = $Matches[0].ToLower() # ---- Download --------------------------------------------------------- $work = Join-Path $env:TEMP ('trmm-' + [guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $work | Out-Null $exe = Join-Path $work 'trmm-installer.exe' try { if ($AddDefenderExclusions -and (Get-Command Add-MpPreference -ErrorAction SilentlyContinue)) { Write-Step 'Adding Microsoft Defender exclusions for the agent' foreach ($p in @( 'C:\Program Files\TacticalAgent\*', 'C:\Program Files\Mesh Agent\*', 'C:\ProgramData\TacticalRMM\*', $work )) { try { Add-MpPreference -ExclusionPath $p } catch { Write-Host " (could not add $p - continuing)" -ForegroundColor DarkGray } } } Write-Step 'Downloading installer (this can take up to a minute)' try { Invoke-WebRequest -Uri "https://$ApiHost/clients/$uid/deploy/" -OutFile $exe -UseBasicParsing -TimeoutSec 180 } catch { $status = $null try { $status = [int]$_.Exception.Response.StatusCode } catch { } if ($status -eq 404) { Write-Fail 'That deployment code was not found. It may have expired or been deleted - ask for a new one.' } else { Write-Fail "Download failed: $($_.Exception.Message)" } return } # Make sure we got a Windows program, not an error page. $header = New-Object byte[] 2 $fs = [IO.File]::OpenRead($exe) try { $read = $fs.Read($header, 0, 2) } finally { $fs.Close() } if ($read -lt 2 -or $header[0] -ne 0x4D -or $header[1] -ne 0x5A) { $text = (Get-Content -Path $exe -Raw -ErrorAction SilentlyContinue) Write-Fail 'The server did not return an installer.' if ($text) { Write-Host ($text.Substring(0, [Math]::Min(300, $text.Length))) -ForegroundColor DarkGray } return } # ---- Signature ---------------------------------------------------- $sig = Get-AuthenticodeSignature -FilePath $exe if ($sig.Status -eq 'Valid') { Write-Step "Installer is signed by: $($sig.SignerCertificate.Subject)" } elseif ($RequireSignature) { Write-Fail "Installer signature is not valid ($($sig.Status)). Stopping." return } else { Write-Host ' Installer is not code signed (expected without TRMM code signing).' -ForegroundColor DarkGray } # ---- Install ------------------------------------------------------ Write-Step 'Installing agent' $proc = Start-Process -FilePath $exe -ArgumentList '--silent' -Wait -PassThru if ($proc.ExitCode -ne 0) { Write-Fail "Installer exited with code $($proc.ExitCode)." return } Write-Step 'Waiting for the agent service to start' $svc = $null for ($i = 0; $i -lt 30; $i++) { $svc = Get-Service -Name 'tacticalrmm' -ErrorAction SilentlyContinue if ($svc -and $svc.Status -eq 'Running') { break } Start-Sleep -Seconds 2 } if ($svc -and $svc.Status -eq 'Running') { Write-Host '' Write-Host 'Done. This computer is now managed by ShastaMSP.' -ForegroundColor Green } else { Write-Fail 'The installer finished, but the agent service is not running yet. Contact ShastaMSP support.' } } finally { if ($AddDefenderExclusions -and (Get-Command Remove-MpPreference -ErrorAction SilentlyContinue)) { try { Remove-MpPreference -ExclusionPath $work } catch { } } Remove-Item -Path $work -Recurse -Force -ErrorAction SilentlyContinue } }