Windows Lucidity AutoScaler Agent Uninstall

Prev Next

Before proceeding with agent uninstall, make sure you follow guidelines mention here.

Run the following commands as Administrator from Windows Powershell:

param ()

# Agent service names
$NW_AGENT_NAME = "nwAgent"
$MAIN_AGENT_NAME = "autoscalerAgent"
$SYNC_AGENT_NAME = "LuciditySyncSvc"

# Default installation and log paths
$LOG_PATH = "C:\ProgramData\lucidity"
$AGENT_PATH = "C:\Program Files\Lucidity"

function removeFolder {
    param ($path)

    # Safely removes a directory and all its contents
    # NOTE: This is a destructive operation. Ensure the path is correct before execution.
    if (Test-Path $path) {

        # Create a temporary empty folder to efficiently purge contents using robocopy
        New-Item -ItemType Directory -Force -Path C:\lucidityEmptyFolder | Out-Null

        # Mirror empty folder to target path to clear contents (faster for large directories)
        robocopy C:\lucidityEmptyFolder $path /purge | Out-Null

        # Remove the target directory
        Remove-Item -Recurse -Force $path -ErrorAction SilentlyContinue

        # Clean up temporary folder
        Remove-Item -Recurse -Force C:\lucidityEmptyFolder -ErrorAction SilentlyContinue
    }
}

function uninstall_agent {
    param ($agent_name)

    # Stops and removes a Windows service from Service Control Manager (SCM)
    Write-Host "Stopping and removing service $agent_name from SCM..."

    $service = Get-Service -Name $agent_name -ErrorAction SilentlyContinue

    # Exit early if service is not present
    if ($null -eq $service) {
        Write-Host "$agent_name is not installed."
        return
    }

    # Retry loop to handle transient failures during service removal
    for ($i = 1; $i -le 5; $i++) {
        try {
            # Stop service if running
            if ($service.Status -eq 'Running') {
                Stop-Service -Name $agent_name -Force -ErrorAction SilentlyContinue
                Start-Sleep -Seconds 3
            }

            # Delete service from SCM
            sc.exe delete $agent_name | Out-Null
            Start-Sleep -Seconds 2

            # Re-check if service still exists
            $service = Get-Service -Name $agent_name -ErrorAction SilentlyContinue
            if ($null -eq $service) {
                Write-Host "$agent_name removed successfully."
                return
            }

            # Fail after max retries
            if ($i -eq 5) {
                Write-Host "$agent_name still exists after 5 attempts!"
                exit 2  
            }
        } catch {
            Write-Host "Error stopping/removing ${agent_name}: $_"
        }
    }
}

function uninstall_sync_agent {

    # Handles removal of sync agent using its dedicated executable
    Write-Host "Stopping and removing $SYNC_AGENT_NAME from SCM..."

    $service = Get-Service -Name $SYNC_AGENT_NAME -ErrorAction SilentlyContinue

    if ($null -eq $service) {
        Write-Host "$SYNC_AGENT_NAME not installed."
        return
    }

    for ($i = 1; $i -le 5; $i++) {
        try {
            # Stop sync agent using its binary (custom control mechanism)
            if ($service.Status -eq 'Running') {
                & "C:\lucidity\lucidity_agent\agent\syncAgent.exe" stop
                Start-Sleep -Seconds 3
            }

            # Remove sync agent service using its binary
            & "C:\lucidity\lucidity_agent\agent\syncAgent.exe" delete
            Start-Sleep -Seconds 2

            # Verify removal
            $service = Get-Service -Name $SYNC_AGENT_NAME -ErrorAction SilentlyContinue
            if ($null -eq $service) {
                Write-Host "$SYNC_AGENT_NAME removed successfully."
                return
            }

            if ($i -eq 5) {
                Write-Host "$SYNC_AGENT_NAME still exists after 5 attempts!"
                exit 3  
            }
        } catch {
            Write-Host "Error stopping/removing ${SYNC_AGENT_NAME}: $_"
        }
    }
}

# --- Main Execution ---

# IMPORTANT:
# Ensure all mount points and volumes have been deboarded before running this script.
# This script permanently removes agent services, binaries, and logs.

Write-Host "`n**** STEP 1: Uninstalling Agent Services"
uninstall_agent $NW_AGENT_NAME
uninstall_agent $MAIN_AGENT_NAME
uninstall_sync_agent

Write-Host "`n**** STEP 2: Removing Lucidity Installation Folders"
removeFolder $AGENT_PATH
removeFolder $LOG_PATH

Write-Host "`n**** Uninstallation complete!"