Advanced System Health Monitor

# Advanced System Health Monitor

function Show-Menu {
    Clear-Host
    Write-Host "=== Advanced System Health Monitor ===" -ForegroundColor Cyan
    Write-Host "1.  CPU Information and Usage"
    Write-Host "2.  Memory Usage Details"
    Write-Host "3.  Disk Space and Health"
    Write-Host "4.  Network Statistics"
    Write-Host "5.  Top Resource-Consuming Processes"
    Write-Host "6.  System Uptime and Last Boot Time"
    Write-Host "7.  Installed Windows Updates"
    Write-Host "8.  Event Log Summary"
    Write-Host "9.  Services Status"
    Write-Host "10. Battery Status (for laptops)"
    Write-Host "11. Exit"
}

function Get-CPUInfo {
    Write-Host "`nCPU Information and Usage:" -ForegroundColor Yellow
    $cpu = Get-WmiObject Win32_Processor
    Write-Host "CPU Model: $($cpu.Name)"
    Write-Host "Number of Cores: $($cpu.NumberOfCores)"
    Write-Host "Number of Logical Processors: $($cpu.NumberOfLogicalProcessors)"
    
    $usage = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
    Write-Host "Current CPU Usage: $([math]::Round($usage, 2))%"
}

function Get-MemoryDetails {
    Write-Host "`nMemory Usage Details:" -ForegroundColor Yellow
    $os = Get-CimInstance Win32_OperatingSystem
    $totalMemory = [math]::Round($os.TotalVisibleMemorySize / 1MB, 2)
    $freeMemory = [math]::Round($os.FreePhysicalMemory / 1MB, 2)
    $usedMemory = $totalMemory - $freeMemory
    $percentUsed = [math]::Round(($usedMemory / $totalMemory) * 100, 2)

    Write-Host "Total Memory: $totalMemory GB"
    Write-Host "Used Memory: $usedMemory GB"
    Write-Host "Free Memory: $freeMemory GB"
    Write-Host "Memory Usage: $percentUsed%"

    $pageFile = Get-WmiObject Win32_PageFileUsage
    Write-Host "Page File Size: $([math]::Round($pageFile.AllocatedBaseSize / 1024, 2)) GB"
    Write-Host "Page File Usage: $([math]::Round($pageFile.CurrentUsage / 1024, 2)) GB"
}

function Get-DiskInfo {
    Write-Host "`nDisk Space and Health:" -ForegroundColor Yellow
    $disks = Get-WmiObject Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
    foreach ($disk in $disks) {
        $freeSpace = [math]::Round($disk.FreeSpace / 1GB, 2)
        $totalSpace = [math]::Round($disk.Size / 1GB, 2)
        $usedSpace = $totalSpace - $freeSpace
        $percentFree = [math]::Round(($freeSpace / $totalSpace) * 100, 2)
        
        Write-Host "Drive $($disk.DeviceID):"
        Write-Host "  Total: $totalSpace GB"
        Write-Host "  Used: $usedSpace GB"
        Write-Host "  Free: $freeSpace GB"
        Write-Host "  Percent Free: $percentFree%"
    }

    Write-Host "`nDisk Health:"
    Get-PhysicalDisk | ForEach-Object {
        Write-Host "  $($_.FriendlyName): $($_.HealthStatus)"
    }
}

function Get-NetworkStats {
    Write-Host "`nNetwork Statistics:" -ForegroundColor Yellow
    $adapters = Get-NetAdapter | Where-Object Status -eq "Up"
    foreach ($adapter in $adapters) {
        Write-Host "Adapter: $($adapter.Name)"
        $stats = $adapter | Get-NetAdapterStatistics
        Write-Host "  Bytes Received: $([math]::Round($stats.ReceivedBytes / 1MB, 2)) MB"
        Write-Host "  Bytes Sent: $([math]::Round($stats.SentBytes / 1MB, 2)) MB"
    }
}

function Get-TopProcesses {
    Write-Host "`nTop Resource-Consuming Processes:" -ForegroundColor Yellow
    Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 | 
        Format-Table Name, @{Name='CPU (s)'; Expression={$_.CPU.ToString('N2')}}, @{Name='Memory (MB)'; Expression={[math]::Round($_.WorkingSet / 1MB, 2)}} -AutoSize

    Write-Host "`nTop Memory-Consuming Processes:"
    Get-Process | Sort-Object WorkingSet -Descending | Select-Object -First 5 | 
        Format-Table Name, @{Name='Memory (MB)'; Expression={[math]::Round($_.WorkingSet / 1MB, 2)}}, @{Name='CPU (s)'; Expression={$_.CPU.ToString('N2')}} -AutoSize
}

