114 lines
3.3 KiB
PowerShell
114 lines
3.3 KiB
PowerShell
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)
|
||
}
|