# Windows RustDesk enroll + heartbeat (token-less) # Run elevated PowerShell $ErrorActionPreference = 'Stop' try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch {} function Get-LocalIPv4 { try { $nic = Get-NetIPConfiguration -ErrorAction SilentlyContinue | Where-Object { $_.IPv4DefaultGateway -and $_.IPv4Address } | Select-Object -First 1 if ($nic -and $nic.IPv4Address) { return $nic.IPv4Address.IPAddress } $ip = Get-NetIPAddress -AddressFamily IPv4 -InterfaceOperationalStatus Up -ErrorAction SilentlyContinue | Where-Object { $_.IPAddress -ne '127.0.0.1' -and $_.IPAddress -notlike '169.254.*' } | Select-Object -First 1 return $ip.IPAddress } catch { return $null } } function Get-RustDeskId { $paths = @( 'C:\Program Files\RustDesk\rustdesk.exe', 'C:\Program Files (x86)\RustDesk\rustdesk.exe' ) foreach ($p in $paths) { if (Test-Path $p) { try { $psi = New-Object System.Diagnostics.ProcessStartInfo $psi.FileName = $p $psi.Arguments = '--get-id' $psi.RedirectStandardOutput = $true $psi.UseShellExecute = $false $psi.CreateNoWindow = $true $proc = [System.Diagnostics.Process]::Start($psi) $ok = $proc.WaitForExit(5000) if (-not $ok) { try { $proc.Kill() } catch {} } $out = $proc.StandardOutput.ReadToEnd().Trim() if ($out) { return $out } } catch {} } } return $null } $body = @{ hostname = $env:COMPUTERNAME user = $env:USERNAME localIp = Get-LocalIPv4 id = Get-RustDeskId } if (-not $body.id) { Write-Host "WARNING: RustDesk ID not found; enroll will still create/update host row." } $enrollUri = 'https://hbbs.xstarwake.com/enroll' $pingUri = 'https://hbbs.xstarwake.com/ping' # Initial enroll (id may be null) try { Invoke-RestMethod -Method Post -Uri $enrollUri -Body ($body | ConvertTo-Json) -ContentType 'application/json' -TimeoutSec 15 | Out-Null Write-Host "Enroll sent to $enrollUri" } catch { Write-Warning "Enroll failed: $($_.Exception.Message)" } # Write heartbeat script $baseDir = 'C:\ProgramData\RustDesk' $hbPath = Join-Path $baseDir 'ping.ps1' New-Item -ItemType Directory -Force -Path $baseDir | Out-Null $hb = @' $ErrorActionPreference = 'SilentlyContinue' try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch {} function Get-LocalIPv4 { try { $nic = Get-NetIPConfiguration -ErrorAction SilentlyContinue | Where-Object { $_.IPv4DefaultGateway -and $_.IPv4Address } | Select-Object -First 1 if ($nic -and $nic.IPv4Address) { return $nic.IPv4Address.IPAddress } $ip = Get-NetIPAddress -AddressFamily IPv4 -InterfaceOperationalStatus Up -ErrorAction SilentlyContinue | Where-Object { $_.IPAddress -ne '127.0.0.1' -and $_.IPAddress -notlike '169.254.*' } | Select-Object -First 1 return $ip.IPAddress } catch { return $null } } $payload = @{ hostname = $env:COMPUTERNAME user = $env:USERNAME localIp = Get-LocalIPv4 } try { Invoke-RestMethod -Method Post -Uri 'https://hbbs.xstarwake.com/ping' -Body ($payload | ConvertTo-Json) -ContentType 'application/json' -TimeoutSec 10 | Out-Null } catch {} '@ Set-Content -Path $hbPath -Value $hb -Encoding UTF8 # Register scheduled task (SYSTEM, highest), repeat every 10 minute(s) $psExe = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" $cmd = '"' + $psExe + '" -NoProfile -ExecutionPolicy Bypass -File "' + $hbPath + '"' $task = 'RustDesk-Ping' schtasks /Delete /TN "$task" /F *> $null $start = (Get-Date).AddMinutes(2).ToString('HH:mm') schtasks /Create /TN "$task" /SC MINUTE /MO 10 /ST $start /RU SYSTEM /RL HIGHEST /TR $cmd /F schtasks /Run /TN "$task" *> $null Write-Host "Heartbeat task '$task' created (every 10 min)."