function Get-SystemUptime {
    Write-Host "`nSystem Uptime and Last Boot Time:" -ForegroundColor Yellow
    $os = Get-CimInstance Win32_OperatingSystem
    $uptime = (Get-Date) - $os.LastBootUpTime
    Write-Host "Last Boot Time: $($os.LastBootUpTime)"
    Write-Host "System Uptime: $($uptime.Days) days, $($uptime.Hours) hours, $($uptime.Minutes) minutes"
}

function Get-WindowsUpdates {
    Write-Host "`nLast 5 Installed Windows Updates:" -ForegroundColor Yellow
    Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 5 | 
        Format-Table HotFixID, Description, InstalledOn -AutoSize
}

function Get-EventLogSummary {
    Write-Host "`nEvent Log Summary (Last 24 Hours):" -ForegroundColor Yellow
    $startTime = (Get-Date).AddHours(-24)
    $logs = @('System', 'Application')
    foreach ($log in $logs) {
        $events = Get-WinEvent -LogName $log -MaxEvents 1000 | Where-Object { $_.TimeCreated -ge $startTime }
        $errorCount = ($events | Where-Object { $_.LevelDisplayName -eq 'Error' }).Count
        $warningCount = ($events | Where-Object { $_.LevelDisplayName -eq 'Warning' }).Count
        Write-Host "$log Log:"
        Write-Host "  Errors: $errorCount"
        Write-Host "  Warnings: $warningCount"
    }
}

function Get-ServicesStatus {
    Write-Host "`nCritical Services Status:" -ForegroundColor Yellow
    $services = @('wuauserv', 'WinDefend', 'BITS', 'Dhcp', 'Dnscache', 'LanmanServer')
    foreach ($service in $services) {
        $status = (Get-Service $service).Status
        Write-Host "$service : $status"
    }
}

function Get-BatteryStatus {
    Write-Host "`nBattery Status:" -ForegroundColor Yellow
    $battery = Get-WmiObject Win32_Battery
    if ($battery) {
        Write-Host "Battery Level: $($battery.EstimatedChargeRemaining)%"
        Write-Host "Status: $($battery.BatteryStatus)"
    } else {
        Write-Host "No battery detected. This might be a desktop computer."
    }
}

do {
    Show-Menu
    $choice = Read-Host "`nEnter your choice (1-11)"

    switch ($choice) {
        "1"  { Get-CPUInfo }
        "2"  { Get-MemoryDetails }
        "3"  { Get-DiskInfo }
        "4"  { Get-NetworkStats }
        "5"  { Get-TopProcesses }
        "6"  { Get-SystemUptime }
        "7"  { Get-WindowsUpdates }
        "8"  { Get-EventLogSummary }
        "9"  { Get-ServicesStatus }
        "10" { Get-BatteryStatus }
        "11" { Write-Host "Exiting program..." -ForegroundColor Yellow; break }
        default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red }
    }

    if ($choice -ne "11") {
        Read-Host "`nPress Enter to continue..."
    }
} while ($choice -ne "11")

This Advanced System Health Monitor includes:

  1. A comprehensive menu with 11 options
  2. Detailed functions for various system health checks:
    • CPU information and real-time usage
    • Detailed memory usage including page file information
    • Disk space, usage, and health status
    • Network statistics for active adapters
    • Top CPU and memory-consuming processes
    • System uptime and last boot time
    • Recent Windows updates
    • Event log summary for the last 24 hours
    • Status of critical system services
    • Battery status for laptops
  3. Use of advanced PowerShell cmdlets and WMI objects
  4. Formatted output for better readability
  5. More comprehensive system information gathering

This tool provides a detailed overview of system health and performance metrics. It’s useful for:

  • Advanced system monitoring and troubleshooting
  • Identifying potential issues across various system components
  • Gathering comprehensive system information for diagnostics
  • Monitoring critical services and system events

This script is suitable for IT professionals, system administrators, or advanced users who need detailed system health information. It provides a single interface to access a wide range of system metrics and statuses, which would otherwise require navigating through multiple system tools or running several different commands.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *