Server Status Report

Just another Tech site

Server Status Report

Server Status Report from PowerShell to HTML

for CPU, Memory, IP Address, and Hard Drive information.


# Define the input file containing server names (one per line)
$ServerListFile = "D:\Lists\Servers.txt"  # Text file containing server names (one per line)
# Define the output HTML file
$OutputHtmlFile = "C:\Logs\ServerStatusReporttbl.html" # Path to save the HTML report

# Initialize an array to store server data
$ServerData = @()

# Read server names from the text file
$Servers = Get-Content -Path $ServerListFile

foreach ($Server in $Servers) {
    try {
        # Get CPU usage
        $CPU = Get-CimInstance -ClassName Win32_Processor -ComputerName $Server | 
               Measure-Object -Property LoadPercentage -Average | 
               Select-Object -ExpandProperty Average

        # Get memory usage
        $Memory = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $Server
        $TotalMemory = [math]::Round($Memory.TotalVisibleMemorySize / 1MB, 2)
        $FreeMemory = [math]::Round($Memory.FreePhysicalMemory / 1MB, 2)
        $UsedMemory = $TotalMemory - $FreeMemory

        # Get network IP address
        $IPAddresses = (Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -ComputerName $Server | 
                        Where-Object { $_.IPAddress -ne $null }).IPAddress -join ", "

        # Get disk information
        $Disks = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $Server | 
                 Where-Object { $_.DriveType -eq 3 } # Only fixed drives

        foreach ($Disk in $Disks) {
            $DiskSize = [math]::Round($Disk.Size / 1GB, 2)
            $FreeSpace = [math]::Round($Disk.FreeSpace / 1GB, 2)

            # Add data to the array
            $ServerData += [PSCustomObject]@{
                ServerName   = $Server
                CPUUsage     = "$CPU%"
                UsedMemory   = "$UsedMemory GB"
                FreeMemory   = "$FreeMemory GB"
                TotalMemory  = "$TotalMemory GB"
                IPAddress    = $IPAddresses
                DriveLetter  = $Disk.DeviceID
                DiskSize     = "$DiskSize GB"
                FreeDiskSpace = "$FreeSpace GB"
            }
        }
    } catch {
        Write-Warning "Failed to retrieve data for server: $Server"
    }
}

# Convert the data to an HTML table
$HtmlContent = $ServerData | ConvertTo-Html -Property ServerName, CPUUsage, UsedMemory, FreeMemory, TotalMemory, IPAddress, DriveLetter, DiskSize, FreeDiskSpace -Head "<style>table {border-collapse: collapse;} th, td {border: 1px solid black; padding: 5px;}</style>" -Title "Server Health Report"

# Save the HTML content to a file
$HtmlContent | Out-File -FilePath $OutputHtmlFile

Write-Host "Report generated successfully at $OutputHtmlFile"