# manages.me — ManagesMe Host (Windows) #Requires -Version 5 $ErrorActionPreference = 'Stop' function Install-ManagesMeRustDesk { param([Parameter(Mandatory=$true)]$Cfg) # Invoke-RestMethod / ConvertFrom-Json return PSCustomObject, not hashtable $IdServer = if ($Cfg.id_server) { $Cfg.id_server } else { '50.240.66.57' } $Relay = if ($Cfg.relay_server) { $Cfg.relay_server } else { '50.240.66.57' } $Key = $Cfg.public_key $Unattended = $Cfg.unattended_password $Token = $Cfg.enroll_token $EnrollUrl = $Cfg.enroll_url $logDir = "$env:ProgramData\manages" New-Item -ItemType Directory -Force -Path $logDir | Out-Null $rdExe = "${env:ProgramFiles}\RustDesk\rustdesk.exe" if (Test-Path $rdExe) { Write-Host "RustDesk already installed - configuring..." -ForegroundColor Green } else { $ver = "1.3.6" $url = "https://manages.me/downloads/rustdesk-$ver-x86_64.exe" $installer = "$env:TEMP\rustdesk-setup.exe" Write-Host "Downloading RustDesk (~22 MB from manages.me)..." -ForegroundColor Cyan $dl = $false if (Get-Command curl.exe -ErrorAction SilentlyContinue) { & curl.exe -fL --retry 3 --connect-timeout 30 -o $installer $url if ($LASTEXITCODE -eq 0) { $dl = $true } } if (-not $dl) { Invoke-WebRequest -Uri $url -OutFile $installer -UseBasicParsing -TimeoutSec 600 } if (-not (Test-Path $installer) -or (Get-Item $installer).Length -lt 1MB) { throw "RustDesk download failed or file too small" } Write-Host "Installing RustDesk (often 2-5 min; dots = still working)..." -ForegroundColor Cyan $proc = Start-Process -FilePath $installer -ArgumentList "--silent-install" -PassThru -WindowStyle Hidden $deadline = (Get-Date).AddMinutes(8) while ((Get-Date) -lt $deadline) { if (Test-Path $rdExe) { break } if ($proc.HasExited) { Start-Sleep -Seconds 3 if (Test-Path $rdExe) { break } } Write-Host "." -NoNewline Start-Sleep -Seconds 4 } Write-Host "" if (-not (Test-Path $rdExe)) { Write-Host "Retrying install once..." -ForegroundColor Yellow & $installer --silent-install 2>$null Start-Sleep -Seconds 10 } } if (-not (Test-Path $rdExe)) { throw "RustDesk install timed out. If RustDesk is already on this PC, open it once from the Start menu, then run ManagesMe-Setup.cmd again." } $confDir = "$env:AppData\RustDesk\config" New-Item -ItemType Directory -Force -Path $confDir | Out-Null $toml = @" rendezvous_server = '$IdServer' nat_type = 1 serial = 0 [options] custom-rendezvous-server = '$IdServer' key = '$Key' custom-relay-server = '$Relay' "@ Set-Content -Path (Join-Path $confDir "RustDesk2.toml") -Value $toml -Encoding UTF8 Write-Host "Installing RustDesk service..." & $rdExe --install-service 2>$null Start-Sleep -Seconds 2 & $rdExe --password $Unattended 2>$null & $rdExe --option allow-auto-disconnect=N 2>$null & $rdExe --option enable-direct-xip=Y 2>$null Restart-Service RustDesk -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 Write-Host "Permanent password is set for manages.me Connect (ignore the 6-character one-time code in the RustDesk window)." -ForegroundColor Cyan $rdId = (& $rdExe --get-id 2>$null | Out-String).Trim() if (-not $rdId) { throw "Could not read RustDesk ID" } Write-Host "RustDesk ID: $rdId" -ForegroundColor Green $os = (Get-CimInstance Win32_OperatingSystem).Caption $body = @{ token = $Token; rustdesk_id = $rdId; os = "$env:COMPUTERNAME - $os" } | ConvertTo-Json Invoke-RestMethod -Uri $EnrollUrl -Method Post -Body $body -ContentType 'application/json' | Out-Null Write-Host "`n=== Done! Open manages.me and click Connect. ===`n" -ForegroundColor Green } $scriptPath = if ($PSCommandPath) { $PSCommandPath } else { $MyInvocation.MyCommand.Definition } $identity = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() if (-not $identity.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Host "Requesting administrator (approve UAC)..." -ForegroundColor Cyan Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"`$scriptPath`"" -Verb RunAs exit } [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Write-Host "`n=== ManagesMe Host ===`n" -ForegroundColor Cyan $email = Read-Host "manages.me email" $sec = Read-Host "Password" -AsSecureString $bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($sec) try { $pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr) } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) } $name = Read-Host "Name for this PC (e.g. Home Office)" $body = @{ email = $email; password = $pass; name = $name } | ConvertTo-Json try { $cfg = Invoke-RestMethod -Uri 'https://manages.me/api/agent_setup.php' -Method Post -Body $body -ContentType 'application/json' } catch { $detail = $_.Exception.Message if ($_.ErrorDetails -and $_.ErrorDetails.Message) { $detail = $_.ErrorDetails.Message } Write-Host "Login or setup failed: $detail" -ForegroundColor Red Read-Host "Press Enter to exit" exit 1 } if (-not $cfg.ok) { Write-Host "Setup failed: $($cfg.error)" -ForegroundColor Red; Read-Host "Press Enter"; exit 1 } Install-ManagesMeRustDesk -Cfg $cfg Read-Host "Press Enter to close"