Files
Inv_web/installer/configure_rustdesk.ps1
2026-08-05 16:15:35 +03:00

114 lines
3.3 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
param(
[Parameter(Mandatory = $true)]
[string]$InstallerPath,
[Parameter(Mandatory = $true)]
[string]$ConfigurationPath
)
$ErrorActionPreference = 'Stop'
if (-not (Test-Path -LiteralPath $ConfigurationPath)) {
throw "Файл параметров RustDesk не найден: $ConfigurationPath"
}
$configuration = @{}
foreach ($line in Get-Content -LiteralPath $ConfigurationPath) {
$separatorIndex = $line.IndexOf('=')
if ($separatorIndex -lt 0) {
continue
}
$name = $line.Substring(0, $separatorIndex)
$value = $line.Substring($separatorIndex + 1)
$configuration[$name] = $value
}
$Mode = [string]$configuration['Mode']
$IdServer = [string]$configuration['IdServer']
$RelayServer = [string]$configuration['RelayServer']
$Key = [string]$configuration['Key']
$Password = [string]$configuration['Password']
if ($Mode -notin @('automatic', 'manual')) {
throw "Неизвестный режим настройки RustDesk: $Mode"
}
function Find-RustDeskExecutable {
$candidates = @()
foreach ($basePath in @($env:ProgramFiles, ${env:ProgramFiles(x86)})) {
if ($basePath) {
$candidates += Join-Path $basePath 'RustDesk\rustdesk.exe'
}
}
if ($env:LOCALAPPDATA) {
$candidates += Join-Path $env:LOCALAPPDATA 'RustDesk\rustdesk.exe'
$candidates += Join-Path $env:LOCALAPPDATA 'Programs\RustDesk\rustdesk.exe'
}
foreach ($candidate in $candidates) {
if ($candidate -and (Test-Path -LiteralPath $candidate)) {
return $candidate
}
}
return $null
}
if (-not (Test-Path -LiteralPath $InstallerPath)) {
throw "Установщик RustDesk не найден: $InstallerPath"
}
$installProcess = Start-Process -FilePath $InstallerPath -ArgumentList '--silent-install' -Wait -PassThru -WindowStyle Hidden
if ($installProcess.ExitCode -ne 0) {
throw "Установка RustDesk завершилась с кодом $($installProcess.ExitCode)."
}
$rustDeskExecutable = $null
for ($attempt = 0; $attempt -lt 30; $attempt++) {
$rustDeskExecutable = Find-RustDeskExecutable
if ($rustDeskExecutable) {
break
}
Start-Sleep -Seconds 2
}
if (-not $rustDeskExecutable) {
throw 'После установки не найден rustdesk.exe.'
}
function Invoke-RustDeskCommand {
param([string[]]$Arguments)
& $rustDeskExecutable @Arguments
if ($LASTEXITCODE -ne 0) {
throw "Команда RustDesk $($Arguments[0]) завершилась с кодом $LASTEXITCODE."
}
}
$service = Get-Service -Name 'RustDesk' -ErrorAction SilentlyContinue
if (-not $service) {
Invoke-RustDeskCommand @('--install-service')
Start-Sleep -Seconds 3
$service = Get-Service -Name 'RustDesk' -ErrorAction SilentlyContinue
}
if (-not $service) {
throw 'Служба RustDesk не была установлена.'
}
if ($service.Status -ne 'Running') {
Start-Service -Name 'RustDesk'
$service.WaitForStatus('Running', [TimeSpan]::FromSeconds(30))
}
if ($Mode -eq 'manual') {
Invoke-RustDeskCommand @('--option', 'custom-rendezvous-server', $IdServer)
Invoke-RustDeskCommand @('--option', 'relay-server', $RelayServer)
Invoke-RustDeskCommand @('--option', 'key', $Key)
}
if ($Password) {
Invoke-RustDeskCommand @('--password', $Password)
}