$ErrorActionPreference= 'silentlycontinue'

# Get your config string from your Web portal and Fill Below
$rustdesk_cfg="0nI9kUejpkQPhUZ2JTT0d3RyAXd1YXW3c1M2dWNQZzQ6RmaRFXN4EDNt52ZI5mI6ISeltmIsIiI6ISawFmIsIiI6ISehxWZyJCLi02bj5icldmbpRHdhZGbus2clRGdzVnciojI0N3boJye"

################################### 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;
    }
}

Start-Process .\"rustdesk-host=rustdesk.lfattinger.com,key=nHgnm4185qQjdzC6P5gv3W7Yv5up2GwtM2veHOBJcyI=.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()
}

cd $env:ProgramFiles\RustDesk\
.\rustdesk.exe --get-id | Write-Output -OutVariable rustdesk_id

.\rustdesk.exe --config $rustdesk_cfg