# ============================================ # UDC Application Update Script (PowerShell) # ============================================ param( [string]$Version ) # Set error action preference $ErrorActionPreference = "Stop" # Set variables $UDC_PATH = "C:\Program Files\UDC" $VERSION_FILE = "S:\SPC\UDC\UDC_Update.txt" $LOG_DIR = "S:\DT\Cameron\UDC\logs" # Get hostname and timestamp $HOSTNAME = $env:COMPUTERNAME $TIMESTAMP = Get-Date -Format "yyyyMMdd_HHmmss" $LOG_FILE = "$LOG_DIR\logs_$($HOSTNAME)_$TIMESTAMP.txt" # Flag to track if we should skip the update $SKIP_UPDATE = $false # Function to write logs function Write-Log { param([string]$Message) $LogMessage = "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] $Message" Write-Host $Message Add-Content -Path $LOG_FILE -Value $LogMessage -ErrorAction SilentlyContinue } # Create log directory if needed try { if (-not (Test-Path $LOG_DIR)) { New-Item -Path $LOG_DIR -ItemType Directory -Force | Out-Null } } catch { Write-Host "WARNING: Cannot access log directory $LOG_DIR" Write-Host "Update will continue but logging is disabled." } Write-Log "============================================" Write-Log "UDC Update Script Started on $HOSTNAME" Write-Log "============================================" # ============================================ # Step 1: Check if PPMon.exe or ppdcs.exe are running # ============================================ Write-Host "Checking for conflicting processes..." $conflictingProcesses = Get-Process -Name "PPMon", "ppdcs" -ErrorAction SilentlyContinue if ($conflictingProcesses) { foreach ($proc in $conflictingProcesses) { Write-Log "$($proc.Name).exe is running. Skipping update process." Write-Host "$($proc.Name).exe is running. Update will be skipped." } $SKIP_UPDATE = $true } # ============================================ # Step 2: Check if UDC directory exists # ============================================ Write-Host "Checking if UDC is installed..." if (-not (Test-Path $UDC_PATH)) { Write-Log "UDC directory not found. Exiting." Write-Host "UDC is not installed on this machine." exit 0 } # ============================================ # Step 3: Check if UDC.exe exists # ============================================ $UDC_EXE = "$UDC_PATH\UDC.exe" if (-not (Test-Path $UDC_EXE)) { Write-Log "UDC.exe not found. Exiting." Write-Host "UDC.exe not found in installation directory." exit 0 } # ============================================ # ONLY PROCEED WITH UPDATE IF NOT SKIPPED # ============================================ if (-not $SKIP_UPDATE) { # ============================================ # Step 4: Determine target version # ============================================ if ($Version) { $NETWORK_VERSION = $Version Write-Host "Using override version: $NETWORK_VERSION" Write-Log "Override version specified: $NETWORK_VERSION" } else { if (-not (Test-Path $VERSION_FILE)) { Write-Log "Version file not found: $VERSION_FILE" Write-Host "ERROR: Version file not found." exit 1 } Write-Host "Reading version information..." try { $versionContent = Get-Content $VERSION_FILE | Where-Object { $_ -match "Version:" } $NETWORK_VERSION = ($versionContent -split ":")[1].Trim() if ([string]::IsNullOrEmpty($NETWORK_VERSION)) { throw "Could not parse network version" } Write-Host "Network version: $NETWORK_VERSION" Write-Log "Network version: $NETWORK_VERSION" } catch { Write-Log "Could not read network version. Error: $_" Write-Host "ERROR: Could not determine network version." exit 1 } } # Build source path from version $SOURCE_PATH = "S:\SPC\UDC\UDC_$NETWORK_VERSION" # ============================================ # Step 5: Check if source files exist # ============================================ if (-not (Test-Path $SOURCE_PATH)) { Write-Log "Source path not found: $SOURCE_PATH" Write-Host "ERROR: Update source files not found at $SOURCE_PATH" exit 1 } # ============================================ # Step 6: Get local UDC.exe version # ============================================ try { $fileVersion = (Get-Item $UDC_EXE).VersionInfo.FileVersion # Trim to 3 parts if it has 4 (e.g., 1.0.30.0 -> 1.0.30) $versionParts = $fileVersion.Split('.') if ($versionParts.Count -eq 4) { $LOCAL_VERSION = "$($versionParts[0]).$($versionParts[1]).$($versionParts[2])" } else { $LOCAL_VERSION = $fileVersion } Write-Host "Local version: $LOCAL_VERSION" Write-Log "Local version: $LOCAL_VERSION" } catch { Write-Log "Could not read local version. Error: $_" Write-Host "ERROR: Could not determine local version." exit 1 } # ============================================ # Step 7: Compare versions # ============================================ try { $netVer = [version]$NETWORK_VERSION $localVer = [version]$LOCAL_VERSION if ($netVer -eq $localVer) { Write-Log "Versions match ($LOCAL_VERSION). No update needed." Write-Host "Version is current. No update required." $SKIP_UPDATE = $true } elseif ($netVer -le $localVer) { Write-Log "Network version ($NETWORK_VERSION) not newer than local ($LOCAL_VERSION)." Write-Host "Local version is current or newer. No update required." $SKIP_UPDATE = $true } else { Write-Host "Update required: $LOCAL_VERSION -> $NETWORK_VERSION" Write-Log "Update required: $LOCAL_VERSION -> $NETWORK_VERSION" } } catch { Write-Log "Error comparing versions. Error: $_" Write-Host "ERROR: Could not compare versions." exit 1 } # ============================================ # Step 8: Perform update if needed # ============================================ if (-not $SKIP_UPDATE) { # Kill UDC.exe if running Write-Host "Checking if UDC.exe is running..." $udcProcess = Get-Process -Name "UDC" -ErrorAction SilentlyContinue if ($udcProcess) { Write-Host "UDC.exe is running. Stopping process..." Write-Log "Killing UDC.exe" try { Stop-Process -Name "UDC" -Force Start-Sleep -Seconds 2 Write-Log "UDC.exe stopped successfully" } catch { Write-Log "Warning: Could not stop UDC.exe. Error: $_" Write-Host "WARNING: Could not stop UDC.exe" } } # Copy update files Write-Host "Copying update files..." Write-Log "Copying files from $SOURCE_PATH to $UDC_PATH" try { # Use robocopy for better performance and logging $robocopyArgs = @( $SOURCE_PATH, $UDC_PATH, "/E", # Copy subdirectories including empty ones "/NFL", # No file list "/NDL", # No directory list "/NJH", # No job header "/NJS", # No job summary "/NC", # No class "/NS", # No size "/NP" # No progress ) $result = robocopy @robocopyArgs # Robocopy exit codes: 0-7 are success, 8+ are errors if ($LASTEXITCODE -ge 8) { throw "Robocopy failed with exit code $LASTEXITCODE" } Write-Host "Files copied successfully." Write-Log "Files copied successfully." } catch { Write-Log "ERROR: File copy failed. Error: $_" Write-Host "ERROR: Failed to copy update files." exit 1 } } } # ============================================ # Step 9: Ensure UDC.exe is running (ONLY if no conflicting processes) # ============================================ # Re-check for conflicting processes before starting UDC $conflictingProcesses = Get-Process -Name "PPMon", "ppdcs" -ErrorAction SilentlyContinue if ($conflictingProcesses) { Write-Log "Conflicting processes still running. Will not start UDC.exe" Write-Host "Conflicting processes detected. UDC.exe will not be started." } else { Write-Host "Verifying UDC.exe is running..." $udcProcess = Get-Process -Name "UDC" -ErrorAction SilentlyContinue if (-not $udcProcess) { Write-Host "UDC.exe is not running. Starting it now..." Write-Log "UDC.exe not running. Starting UDC.exe" try { Start-Process -FilePath $UDC_EXE Start-Sleep -Seconds 2 # Verify it started $udcProcess = Get-Process -Name "UDC" -ErrorAction SilentlyContinue if ($udcProcess) { Write-Log "UDC.exe started successfully." Write-Host "UDC.exe started successfully!" } else { Write-Log "WARNING: UDC.exe may not have started." Write-Host "WARNING: UDC.exe may not have started properly." } } catch { Write-Log "WARNING: Could not start UDC.exe. Error: $_" Write-Host "WARNING: Could not start UDC.exe" } } else { Write-Log "UDC.exe is already running." Write-Host "UDC.exe is already running." } } Write-Log "UDC Update Script Completed" Write-Log "============================================" exit 0