cls $ErrorActionPreference= 'silentlycontinue' ################### Setup RustDesk Server settings ################### # Kill any running rustdesk processes before continuing Write-Host "Checking for running rustdesk processes..." Write-Output "________________________________________" Write-Output "" # Kill any rustdesk process (rustdesk.exe or any other rustdesk-related process) taskkill /F /IM "rustdesk.*" 2>$null Write-Output "________________________________________" Write-Output "" # Wait for 3 seconds to allow the processes to be fully terminated Start-Sleep -Seconds 3 # Check if any rustdesk process is still running after the kill command $rustdeskProcesses = Get-Process -Name "rustdesk" -ErrorAction SilentlyContinue if ($rustdeskProcesses) { Write-Host "Error: rustdesk process is still running. Exiting script." Write-Output "_______________________________________________________" Write-Output "" Start-Sleep -Seconds 10 exit } Write-Host "No rustdesk process is running. Continuing with the script..." -ForegroundColor Green Write-Output "___________________________________________________________" Write-Output "" # Wait for 3 seconds to allow the processes to be fully terminated Start-Sleep -Seconds 3 ################################### Please Do Not Edit Below This Line ######################################### # 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; } } # This function will return the latest version and download link as an object function getLatest() { $Page = Invoke-WebRequest -Uri 'https://github.com/rustdesk/rustdesk/releases/latest' -UseBasicParsing $HTML = New-Object -Com "HTMLFile" try { $HTML.IHTMLDocument2_write($Page.Content) } catch { $src = [System.Text.Encoding]::Unicode.GetBytes($Page.Content) $HTML.write($src) } # Current example link: https://github.com/rustdesk/rustdesk/releases/download/1.3.9/rustdesk-1.3.9-x86_64.exe $Downloadlink = ($HTML.Links | Where {$_.href -match '(.)+\/rustdesk\/rustdesk\/releases\/download\/\d{1}.\d{1,2}.\d{1,2}(.{0,3})\/rustdesk(.)+x86_64.exe'} | select -first 1).href # bugfix - sometimes you need to replace "about:" $Downloadlink = $Downloadlink.Replace('about:', 'https://github.com') $Version = "unknown" if ($Downloadlink -match './rustdesk/rustdesk/releases/download/(?.*)/rustdesk-(.)+x86_64.exe') { $Version = $matches['content'] } if ($Version -eq "unknown" -or $Downloadlink -eq "") { Write-Output "ERROR: Version or download link not found." Write-Output "__________________________________________" Write-Output "" Exit } # Create object to return $params += @{Version = $Version} $params += @{Downloadlink = $Downloadlink} $Result = New-Object PSObject -Property $params return($Result) } $RustDeskOnGitHub = getLatest $rdver = ((Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\RustDesk\").Version) if ($rdver -eq $RustDeskOnGitHub.Version) { Write-Output "RustDesk $rdver is the newest version." Write-Output "______________________________________" Write-Output "" # Wait for 3 seconds to allow the processes to be fully terminated Start-Sleep -Seconds 3 ################### Setup RustDesk Server settings ################### # Get the current user's profile path $currentUserProfile = [System.Environment]::GetFolderPath('UserProfile') # Define the config file path for the current user $configFilePath = "$currentUserProfile\AppData\Roaming\RustDesk\config\RustDesk2.toml" # Define the new settings to be added or replaced $newSettings = @" rendezvous_server = 'rustdesk.greenhome.stream' [options] api-server = 'https://rustdesk-api.greenhome.stream' relay-server = 'rustdesk.greenhome.stream' show-monitors-toolbar = 'Y' custom-rendezvous-server = 'rustdesk.greenhome.stream' enable-remote-printer = 'N' enable-audio = 'N' disable-audio = 'Y' av1-test = 'Y' enable-camera = 'N' key = '0xA3V7UvjQvq0q21ywtKVX+HwrlYizhwXsl5HXkk8Ag=' "@ # Read the current content of the config file $currentConfig = Get-Content -Path $configFilePath # Split the new settings into an array of lines $newSettingsLines = $newSettings -split "`r`n" # Create a hash table for the new settings for easy lookup $newSettingsHashtable = @{} foreach ($line in $newSettingsLines) { $lineTrimmed = $line.Trim() if ($lineTrimmed -ne "") { $key = $lineTrimmed.Split('=')[0].Trim() # Extract the option key $newSettingsHashtable[$key] = $lineTrimmed # Store the whole line (option=value) } } # Initialize an array to store the updated config $updatedConfig = @() # Process the current config and replace lines where necessary foreach ($line in $currentConfig) { $lineTrimmed = $line.Trim() # Extract the option key from the line (before the '=' symbol) $settingKey = $lineTrimmed.Split('=')[0].Trim() if ($newSettingsHashtable.ContainsKey($settingKey)) { # If the setting is in the new settings, replace it with the new value $updatedConfig += $newSettingsHashtable[$settingKey] $newSettingsHashtable.Remove($settingKey) # Remove it from the hashtable to track if it has been replaced } else { # Otherwise, keep the current line $updatedConfig += $lineTrimmed } } # Add any new settings that were not replaced (i.e., the remaining ones in the hashtable) foreach ($remainingLine in $newSettingsHashtable.Values) { $updatedConfig += $remainingLine } # Write the updated configuration back to the file Set-Content -Path $configFilePath -Value $updatedConfig # Notify the user Write-Host "Config file updated successfully!" -ForegroundColor Green Write-Output "________________________________________" Write-Output "" # Wait for 3 seconds before starting RustDesk Start-Sleep -Seconds 3 # Notify the user Write-Host "Starting RustDesk App and Service..." -ForegroundColor Green Write-Output "________________________________________" Write-Output "" # Wait for 3 seconds before starting RustDesk Start-Sleep -Seconds 3 # Run the RustDesk service installation command Start-Process "C:\Program Files\RustDesk\rustdesk.exe" -ArgumentList "--install-service" # Notify the user Write-Host "RustDesk service started successfully." -ForegroundColor Green Write-Output "________________________________________" Write-Output "" cd $env:ProgramFiles\RustDesk\ .\rustdesk.exe --get-id | Write-Output -OutVariable rustdesk_id Write-Output "________________________________________" Write-Output "" # Show the value of the ID Variable Write-Host "RustDesk ID: $rustdesk_id" -ForegroundColor Green # Wait for 30 seconds before starting RustDesk Start-Sleep -Seconds 30 Exit } ############## Upgrade RustDesk on the latest version ############## if (!(Test-Path C:\Temp)) { New-Item -ItemType Directory -Force -Path C:\Temp | Out-Null } cls cd C:\Temp Invoke-WebRequest $RustDeskOnGitHub.Downloadlink -Outfile "rustdesk.exe" Start-Process .\rustdesk.exe --silent-install Start-Sleep -seconds 20 $ServiceName = 'Rustdesk' $arrService = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue if ($arrService -eq $null) { Write-Output "Installing service" cd $env:ProgramFiles\RustDesk Start-Process .\rustdesk.exe --install-service Start-Sleep -seconds 20 $arrService = Get-Service -Name $ServiceName } while ($arrService.Status -ne 'Running') { Start-Service $ServiceName Start-Sleep -seconds 5 $arrService.Refresh() } ################### Setup RustDesk Server settings ################### # Kill any running rustdesk processes before continuing Write-Host "Checking for running rustdesk processes..." Write-Output "________________________________________" Write-Output "" # Kill any rustdesk process (rustdesk.exe or any other rustdesk-related process) taskkill /F /IM "rustdesk.*" 2>$null Write-Output "________________________________________" Write-Output "" # Wait for 3 seconds to allow the processes to be fully terminated Start-Sleep -Seconds 3 # Check if any rustdesk process is still running after the kill command $rustdeskProcesses = Get-Process -Name "rustdesk" -ErrorAction SilentlyContinue if ($rustdeskProcesses) { Write-Host "Error: rustdesk process is still running. Exiting script." Write-Output "_______________________________________________________" Write-Output "" Start-Sleep -Seconds 10 exit } Write-Host "No rustdesk process is running. Continuing with the script..." -ForegroundColor Green Write-Output "___________________________________________________________" Write-Output "" # Wait for 3 seconds to allow the processes to be fully terminated Start-Sleep -Seconds 3 # Get the current user's profile path $currentUserProfile = [System.Environment]::GetFolderPath('UserProfile') # Define the config file path for the current user $configFilePath = "$currentUserProfile\AppData\Roaming\RustDesk\config\RustDesk2.toml" # Define the new settings to be added or replaced $newSettings = @" rendezvous_server = 'rustdesk.greenhome.stream' [options] api-server = 'https://rustdesk-api.greenhome.stream' relay-server = 'rustdesk.greenhome.stream' show-monitors-toolbar = 'Y' custom-rendezvous-server = 'rustdesk.greenhome.stream' enable-remote-printer = 'N' enable-audio = 'N' disable-audio = 'Y' av1-test = 'Y' enable-camera = 'N' key = '0xA3V7UvjQvq0q21ywtKVX+HwrlYizhwXsl5HXkk8Ag=' "@ # Read the current content of the config file $currentConfig = Get-Content -Path $configFilePath # Split the new settings into an array of lines $newSettingsLines = $newSettings -split "`r`n" # Create a hash table for the new settings for easy lookup $newSettingsHashtable = @{} foreach ($line in $newSettingsLines) { $lineTrimmed = $line.Trim() if ($lineTrimmed -ne "") { $key = $lineTrimmed.Split('=')[0].Trim() # Extract the option key $newSettingsHashtable[$key] = $lineTrimmed # Store the whole line (option=value) } } # Initialize an array to store the updated config $updatedConfig = @() # Process the current config and replace lines where necessary foreach ($line in $currentConfig) { $lineTrimmed = $line.Trim() # Extract the option key from the line (before the '=' symbol) $settingKey = $lineTrimmed.Split('=')[0].Trim() if ($newSettingsHashtable.ContainsKey($settingKey)) { # If the setting is in the new settings, replace it with the new value $updatedConfig += $newSettingsHashtable[$settingKey] $newSettingsHashtable.Remove($settingKey) # Remove it from the hashtable to track if it has been replaced } else { # Otherwise, keep the current line $updatedConfig += $lineTrimmed } } # Add any new settings that were not replaced (i.e., the remaining ones in the hashtable) foreach ($remainingLine in $newSettingsHashtable.Values) { $updatedConfig += $remainingLine } # Write the updated configuration back to the file Set-Content -Path $configFilePath -Value $updatedConfig Write-Host "Config file updated successfully!" -ForegroundColor Green Write-Output "________________________________________" # Wait for 3 seconds before starting RustDesk Start-Sleep -Seconds 3 # Notify the user Write-Host "Starting RustDesk App and Service..." -ForegroundColor Green Write-Output "________________________________________" # Wait for 3 seconds before starting RustDesk Start-Sleep -Seconds 3 # Run the RustDesk service installation command Start-Process "C:\Program Files\RustDesk\rustdesk.exe" -ArgumentList "--install-service" Write-Host "RustDesk service started successfully." -ForegroundColor Green Write-Output "________________________________________" # Wait for 3 seconds before starting RustDesk Start-Sleep -Seconds 3 cd $env:ProgramFiles\RustDesk\ .\rustdesk.exe --get-id | Write-Output -OutVariable rustdesk_id Write-Output "________________________________________" # Show the value of the ID Variable Write-Host "RustDesk ID: $rustdesk_id" -ForegroundColor Green