diff --git a/CLIENT_AGENT_V2_INSTALLER.md b/CLIENT_AGENT_V2_INSTALLER.md index 78b94fb..72882ab 100644 --- a/CLIENT_AGENT_V2_INSTALLER.md +++ b/CLIENT_AGENT_V2_INSTALLER.md @@ -80,3 +80,8 @@ Setup. Это исключает распаковку Tcl/Tk во временн ```text %ProgramData%\Inv_web Client\rustdesk-install.log ``` + +Перед обновлением сценарий автоматически закрывает уже открытое пользовательское +окно RustDesk. После тихой установки он ожидает завершения только процесса +установщика, поэтому запущенное окно RustDesk не задерживает завершение установки +клиента. diff --git a/installer/configure_rustdesk.ps1 b/installer/configure_rustdesk.ps1 index d1533ca..b7d3ee4 100644 --- a/installer/configure_rustdesk.ps1 +++ b/installer/configure_rustdesk.ps1 @@ -97,22 +97,68 @@ function Find-RustDeskExecutable { return $null } +function Stop-RustDeskDesktopProcesses { + $serviceProcessId = 0 + try { + $serviceInstance = Get-CimInstance ` + -ClassName Win32_Service ` + -Filter "Name = 'RustDesk'" ` + -ErrorAction SilentlyContinue + if ($serviceInstance) { + $serviceProcessId = [int]$serviceInstance.ProcessId + } + } + catch { + Write-InstallLog "Не удалось определить PID службы RustDesk: $($_.Exception.Message)" + } + + $desktopProcesses = @( + Get-Process -Name 'RustDesk' -ErrorAction SilentlyContinue | + Where-Object { ($serviceProcessId -le 0) -or ($_.Id -ne $serviceProcessId) } + ) + foreach ($process in $desktopProcesses) { + Write-InstallLog "Закрывается пользовательский процесс RustDesk, PID: $($process.Id)." + Stop-Process -Id $process.Id -Force -ErrorAction SilentlyContinue + } +} + if (-not (Test-Path -LiteralPath $InstallerPath)) { throw "Установщик RustDesk не найден: $InstallerPath" } +Stop-RustDeskDesktopProcesses + Write-InstallLog 'Запускается встроенный установщик RustDesk.' -$installProcess = Start-Process -FilePath $InstallerPath -ArgumentList '--silent-install' -Wait -PassThru -WindowStyle Hidden -$installExitCode = $installProcess.ExitCode -Write-InstallLog "Установщик RustDesk завершён с кодом: $installExitCode" +$installProcess = Start-Process -FilePath $InstallerPath -ArgumentList '--silent-install' -PassThru -WindowStyle Hidden + +# Start-Process -Wait waits for the complete process tree. RustDesk starts its +# desktop window as a child process after installation, so -Wait blocks until +# that window is closed manually. Process.WaitForExit waits only for the +# installer process itself and lets setup continue while RustDesk is running. +$installerExited = $installProcess.WaitForExit(60000) +if ($installerExited) { + $installProcess.Refresh() + $installExitCode = $installProcess.ExitCode + Write-InstallLog "Процесс установщика RustDesk завершён с кодом: $installExitCode" +} +else { + $installedExecutable = Find-RustDeskExecutable + if (-not $installedExecutable) { + throw 'Установщик RustDesk не завершился за 60 секунд, установленный rustdesk.exe не найден.' + } + + Write-InstallLog 'Процесс установки не завершился за 60 секунд, но RustDesk уже установлен. Установка продолжается.' + Stop-Process -Id $installProcess.Id -Force -ErrorAction SilentlyContinue + $installExitCode = 0 +} $rustDeskExecutable = $null -for ($attempt = 0; $attempt -lt 60; $attempt++) { +for ($attempt = 0; $attempt -lt 45; $attempt++) { $rustDeskExecutable = Find-RustDeskExecutable if ($rustDeskExecutable) { break } - Start-Sleep -Seconds 2 + Start-Sleep -Seconds 1 } if (-not $rustDeskExecutable) {