& { $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' if (-not [Environment]::Is64BitOperatingSystem -or [Environment]::OSVersion.Platform -ne 'Win32NT') { throw 'DarwinPC requires Windows x64.' } $releaseVersion = '0.8.3' $expectedHash = 'F577888616D43E488A6BCF9C8EDADA748E1C6B203E1E0597505F58967CE08F8E' $downloadUri = 'https://h.firstend.ru/downloads/darwin/0.8.3/DarwinPC.exe' $installDirectory = Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) ('Orion\DarwinPC\' + $releaseVersion) $executable = Join-Path $installDirectory 'DarwinPC.exe' [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 New-Item -ItemType Directory -Path $installDirectory -Force | Out-Null if (-not (Test-Path -LiteralPath $executable) -or (Get-FileHash -LiteralPath $executable -Algorithm SHA256).Hash -ne $expectedHash) { $downloadFile = Join-Path $installDirectory ([Guid]::NewGuid().ToString('N') + '.download') try { Write-Host 'Downloading DarwinPC...' Invoke-WebRequest -UseBasicParsing -Uri $downloadUri -OutFile $downloadFile if ((Get-FileHash -LiteralPath $downloadFile -Algorithm SHA256).Hash -ne $expectedHash) { throw 'DarwinPC download verification failed.' } Move-Item -LiteralPath $downloadFile -Destination $executable -Force } finally { if (Test-Path -LiteralPath $downloadFile) { Remove-Item -LiteralPath $downloadFile -Force } } } $stopInfo = New-Object System.Diagnostics.ProcessStartInfo $stopInfo.FileName = $executable $stopInfo.Arguments = '--status' $stopInfo.UseShellExecute = $false $stopInfo.CreateNoWindow = $true $stopInfo.RedirectStandardOutput = $true $stopInfo.RedirectStandardError = $true $previousAgentId = $null foreach ($command in @('--status', '--exit')) { $stopInfo.Arguments = $command $stopProcess = [Diagnostics.Process]::Start($stopInfo) try { if (-not $stopProcess.WaitForExit(15000)) { throw 'The previous DarwinPC agent did not respond.' } if ($command -eq '--status' -and $stopProcess.ExitCode -eq 0) { $status = $stopProcess.StandardOutput.ReadToEnd() | ConvertFrom-Json $previousAgentId = $status.Data.ProcessId } if ($command -eq '--status' -and -not $previousAgentId) { break } } finally { $stopProcess.Dispose() } } if ($previousAgentId) { $previousAgent = Get-Process -Id $previousAgentId -ErrorAction SilentlyContinue if ($previousAgent) { try { if (-not $previousAgent.WaitForExit(15000)) { throw 'The previous DarwinPC agent is still shutting down.' } } finally { $previousAgent.Dispose() } } } Start-Process -FilePath $executable -WindowStyle Hidden Write-Host 'DarwinPC started. Allow the Windows firewall setup, then select this PC on your watch.' }