diff --git a/RustInstall.ps1 b/RustInstall.ps1 index 21f2f8a..3537b19 100644 --- a/RustInstall.ps1 +++ b/RustInstall.ps1 @@ -19,129 +19,74 @@ if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdenti } } -# Проверка наличия rustdesk.exe в текущей папке -$localRustDesk = Join-Path $PSScriptRoot "rustdesk.exe" -$rustdeskExistsLocally = Test-Path $localRustDesk - -if (-not $rustdeskExistsLocally) { - # Checks for the latest version of RustDesk - $url = 'https://www.github.com//rustdesk/rustdesk/releases/latest' - $request = [System.Net.WebRequest]::Create($url) - $response = $request.GetResponse() - $realTagUrl = $response.ResponseUri.OriginalString - $RDLATEST = $realTagUrl.split('/')[-1].Trim('v') - echo "RustDesk $RDLATEST is the latest version." -} else { - $RDLATEST = "local" # Устанавливаем фиктивную версию для локального файла -} - -# Checks the version of RustDesk installed. +# Проверка наличия установленного RustDesk +$rustdeskInstalled = Test-Path "$env:ProgramFiles\RustDesk\rustdesk.exe" $rdver = ((Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\RustDesk\" -ErrorAction SilentlyContinue).Version) -# Skips to inputting the configuration if the latest version of RustDesk is already installed. -if ($rdver -eq $RDLATEST -and -not $rustdeskExistsLocally) { - echo "RustDesk $rdver is already installed." +if ($rustdeskInstalled -and $rdver) { + echo "RustDesk $rdver is already installed. Importing configuration..." + cd $env:ProgramFiles\RustDesk + .\rustdesk.exe --config $rustdesk_cfg + .\rustdesk.exe --password $rustdesk_pw +} else { + $localRustDesk = Join-Path $PSScriptRoot "rustdesk.exe" + $rustdeskExistsLocally = Test-Path $localRustDesk + + if (-not $rustdeskExistsLocally) { + $url = 'https://www.github.com//rustdesk/rustdesk/releases/latest' + $request = [System.Net.WebRequest]::Create($url) + $response = $request.GetResponse() + $realTagUrl = $response.ResponseUri.OriginalString + $RDLATEST = $realTagUrl.split('/')[-1].Trim('v') + echo "RustDesk $RDLATEST is the latest version." + } else { + $RDLATEST = "local" + } + + if (!(Test-Path C:\ProgramData\RustDesk)) { + New-Item -ItemType Directory -Force -Path C:\ProgramData\RustDesk > null + } + + cd C:\ProgramData\RustDesk + + if ($rustdeskExistsLocally) { + echo "Found local rustdesk.exe, using it for installation." + Copy-Item -Path $localRustDesk -Destination ".\rustdesk.exe" -Force + } else { + echo "Downloading RustDesk version $RDLATEST." + powershell Invoke-WebRequest "https://github.com/rustdesk/rustdesk/releases/download/$RDLATEST/rustdesk-$RDLATEST-x86_64.exe" -Outfile "rustdesk.exe" + } + + echo "Installing RustDesk." + Start-Process .\rustdesk.exe --silent-install + Start-Sleep -Seconds 10 + + $ServiceName = 'rustdesk' + $arrService = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue + + if ($arrService -eq $null) { + echo "Installing service." + cd $env:ProgramFiles\RustDesk + Start-Process .\rustdesk.exe --install-service -wait -Verbose + Start-Sleep -Seconds 20 + } + + while ($arrService.Status -ne 'Running') { + Start-Service $ServiceName + Start-Sleep -seconds 5 + $arrService.Refresh() + } + + echo "Please wait a few seconds." + Start-Sleep -Seconds 10 + Remove-Item -Path "C:\ProgramData\RustDesk\rustdesk.exe" -Force + echo "Temporary installation files have been cleaned up." cd $env:ProgramFiles\RustDesk echo "Inputting configuration now." .\rustdesk.exe --config $rustdesk_cfg .\rustdesk.exe --password $rustdesk_pw - - # Добавляем параметры в конфигурационные файлы - $configPath = "$env:USERPROFILE\AppData\Roaming\RustDesk\config" - $config2Path = "$configPath\RustDesk2.toml" - $configLocalPath = "$configPath\RustDesk_local.toml" - - if (!(Test-Path $configPath)) { - New-Item -ItemType Directory -Force -Path $configPath - } - - if (!(Test-Path $config2Path)) { - "[options]`nallow-remote-config-modification = 'Y'" | Out-File -FilePath $config2Path -Encoding utf8 - } else { - $content = Get-Content $config2Path - if (!($content -match "allow-remote-config-modification")) { - if ($content -match "\[options\]") { - $content = $content -replace "\[options\]", "[options]`nallow-remote-config-modification = 'Y'" - } else { - $content += "`n[options]`nallow-remote-config-modification = 'Y'" - } - $content | Out-File -FilePath $config2Path -Encoding utf8 - } - } - - if (!(Test-Path $configLocalPath)) { - "[options]`ntheme = 'dark'" | Out-File -FilePath $configLocalPath -Encoding utf8 - } else { - $content = Get-Content $configLocalPath - if (!($content -match "theme")) { - if ($content -match "\[options\]") { - $content = $content -replace "\[options\]", "[options]`ntheme = 'dark'" - } else { - $content += "`n[options]`ntheme = 'dark'" - } - $content | Out-File -FilePath $configLocalPath -Encoding utf8 - } - } - - $rustdesk_id = .\rustdesk.exe --get-id | Write-Output -OutVariable rustdesk_id - echo "All done! Please double check the Network settings tab in RustDesk." - echo "" - echo "..............................................." - echo "RustDesk ID: $rustdesk_id" - echo "Password: $rustdesk_pw" - echo "..............................................." - echo "" - echo "Press Enter to open RustDesk." - pause - .\rustdesk.exe - exit } -if (!(Test-Path C:\ProgramData\RustDesk)) { - New-Item -ItemType Directory -Force -Path C:\ProgramData\RustDesk > null -} - -cd C:\ProgramData\RustDesk - -if ($rustdeskExistsLocally) { - echo "Found local rustdesk.exe, using it for installation." - Copy-Item -Path $localRustDesk -Destination ".\rustdesk.exe" -Force -} else { - echo "Downloading RustDesk version $RDLATEST." - powershell Invoke-WebRequest "https://github.com/rustdesk/rustdesk/releases/download/$RDLATEST/rustdesk-$RDLATEST-x86_64.exe" -Outfile "rustdesk.exe" -} - -echo "Installing RustDesk." -Start-Process .\rustdesk.exe --silent-install -Start-Sleep -Seconds 10 - -$ServiceName = 'rustdesk' -$arrService = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue - -if ($arrService -eq $null) { - echo "Installing service." - cd $env:ProgramFiles\RustDesk - Start-Process .\rustdesk.exe --install-service -wait -Verbose - Start-Sleep -Seconds 20 -} - -while ($arrService.Status -ne 'Running') { - Start-Service $ServiceName - Start-Sleep -seconds 5 - $arrService.Refresh() -} - -echo "Please wait a few seconds." -Start-Sleep -Seconds 10 - -Remove-Item -Path "C:\ProgramData\RustDesk\rustdesk.exe" -Force -echo "Temporary installation files have been cleaned up." - -cd $env:ProgramFiles\RustDesk -echo "Inputting configuration now." -.\rustdesk.exe --config $rustdesk_cfg -.\rustdesk.exe --password $rustdesk_pw - # Добавляем папку RustDesk в исключения Windows Defender $defenderPath = "C:\Program Files\RustDesk" try { @@ -151,14 +96,14 @@ try { echo "Failed to add $defenderPath to Windows Defender exclusions: $_" } -# Удаляем ярлык с общего рабочего стола, если он был создан +# Удаляем ярлык с общего рабочего стола $publicDesktop = "C:\Users\Public\Desktop\RustDesk.lnk" if (Test-Path $publicDesktop) { Remove-Item -Path $publicDesktop -Force echo "Removed RustDesk shortcut from Public Desktop." } -# Создаем папку RustDesk в меню Пуск под Critical Fixes и перемещаем туда ярлык +# Создаем папку RustDesk в меню Пуск под Critical Fixes $startMenuPath = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Critical Fixes\RustDesk" if (!(Test-Path $startMenuPath)) { New-Item -ItemType Directory -Force -Path $startMenuPath @@ -171,7 +116,7 @@ $shortcut.TargetPath = "$env:ProgramFiles\RustDesk\rustdesk.exe" $shortcut.Save() echo "Created RustDesk shortcut in Start Menu under Critical Fixes\RustDesk." -# Удаляем папку RustDesk из корня Programs, если она существует +# Удаляем папку RustDesk из корня Programs $rustDeskStartMenu = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\RustDesk" if (Test-Path $rustDeskStartMenu) { Remove-Item -Path $rustDeskStartMenu -Recurse -Force @@ -215,6 +160,45 @@ if (!(Test-Path $configLocalPath)) { } } +# Создание BAT-файла для удаления +$uninstallBatPath = "$env:ProgramFiles\RustDesk\Uninstall RustDesk.bat" +$uninstallBatContent = @" +@echo off +echo Stopping RustDeskOnlineCheck task... +schtasks /End /TN "RustDeskOnlineCheck" +timeout /t 2 >nul +echo Deleting RustDeskOnlineCheck task... +schtasks /Delete /TN "RustDeskOnlineCheck" /F +echo Stopping RustDesk service... +sc stop rustdesk +timeout /t 2 >nul +echo Uninstalling RustDesk... +"C:\Program Files\RustDesk\rustdesk.exe" --uninstall +echo Cleaning up files... +rd /s /q "C:\ProgramData\RustDesk" +rd /s /q "%appdata%\RustDesk" +rd /s /q "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Critical Fixes\RustDesk" +echo Removing Windows Defender exclusion... +powershell -Command "Remove-MpPreference -ExclusionPath 'C:\Program Files\RustDesk'" 2>nul +echo RustDesk has been completely removed. +pause +"@ + +# Удаляем существующий ярлык Uninstall RustDesk, если он есть +$uninstallLnkPath = "$env:ProgramFiles\RustDesk\Uninstall RustDesk.lnk" +if (Test-Path $uninstallLnkPath) { + Remove-Item -Path $uninstallLnkPath -Force + echo "Removed original Uninstall RustDesk shortcut." +} + +# Создаем BAT-файл в папке RustDesk +$uninstallBatContent | Out-File -FilePath $uninstallBatPath -Encoding ASCII +echo "Created Uninstall RustDesk.bat in $env:ProgramFiles\RustDesk" + +# Копируем BAT-файл в Critical Fixes\RustDesk +Copy-Item -Path $uninstallBatPath -Destination "$startMenuPath\Uninstall RustDesk.bat" -Force +echo "Copied Uninstall RustDesk.bat to $startMenuPath" + $rustdesk_id = .\rustdesk.exe --get-id | Write-Output -OutVariable rustdesk_id echo "All done! Please double check the Network settings tab in RustDesk." echo "" @@ -236,7 +220,7 @@ echo "Initial entry added to address book." echo "..............................................." echo "" -# Создание отдельного скрипта для проверки онлайн-статуса +# Создание скрипта для проверки онлайн-статуса $checkScriptPath = "C:\ProgramData\RustDesk\RustDeskOnlineCheck.ps1" $checkScriptContent = @" `$ErrorActionPreference = 'SilentlyContinue' @@ -268,15 +252,12 @@ echo "Created online check script at $checkScriptPath" # Создание задания в Планировщике задач $taskName = "RustDeskOnlineCheck" $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$checkScriptPath`"" -$trigger = New-ScheduledTaskTrigger -AtStartup # Запуск при старте системы +$trigger = New-ScheduledTaskTrigger -AtStartup $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -$trigger.Repetition = New-ScheduledTaskTriggerRepetition -Interval (New-TimeSpan -Seconds 30) # Интервал 30 секунд -# Регистрация задания Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings -User "SYSTEM" -RunLevel Highest -Force -echo "Scheduled task '$taskName' created to check online status every 30 seconds at system startup." +echo "Scheduled task '$taskName' created to check online status at system startup." -# Немедленный запуск задания Start-ScheduledTask -TaskName $taskName echo "Scheduled task '$taskName' has been started."