Last active
February 2, 2024 07:11
-
-
Save lalibi/c4707d4f62f174d24df4cf12348aaa65 to your computer and use it in GitHub Desktop.
Install-RustDesk.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ErrorActionPreference= 'SilentlyContinue' | |
# Ask for the server IP | |
$server_ip = Read-Host -Prompt 'Enter the server IP (<Enter> for none:)' | |
# Ask for password | |
$rustdesk_pw = Read-Host -Prompt 'Enter the password (<Enter> for random:)' | |
# Generate a random password if none is provided | |
if ($rustdesk_pw -eq '') | |
{ | |
$rustdesk_pw = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count 16 | % { [char]$_ }) | |
} | |
# Run as administrator and stays in the current directory | |
if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) | |
{ | |
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) | |
{ | |
Start-Process PowerShell -Verb RunAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`""; | |
Exit; | |
} | |
} | |
# Get the latest version of RustDesk | |
Write-Progress -Activity "Installing RustDesk" -Status "Getting latest version" -PercentComplete 0 | |
$url = 'https://github.com/rustdesk/rustdesk/releases' | |
$response = Invoke-WebRequest -Uri $url | |
$installed_version = ((Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\RustDesk\").Version) | |
$latest_version = ($response.Links | Where-Object {$_.href -like "/rustdesk/rustdesk/releases/tag/*"} | Select-Object -First 1).href.Split('/')[-1] | |
$latest_version_download = ($response.Links | Where-Object {$_.href -like "https://github.com/rustdesk/rustdesk/releases/download/*"} | Select-Object -First 1).href | |
# Check if the latest version is installed | |
if ($installed_version -eq $latest_version) | |
{ | |
Write-Output "RustDesk $installed_version is the newest version" | |
Exit | |
} | |
Push-Location $env:TEMP | |
# Download and install the latest version | |
Write-Progress -Activity "Installing RustDesk" -Status "Downloading latest version" -PercentComplete 25 | |
Invoke-WebRequest $latest_version_download -Outfile "rustdesk.exe" | |
Start-Process .\rustdesk.exe --silent-install | |
# Wait for the service to start | |
Write-Progress -Activity "Installing RustDesk" -Status "Waiting for the service to start" -PercentComplete 50 | |
do { | |
$service_name = 'Rustdesk' | |
$service = Get-Service -Name $service_name | |
Start-Sleep -seconds 3 | |
} while ($service -eq $null) | |
Pop-Location | |
# Preconfigure the server | |
if ($server_ip -ne '') | |
{ | |
# Stop the service | |
Stop-Service -Name $service_name | |
Write-Progress -Activity "Installing RustDesk" -Status "Preconfigure server" -PercentComplete 75 | |
$config_file_name = 'RustDesk2.toml' | |
$config_file_base = Join-Path $env:APPDATA 'RustDesk\config' | |
$service_config_file_base = Join-Path $env:windir 'ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config' | |
if (-Not (Test-Path $config_file_base)) | |
{ | |
New-Item $config_file_base -ItemType Directory | Out-Null | |
} | |
$config_file = Join-Path $config_file_base $config_file_name | |
New-Item $config_file -Force | Out-Null | |
Set-Content $config_file "rendezvous_server = '$server_ip' `nnat_type = 1`nserial = 0`n`n[options]`ncustom-rendezvous-server = '$server_ip'`nkey = ''`nrelay-server = ''`napi-server = ''" | |
$service_config_file = Join-Path $service_config_file_base $config_file_name | |
New-Item $service_config_file -Force | Out-Null | |
Set-Content $service_config_file "rendezvous_server = '$server_ip' `nnat_type = 1`nserial = 0`n`n[options]`ncustom-rendezvous-server = '$server_ip'`nkey = ''`nrelay-server = ''`napi-server = ''" | |
# Start the service | |
Start-Service -Name $service_name | |
} | |
# Wait for the installation to finish | |
while (-Not (Test-Path "$env:ProgramFiles\RustDesk")) { | |
Start-Sleep -seconds 3 | |
} | |
Write-Progress -Activity "Installing RustDesk" -Status "Done!" -PercentComplete 100 | |
Push-Location "$env:ProgramFiles\RustDesk" | |
.\rustdesk.exe --get-id | Write-Output -OutVariable rustdesk_id | Out-Null | |
.\rustdesk.exe --password $rustdesk_pw | |
Write-Host ('-' * 50) | |
# Show the value of the ID Variable | |
Write-Host "RustDesk ID: $rustdesk_id" -ForegroundColor Green | |
# Show the value of the Password Variable | |
Write-Host "Password: $rustdesk_pw" -ForegroundColor Green | |
Pop-Location |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment