# EsaSim worker repair (Windows): puts worker 0.13.2 in place of every EsaSim worker service on this computer and # lets each one update itself from now on. Keeps their pairing, settings, folders and data. # Run in an administrator PowerShell: & ([scriptblock]::Create((irm https://esacode.com/install/fix-worker.ps1))) $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 $server = 'https://esacode.com'; $version = '0.13.2'; $sha = 'b297f3714c5564666212e002ce1342fbb7940f7e7f17d10df283f0ec1b6734ea' function Say($t, $c = 'Gray') { Write-Host $t -ForegroundColor $c } $admin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $admin) { Say 'Run this in an administrator PowerShell (right-click PowerShell > Run as administrator).' Red; return } if ($sha -eq '') { Say "$server offers no ready-made worker for Windows." Red; return } $services = @(Get-CimInstance Win32_Service -Filter "Name LIKE 'EsaSimWorker%'") if ($services.Count -eq 0) { Say 'No EsaSim worker service on this computer.' Yellow Say 'Install one with the command from the dashboard: Computing > Add a worker (or the worker''s page > Re-pair).' Yellow return } # The new program: downloaded once, checked against its SHA-256, and asked its version before anything is changed. $new = Join-Path $env:TEMP "esasim-worker-$version.exe" Say "Downloading worker $version from $server ..." Invoke-WebRequest "$server/install/esasim-worker-windows-x64.exe" -OutFile $new -UseBasicParsing $got = (Get-FileHash $new -Algorithm SHA256).Hash.ToLower() if ($got -ne $sha.ToLower()) { Say "The download does not match its SHA-256 ($got): nothing was changed." Red; Remove-Item $new -Force; return } $says = (& $new version 2>&1 | Out-String) if ($says -notmatch [regex]::Escape($version)) { Say "The new program does not start or does not say version $version ($says): nothing was changed." Red; return } foreach ($s in $services) { $exe = if ($s.PathName -match '^"([^"]+)"') { $Matches[1] } else { ($s.PathName -split '\.exe', 2)[0] + '.exe' } $dir = Split-Path -Parent $exe Say '' Say "$($s.Name): $exe" White try { if ($s.State -ne 'Stopped') { Stop-Service $s.Name -Force; (Get-Service $s.Name).WaitForStatus('Stopped', [TimeSpan]::FromSeconds(60)) } # a worker that did not stop in time: its process goes, so the program can be replaced Get-CimInstance Win32_Process -Filter "Name = 'esasim-worker.exe'" | Where-Object { $_.ExecutablePath -eq $exe } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue } Start-Sleep -Seconds 1 if (Test-Path $exe) { Copy-Item $exe "$exe.old" -Force } Copy-Item $new $exe -Force # the service may change its own folder: that is how it updates itself from now on icacls $dir /grant '*S-1-5-19:(OI)(CI)M' | Out-Null Start-Service $s.Name Say " now worker $version, running, and it updates itself from now on." Green } catch { Say " not done: $($_.Exception.Message)" Red if ((Test-Path "$exe.old") -and -not (Test-Path $exe)) { Copy-Item "$exe.old" $exe -Force } try { Start-Service $s.Name } catch { } } } Remove-Item $new -Force -ErrorAction SilentlyContinue Say '' Say 'Done. In the dashboard (Computing), each worker shows its new version within a minute.' Green