From 08eceb65242d0495a7b9574e880608fbfd5ceb8f Mon Sep 17 00:00:00 2001 From: Jeroen Verburgh Date: Tue, 18 Mar 2025 23:51:08 +0100 Subject: [PATCH 1/2] Implemented in-place upgrade. If no arguments are provided, the script prefers to reuse an existing Server ID if possible. Omission of '0' now means no processes/services need to be monitored. Postponed v1 agent uninstall so that its Server ID remains available during $SID initalization. --- install.ps1 | 51 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/install.ps1 b/install.ps1 index 32832d9..ef60a27 100644 --- a/install.ps1 +++ b/install.ps1 @@ -45,6 +45,40 @@ if ([System.Environment]::OSVersion.Version.Build -lt 17763 -and [Net.ServicePoi [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $commonSecurityProtocols } +# Installation folder +$folderPath = "C:\Program Files\HetrixTools" + +# Get the Server ID +$SID = $args[0] +if ([string]::IsNullOrWhiteSpace($SID)) { + # Try to reuse Server ID from existing v2 agent installation + $existingConfig = "$folderPath\hetrixtools.cfg" + if (Test-Path -Path $existingConfig) { + foreach ($line in Get-Content $existingConfig) { + if ($line -match "^\s*SID\s*=\s*([a-z0-9]{32})\s*$") { + $SID = $Matches[1] + break + } + } + } + # Try to reuse Server ID from existing v1 agent installation + $existingSID = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HetrixToolsAgent\Parameters" + if ([string]::IsNullOrWhiteSpace($SID) -and (Test-Path -Path $existingSID)) { + $SID = (Get-ItemProperty -Path $existingSID -Name 'sid').sid + } +} + +# Make sure the SID is plausible +Write-Host "Checking Server ID (SID)..." +if ($SID -match "^[a-z0-9]{32}$") { + Write-Host "Server ID: $SID" -ForegroundColor Cyan +} +else { + Write-Host "Error: Server ID is empty." -ForegroundColor Yellow + exit +} +Write-Host "... done." + # Find and uninstall v1 agent Write-Host "Checking for old agent..." $processName = "HetrixToolsAgent.exe" @@ -76,20 +110,6 @@ if ($processes) { } Write-Host "... done." -# Get the Server ID -$SID = $args[0] - -# Make sure the SID is not empty -Write-Host "Checking Server ID (SID)..." -if ($SID -eq "") { - Write-Host "Error: Server ID is empty." - exit -} -Write-Host "... done." - -# Installation folder -$folderPath = "C:\Program Files\HetrixTools" - # Check if the folder exists Write-Host "Checking installation folder..." if (-Not (Test-Path -Path $folderPath)) { @@ -122,7 +142,7 @@ Write-Host "Inserting the Server ID into the config file..." # Check if any processes/services need to be monitored Write-Host "Checking if any processes/services need to be monitored..." -if ($args[1] -ne "0") { +if ($null -ne $args[1] -and $args[1] -ne "0") { # Insert the processes/services into the config file Write-Host "Inserting the processes/services into the config file..." # Split the string into an array and filter out empty elements @@ -173,6 +193,7 @@ if ($existingTask) { Unregister-ScheduledTask -TaskName $taskName -Confirm:$false } Write-Host "... done." + Write-Host "Creating the new scheduled task..." # Calculate the next full minute $currentTime = Get-Date From a9a1d6fb9f1273651415b355e053ea5855908a49 Mon Sep 17 00:00:00 2001 From: Jeroen Verburgh Date: Wed, 19 Mar 2025 00:03:58 +0100 Subject: [PATCH 2/2] Moved v1 agent uninstall after installation has completed and before v2 agent is started. Added sanity checks to ensure v1 agent remains available until v2 agent is ready to start. --- install.ps1 | 100 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 63 insertions(+), 37 deletions(-) diff --git a/install.ps1 b/install.ps1 index ef60a27..9a6cab3 100644 --- a/install.ps1 +++ b/install.ps1 @@ -79,37 +79,6 @@ else { } Write-Host "... done." -# Find and uninstall v1 agent -Write-Host "Checking for old agent..." -$processName = "HetrixToolsAgent.exe" -$processes = Get-WmiObject Win32_Process | Where-Object { $_.Name -eq $processName } -$paths = @() -if ($processes) { - foreach ($process in $processes) { - $processPath = $process.ExecutablePath - if ($processPath) { - Write-Host "Found process $($process.ProcessId) running from path $processPath" - $paths += $processPath.Trim() - } else { - Write-Host "Unable to retrieve the path for the process $($process.ProcessId)." - } - } - $uniquePaths = $paths | Select-Object -Unique - if ($uniquePaths.Count -eq 1) { - $finalPath = $uniquePaths - Write-Host "The unique path for all instances is $finalPath" - Write-Host "Uninstalling the old agent..." - & "$finalPath" stop - & "$finalPath" remove - & taskkill /IM "HetrixToolsAgent.exe" /F - } else { - Write-Host "Error: Cannot uninstall the old agent because there are multiple instances running from different paths." - Write-Host "Please manually uninstall the old agent and then re-run this install script again." - exit 1 - } -} -Write-Host "... done." - # Check if the folder exists Write-Host "Checking installation folder..." if (-Not (Test-Path -Path $folderPath)) { @@ -128,13 +97,30 @@ if (-Not (Test-Path -Path $folderPath)) { Write-Host "... done." # Download the agent -Write-Host "Downloading the agent..." +$items = @( + [PSCustomObject] @{ + description = "agent" + source = "https://raw.githubusercontent.com/hetrixtools/agent-windows/$BRANCH/hetrixtools_agent.ps1" + target = "$folderPath\hetrixtools_agent.ps1" + }, + [PSCustomObject] @{ + description = "config file" + source = "https://raw.githubusercontent.com/hetrixtools/agent-windows/$BRANCH/hetrixtools.cfg" + target = "$folderPath\hetrixtools.cfg" + } +) $wc = New-Object System.Net.WebClient -$wc.DownloadFile("https://raw.githubusercontent.com/hetrixtools/agent-windows/$BRANCH/hetrixtools_agent.ps1", "$folderPath\hetrixtools_agent.ps1") -Write-Host "... done." -Write-Host "Downloading the config file..." -$wc.DownloadFile("https://raw.githubusercontent.com/hetrixtools/agent-windows/$BRANCH/hetrixtools.cfg", "$folderPath\hetrixtools.cfg") -Write-Host "... done." +foreach ($item in $items) { + Write-Host "Downloading the ${$item.description}..." + $wc.DownloadFile($item.source, $item.target) + if (Test-Path -Path $item.target) { + Write-Host "... done." + } + else { + Write-Host "... failed." -ForegroundColor Yellow + exit + } +} # Insert the Server ID into the config file Write-Host "Inserting the Server ID into the config file..." @@ -212,6 +198,46 @@ Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTri $task = Get-ScheduledTask -TaskName $taskName $task.Settings.ExecutionTimeLimit = "PT2M" Set-ScheduledTask -TaskName $taskName -TaskPath "\" -Settings $task.Settings + +# Check if agent is ready to replace v1 agent and/or to confirm installation +$taskExecutionTimeLimit = (Get-ScheduledTask -TaskName $taskName).Settings.ExecutionTimeLimit +if ($taskExecutionTimeLimit -eq $task.Settings.ExecutionTimeLimit) { + Write-Host "... done." +} +else { + Write-Host "... failed." -ForegroundColor Yellow + exit +} + +# Find and uninstall v1 agent +Write-Host "Checking for old agent..." +$processName = "HetrixToolsAgent.exe" +$processes = Get-WmiObject Win32_Process | Where-Object { $_.Name -eq $processName } +$paths = @() +if ($processes) { + foreach ($process in $processes) { + $processPath = $process.ExecutablePath + if ($processPath) { + Write-Host "Found process $($process.ProcessId) running from path $processPath" + $paths += $processPath.Trim() + } else { + Write-Host "Unable to retrieve the path for the process $($process.ProcessId)." + } + } + $uniquePaths = $paths | Select-Object -Unique + if ($uniquePaths.Count -eq 1) { + $finalPath = $uniquePaths + Write-Host "The unique path for all instances is $finalPath" + Write-Host "Uninstalling the old agent..." + & "$finalPath" stop + & "$finalPath" remove + & taskkill /IM "HetrixToolsAgent.exe" /F + } else { + Write-Host "Error: Cannot uninstall the old agent because there are multiple instances running from different paths." + Write-Host "Please manually uninstall the old agent and then re-run this install script again." + exit 1 + } +} Write-Host "... done." # Start the scheduled task