#Requires -RunAsAdministrator <# .SYNOPSIS View deployment logs from S:\DT\ADATA\SCRIPT\DEPLOY\LOGS .DESCRIPTION Helper script to view, search, and analyze deployment logs. .PARAMETER Latest Show only the most recent log files. .PARAMETER Hostname Filter logs by hostname. .PARAMETER Date Filter logs by date (YYYYMMDD format). .PARAMETER Failed Show only logs that indicate failures. .PARAMETER Successful Show only logs that indicate successful deployments. .EXAMPLE .\View-DeploymentLogs.ps1 .EXAMPLE .\View-DeploymentLogs.ps1 -Latest 10 .EXAMPLE .\View-DeploymentLogs.ps1 -Hostname "G1JJVH63ESF" .EXAMPLE .\View-DeploymentLogs.ps1 -Failed .NOTES Author: System Administrator Date: 2025-10-17 Version: 1.0 #> param( [Parameter(Mandatory=$false)] [int]$Latest = 0, [Parameter(Mandatory=$false)] [string]$Hostname, [Parameter(Mandatory=$false)] [string]$Date, [Parameter(Mandatory=$false)] [switch]$Failed, [Parameter(Mandatory=$false)] [switch]$Successful ) $LogDir = "S:\DT\ADATA\SCRIPT\DEPLOY\LOGS" function Show-Menu { Write-Host "`n========================================" -ForegroundColor Cyan Write-Host "WinRM HTTPS Deployment Log Viewer" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" Write-Host "Log Directory: $LogDir" -ForegroundColor Gray Write-Host "" Write-Host "1. List all logs" -ForegroundColor White Write-Host "2. Show latest logs" -ForegroundColor White Write-Host "3. Search by hostname" -ForegroundColor White Write-Host "4. Show failed deployments" -ForegroundColor White Write-Host "5. Show successful deployments" -ForegroundColor White Write-Host "6. Generate summary report" -ForegroundColor White Write-Host "Q. Quit" -ForegroundColor White Write-Host "" } function Get-DeploymentLogs { param([string]$Filter = "*") if (-not (Test-Path $LogDir)) { Write-Host "[ERROR] Log directory not found: $LogDir" -ForegroundColor Red return @() } $logs = Get-ChildItem -Path $LogDir -Filter "$Filter*.txt" | Sort-Object LastWriteTime -Descending return $logs } function Show-LogContent { param([string]$LogPath) Write-Host "`n========================================" -ForegroundColor Cyan Write-Host "Log File: $(Split-Path $LogPath -Leaf)" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" Get-Content $LogPath | ForEach-Object { if ($_ -match 'ERROR|FAIL') { Write-Host $_ -ForegroundColor Red } elseif ($_ -match 'SUCCESS|OK') { Write-Host $_ -ForegroundColor Green } elseif ($_ -match 'WARN') { Write-Host $_ -ForegroundColor Yellow } else { Write-Host $_ } } Write-Host "" } function Get-DeploymentSummary { $logs = Get-DeploymentLogs $summary = @{ Total = $logs.Count Successful = 0 Failed = 0 Hostnames = @{} } foreach ($log in $logs) { $content = Get-Content $log.FullName -Raw # Extract hostname from filename $filename = $log.Name if ($filename -match '^([^-]+)-') { $hostname = $matches[1] if (-not $summary.Hostnames.ContainsKey($hostname)) { $summary.Hostnames[$hostname] = @{ Total = 0 Successful = 0 Failed = 0 LastDeployment = $log.LastWriteTime } } $summary.Hostnames[$hostname].Total++ } # Check if successful or failed if ($content -match 'SUCCESS.*Complete|Setup Complete') { $summary.Successful++ if ($hostname) { $summary.Hostnames[$hostname].Successful++ } } elseif ($content -match 'ERROR|FAIL|failed') { $summary.Failed++ if ($hostname) { $summary.Hostnames[$hostname].Failed++ } } } return $summary } function Show-SummaryReport { Write-Host "`n========================================" -ForegroundColor Cyan Write-Host "Deployment Summary Report" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" $summary = Get-DeploymentSummary Write-Host "Total Logs: $($summary.Total)" -ForegroundColor White Write-Host "Successful: $($summary.Successful)" -ForegroundColor Green Write-Host "Failed: $($summary.Failed)" -ForegroundColor Red Write-Host "" if ($summary.Hostnames.Count -gt 0) { Write-Host "Deployment by Hostname:" -ForegroundColor Yellow Write-Host "" $summary.Hostnames.GetEnumerator() | Sort-Object { $_.Value.LastDeployment } -Descending | ForEach-Object { $hostname = $_.Key $stats = $_.Value $status = if ($stats.Successful -gt 0) { "SUCCESS" } else { "FAILED" } $color = if ($stats.Successful -gt 0) { "Green" } else { "Red" } Write-Host " $hostname - $status (Attempts: $($stats.Total), Last: $($stats.LastDeployment))" -ForegroundColor $color } } Write-Host "" } # Main execution try { # Check if log directory exists if (-not (Test-Path $LogDir)) { Write-Host "[WARN] Log directory does not exist: $LogDir" -ForegroundColor Yellow Write-Host "Creating log directory..." -ForegroundColor Yellow New-Item -ItemType Directory -Path $LogDir -Force | Out-Null Write-Host "[OK] Log directory created" -ForegroundColor Green exit 0 } # Handle command-line parameters if ($Latest -gt 0) { Write-Host "`nShowing $Latest most recent logs:" -ForegroundColor Cyan Write-Host "" $logs = Get-DeploymentLogs | Select-Object -First $Latest foreach ($log in $logs) { Write-Host "$($log.Name) - $(Get-Date $log.LastWriteTime -Format 'yyyy-MM-dd HH:mm:ss')" -ForegroundColor White } Write-Host "" exit 0 } if ($Hostname) { Write-Host "`nShowing logs for hostname: $Hostname" -ForegroundColor Cyan $logs = Get-DeploymentLogs -Filter $Hostname if ($logs.Count -eq 0) { Write-Host "[WARN] No logs found for hostname: $Hostname" -ForegroundColor Yellow exit 0 } foreach ($log in $logs) { Show-LogContent -LogPath $log.FullName } exit 0 } if ($Failed) { Write-Host "`nShowing failed deployments:" -ForegroundColor Red Write-Host "" $logs = Get-DeploymentLogs foreach ($log in $logs) { $content = Get-Content $log.FullName -Raw if ($content -match 'ERROR|FAIL|failed') { Write-Host "$($log.Name) - FAILED" -ForegroundColor Red } } Write-Host "" exit 0 } if ($Successful) { Write-Host "`nShowing successful deployments:" -ForegroundColor Green Write-Host "" $logs = Get-DeploymentLogs foreach ($log in $logs) { $content = Get-Content $log.FullName -Raw if ($content -match 'SUCCESS.*Complete|Setup Complete') { Write-Host "$($log.Name) - SUCCESS" -ForegroundColor Green } } Write-Host "" exit 0 } # Interactive menu if no parameters while ($true) { Show-Menu $choice = Read-Host "Select an option" switch ($choice) { "1" { Write-Host "`nAll deployment logs:" -ForegroundColor Cyan Write-Host "" $logs = Get-DeploymentLogs foreach ($log in $logs) { Write-Host "$($log.Name) - $(Get-Date $log.LastWriteTime -Format 'yyyy-MM-dd HH:mm:ss')" -ForegroundColor White } Write-Host "" Read-Host "Press Enter to continue" } "2" { $count = Read-Host "How many recent logs to show?" Write-Host "`nShowing $count most recent logs:" -ForegroundColor Cyan Write-Host "" $logs = Get-DeploymentLogs | Select-Object -First ([int]$count) foreach ($log in $logs) { Write-Host "$($log.Name) - $(Get-Date $log.LastWriteTime -Format 'yyyy-MM-dd HH:mm:ss')" -ForegroundColor White } Write-Host "" Read-Host "Press Enter to continue" } "3" { $searchHostname = Read-Host "Enter hostname to search" Write-Host "`nShowing logs for hostname: $searchHostname" -ForegroundColor Cyan $logs = Get-DeploymentLogs -Filter $searchHostname if ($logs.Count -eq 0) { Write-Host "[WARN] No logs found for hostname: $searchHostname" -ForegroundColor Yellow } else { foreach ($log in $logs) { Show-LogContent -LogPath $log.FullName } } Read-Host "Press Enter to continue" } "4" { Write-Host "`nFailed deployments:" -ForegroundColor Red Write-Host "" $logs = Get-DeploymentLogs foreach ($log in $logs) { $content = Get-Content $log.FullName -Raw if ($content -match 'ERROR|FAIL|failed') { Write-Host "$($log.Name) - FAILED" -ForegroundColor Red } } Write-Host "" Read-Host "Press Enter to continue" } "5" { Write-Host "`nSuccessful deployments:" -ForegroundColor Green Write-Host "" $logs = Get-DeploymentLogs foreach ($log in $logs) { $content = Get-Content $log.FullName -Raw if ($content -match 'SUCCESS.*Complete|Setup Complete') { Write-Host "$($log.Name) - SUCCESS" -ForegroundColor Green } } Write-Host "" Read-Host "Press Enter to continue" } "6" { Show-SummaryReport Read-Host "Press Enter to continue" } "Q" { Write-Host "`nExiting..." -ForegroundColor Cyan exit 0 } default { Write-Host "`n[ERROR] Invalid option" -ForegroundColor Red Start-Sleep -Seconds 1 } } } } catch { Write-Host "`n[ERROR] $($_.Exception.Message)" -ForegroundColor Red exit 1 }