PowerShell script for server information in an email report

Just another Tech site

PowerShell script for server information in an email report

PowerShell script that gathers server CPU and memory utilization, disk space, and uptime, generates a report, and sends it via email to IT Support. This script uses WMI and requires SMTP configuration for email functionality.

# **Configuration Section**
$SMTPServer = "smtp.yourdomain.com"  # Replace with your SMTP server
$SMTPPort = 587                     # Replace with your SMTP port (e.g., 25, 587)
$EmailFrom = "server-report@yourdomain.com"
$EmailTo = "itsupport@yourdomain.com"
$EmailSubject = "Server Health Report"
$SMTPUsername = "your-email-username"  # Replace with your SMTP username
$SMTPPassword = "your-email-password"  # Replace with your SMTP password

# **Gather Server Information**
$ServerName = $env:COMPUTERNAME
$CPU = Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select-Object -ExpandProperty Average
$Memory = Get-WmiObject Win32_OperatingSystem
$TotalMemory = [math]::Round($Memory.TotalVisibleMemorySize / 1MB, 2)
$FreeMemory = [math]::Round($Memory.FreePhysicalMemory / 1MB, 2)
$UsedMemory = [math]::Round($TotalMemory - $FreeMemory, 2)
$DiskInfo = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" | ForEach-Object {
    [PSCustomObject]@{
        DriveLetter = $_.DeviceID
        FreeSpaceGB = [math]::Round($_.FreeSpace / 1GB, 2)
        TotalSpaceGB = [math]::Round($_.Size / 1GB, 2)
        UsedSpaceGB = [math]::Round(($_.Size - $_.FreeSpace) / 1GB, 2)
    }
}
$Uptime = (Get-WmiObject Win32_OperatingSystem).LastBootUpTime
$UptimeFormatted = (Get-Date) - ([Management.ManagementDateTimeConverter]::ToDateTime($Uptime))

# **Generate Report**
$Report = @"
Server Health Report for $ServerName

CPU Utilization: $CPU%
Memory Usage: $UsedMemory GB used out of $TotalMemory GB
Disk Usage:
@($DiskInfo | ForEach-Object { "Drive: $($_.DriveLetter) - Free: $($_.FreeSpaceGB) GB, Used: $($_.UsedSpaceGB) GB, Total: $($_.TotalSpaceGB) GB" }) -join "`n"
Uptime: $([math]::Floor($UptimeFormatted.TotalDays)) days, $($UptimeFormatted.Hours) hours, $($UptimeFormatted.Minutes) minutes
"@

# **Send Email**
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -Body $Report -SmtpServer $SMTPServer -Port $SMTPPort -Credential (New-Object PSCredential($SMTPUsername, (ConvertTo-SecureString $SMTPPassword -AsPlainText -Force))) -UseSsl

Key Notes:

  1. SMTP Configuration: Replace placeholders with your actual SMTP server details and credentials.
  2. Permissions: Ensure the script is run with sufficient privileges to access WMI and send emails.
  3. Disk Filtering: The script filters only local drives (DriveType=3).
  4. Customization: You can modify the email body or add additional metrics as needed.

This script is designed to be run on a Windows server.

Another way 

# Define variables
$SMTPServer = "localhost"  # Replace with your SMTP server if not localhost
$From = "server-report@yourdomain.com"
$To = "itsupport@yourdomain.com"
$Subject = "Server Disk Space and Uptime Report"
$Body = @()

# Get server uptime
$Uptime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$UptimeFormatted = (Get-Date) - $Uptime
$Body += "Server Uptime: $([math]::Floor($UptimeFormatted.TotalDays)) days, $($UptimeFormatted.Hours) hours, $($UptimeFormatted.Minutes) minutes`n"

# Get disk space information
$Body += "Disk Space Report:`n"
$Disks = Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3"  # Only fixed drives
foreach ($Disk in $Disks) {
    $FreeSpaceGB = [math]::Round($Disk.FreeSpace / 1GB, 2)
    $TotalSpaceGB = [math]::Round($Disk.Size / 1GB, 2)
    $UsedSpaceGB = $TotalSpaceGB - $FreeSpaceGB
    $PercentFree = [math]::Round(($FreeSpaceGB / $TotalSpaceGB) * 100, 2)
    $Body += "Drive $($Disk.DeviceID): $UsedSpaceGB GB used, $FreeSpaceGB GB free ($PercentFree% free)`n"
}

# Convert body to string
$Body = $Body -join "`n"

# Send email
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer