Skip to content

Instantly share code, notes, and snippets.

@adamz01h
Last active December 20, 2023 16:33
Show Gist options
  • Select an option

  • Save adamz01h/f1f11b2ab8127b615af6791d8acb15bb to your computer and use it in GitHub Desktop.

Select an option

Save adamz01h/f1f11b2ab8127b615af6791d8acb15bb to your computer and use it in GitHub Desktop.
Rustdesk Powershell Installer that works.
$key = ''
$rendezvous_server = ''
$relay_server = ''
$api_server = ''
$nat_type = 1
$serial = 0
$setupUnattendedAccess = 1
$process = "rustdesk"
$ServiceName = 'Rustdesk'
function _service{
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$service,
[ValidateSet("start", "stop")]
$options
)
"service: $service, options: $options"
$check_count = 5
$checkInterval = 5
if($options -eq "start"){
while ($true) {
# Try to get the service
$service_out = Get-Service -Name $service -ErrorAction SilentlyContinue
# Check if the service is found and running
if ($service_out -and $service_out.Status -eq 'Running') {
Write-Host "Service '$service' is running."
break
}
elseif ($service_out) {
Write-Host "Service '$service' is installed but not running yet."
start-service $service
}
else {
Write-Host "Waiting for service '$service' to be installed..."
$check_count = $check_count - 1
if($check_count -eq 0){
Write-Host "Service '$service' is not installed."
#isntall service
Set-Location $env:ProgramFiles\RustDesk\
Start-Process .\rustdesk.exe --install-service
start-sleep 5
$check_count = 5
}
}
# Wait for the specified interval before checking again
Start-Sleep -Seconds $checkInterval
}
}
if ($options -eq "stop") {
while ($true) {
# Try to get the service
$service_out = Get-Service -Name $service -ErrorAction SilentlyContinue
# Check if the service is found and running
if ($service_out -and $service_out.Status -eq 'Running') {
Write-Host "Service '$service' is running."
Stop-Service $service
}
elseif ($service_out) {
Write-Host "Service '$service' is installed but not running."
break
}
else {
Write-Host "Waiting for service '$service' to be installed..."
}
# Wait for the specified interval before checking again
Start-Sleep -Seconds $checkInterval
}
}
}
function _process_kill{
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$name,
$options
)
"name: $name, options: $options"
# Interval in seconds for checking the process
$checkInterval = 5
# Start the loop
while ($true) {
# Try to get the process
$process = Get-Process -Name $name -ErrorAction SilentlyContinue
# Check if the process is running
if ($process) {
Write-Host "Process '$name' is running."
Stop-Process -Name $name -Force
break
}
else {
Write-Host "Process '$name' is not running."
break
}
# Wait for the specified interval before checking again
Start-Sleep -Seconds $checkInterval
}
}
#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;
}
}
If (!(Test-Path $env:Temp)) {
New-Item -ItemType Directory -Force -Path $env:Temp > null
}
write-host "Checking latest version of Rustdesk..."
$apiResponse = Invoke-RestMethod -Uri "https://api.github.com/repos/rustdesk/rustdesk/releases/latest"
# Extract the tag name
$version = $apiResponse.tag_name
write-host "Latest version of Rustdesk is $version"
If (!(Test-Path "$env:ProgramFiles\Rustdesk\RustDesk.exe")) {
Write-Host "Rustdesk Not Found..."
Set-Location $env:Temp
write-host "Downloading Rustdesk..."
Invoke-WebRequest "https://github.com/rustdesk/rustdesk/releases/download/$version/rustdesk-$version-x86_64.exe" -Outfile "rustdesk.exe"
write-host "Installing Rustdesk..."
Start-Process .\rustdesk.exe --silent-install
Start-Sleep 10
_process_kill -name $process
Write-Output "Installing service..."
Set-Location $env:ProgramFiles\RustDesk
Start-Process .\rustdesk.exe --install-service
Start-Sleep 10
Write-Output "Service installed..."
_process_kill -name $process
Write-Host "Starting Rustdesk Service...."
_service -service $ServiceName -options "start"
}else{
write-host "Rustdesk is already installed!"
$rustdesk_version = ((Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\RustDesk\").Version)
write-host "Rustdesk version is $rustdesk_version"
if ($rustdesk_version -eq $version) {
Write-Output "RustDesk $rustdesk_version is the newest version"
}else{
#install new version
write-host "Rustdesk is not the newest version!"
write-host "Downloading Rustdesk..."
Invoke-WebRequest "https://github.com/rustdesk/rustdesk/releases/download/$version/rustdesk-$version-x86_64.exe" -Outfile "rustdesk.exe"
write-host "Installing Rustdesk..."
Start-Process .\rustdesk.exe --silent-install
Start-Sleep 10
}
}
## if(!$setupUnattendedAccess -eq 1 ){
## $setupUnattendedAccess = Read-Host -Prompt "Do you want to set up unattended access? (Y/N)"
## }
if ($setupUnattendedAccess -eq 1) {
# Add code here to set up unattended access
Write-Host "Unattended access will be set up."
Write-Host "Starting Rustdesk Service...."
_service -service $ServiceName -options "start"
Write-Host "Starting Rustdesk Process..."
Set-Location $env:ProgramFiles\RustDesk\
Start-Process .\rustdesk.exe
$username = ((Get-WMIObject -ClassName Win32_ComputerSystem).Username).Split('\')[1]
Write-Host "Setting up config files..."
$localservice_config = "C:\Windows\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config\RustDesk2.toml"
$rustdesk_adddata_config = "$env:APPDATA\RustDesk\config\RustDesk2.toml"
#prob need to test path here
$user_appdata_config = "C:\Users\$username\AppData\Roaming\RustDesk\config\RustDesk2.toml"
#stop services to write config files
Write-Host "Stopping Rustdesk Services to write config files..."
_service -service $ServiceName -options "stop"
_process_kill -name $process
$config = "rendezvous_server = '$rendezvous_server' `nnat_type = $nat_type`nserial = $serial`n`n[options]`ncustom-rendezvous-server = '$rendezvous_server'`nkey = '$key'`nrelay-server = '$relay_server'`napi-server = '$api_server'"
Remove-Item $rustdesk_adddata_config
Start-Sleep 1
New-Item $rustdesk_adddata_config | Out-Null
Start-Sleep 1
Set-Content $rustdesk_adddata_config $config
Write-Host "Admin User config file created..."
Remove-Item $user_appdata_config
Start-Sleep 1
New-Item $user_appdata_config | Out-Null
Start-Sleep 1
Set-Content $user_appdata_config $config
Write-Host "User config file created..."
Remove-Item $localservice_config
Start-Sleep 1
New-Item $localservice_config | Out-Null
Start-Sleep 1
Set-Content $localservice_config $config
Write-Host "Local Service config file created..."
write-host "Generating random password..."
# Define ASCII ranges for uppercase letters, lowercase letters, numbers, and symbols
$upperCaseLetters = 65..90
$lowerCaseLetters = 97..122
$numbers = 48..57
$symbols = 33, 35, 36, 37, 42, 43, 64, 94
# Randomly select one character from each set and convert to char
$randomNumber = [char]($numbers | Get-Random -Count 1)
$randomUpperCase = [char]($upperCaseLetters | Get-Random -Count 1)
$randomLowerCase = [char]($lowerCaseLetters | Get-Random -Count 1)
$randomSymbol = [char]($symbols | Get-Random -Count 1)
# Generate the remaining 12 characters from the full set (including symbols) and convert to char array
$remainingChars = (($upperCaseLetters + $lowerCaseLetters + $numbers + $symbols) | Get-Random -Count 12 | ForEach-Object { [char]$_ })
# Combine all characters and shuffle
$allChars = ($randomNumber, $randomUpperCase, $randomLowerCase, $randomSymbol) + $remainingChars
$shuffledChars = $allChars | Get-Random -Count 16
# Join the shuffled characters into a single string
$rustdesk_pw = -join $shuffledChars
Write-Host "Starting Rustdesk Services to set the password...."
_service -service $ServiceName -options "start"
Write-Host "Starting Rustdesk Process to set the password..."
Set-Location $env:ProgramFiles\RustDesk\
Start-Process .\rustdesk.exe
.\rustdesk.exe --password "$rustdesk_pw"
Start-Sleep 5
.\rustdesk.exe --get-id | Write-Output -OutVariable rustdesk_id
# Load the System.Web assembly if it's not already loaded
if (-not ([System.Management.Automation.PSTypeName]'System.Web.HttpUtility').Type) {
[System.Reflection.Assembly]::LoadWithPartialName('System.Web')
}
# Use HttpUtility.UrlEncode method to URL encode the string
$urlencoded_pw = [System.Web.HttpUtility]::UrlEncode($rustdesk_pw)
Write-Output "RustDesk ID is: $rustdesk_id"
Write-Output "RustDesk Password is: $rustdesk_pw"
Write-Output "RustDesk Encoded : $urlencoded_pw"
#write output to file
$output = "RustDesk ID is: $rustdesk_id`nRustDesk Password is: $rustdesk_pw`nRustDesk Encoded : $urlencoded_pw"
$output | Out-File -FilePath "$env:Temp\RustDesk_access.txt"
#restart services
_service -service $ServiceName -options "stop"
_process_kill -name $process
_service -service $ServiceName -options "start"
#hit any key to continue
#Read-Host -Prompt "Press Enter to continue"
} else {
#exit were done here
Write-Host "Unattended access will not be set up."
}
Exit-Process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment