PowerShell script to get all the Windows server drive report in a email

 Here is the updated script.





<#
.SYNOPSIS
Collects drive space details from a list of Windows servers and emails an HTML report.

.NOTES
- Requires WinRM/CIM access to target servers (Enable-PSRemoting on targets, or WMI/DCOM open via firewall).
- Run under an account with admin rights on target servers, or supply -Credential.
- Works on Windows PowerShell 5.1 (built-in Send-MailMessage). For PS7, see notes at bottom.
#>

# ---- CONFIG ----

# Read server names from an external text file
$serverListPath = "C:\Scripts\servers.txt"

$servers = Get-Content $serverListPath |
           Where-Object { $_.Trim() -ne "" }

$smtpServer    = "smtp.yourcompany.com"
$from          = "monitoring@yourcompany.com"
$to            = "you@yourcompany.com"
$subject       = "Windows Servers - Drive Space Report - $(Get-Date -Format 'yyyy-MM-dd')"
$freeThreshold = 20   # percent free below which status = Check Now


# ---- COLLECT DATA ----

$results = foreach ($server in $servers) {
    try {
        $disks = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $server -Filter "DriveType=3" -ErrorAction Stop

        foreach ($disk in $disks) {
            $sizeGB    = [math]::Round($disk.Size / 1GB, 2)
            $freeGB    = [math]::Round($disk.FreeSpace / 1GB, 2)
            $usedGB    = [math]::Round($sizeGB - $freeGB, 2)
            $pctFree   = if ($sizeGB -gt 0) { [math]::Round(($freeGB / $sizeGB) * 100, 1) } else { 0 }
            $status    = if ($pctFree -lt $freeThreshold) { "Check Now" } else { "Healthy" }

            [PSCustomObject]@{
                Server      = $server
                DeviceID    = $disk.DeviceID
                SizeGB      = $sizeGB
                UsedGB      = $usedGB
                FreeGB      = $freeGB
                PercentFree = $pctFree
                Status      = $status
            }
        }
    }
    catch {
        [PSCustomObject]@{
            Server      = $server
            DeviceID    = "-"
            SizeGB      = $null
            UsedGB      = $null
            FreeGB      = $null
            PercentFree = $null
            Status      = "Server not reachable"
        }
    }
}


# ---- BUILD HTML OUTPUT WITH INLINE STYLES ----

$rows = foreach ($r in $results) {

    $statusStyle = switch ($r.Status) {
        "Healthy"              { "color: #2e7d32; font-weight: bold;" }
        "Check Now"            { "color: #c62828; font-weight: bold;" }
        "Server not reachable" { "color: #c62828; font-weight: bold; background-color: #ffebee;" }
        default                { "" }
    }

    "<tr>
        <td style='border: 1px solid #e0e0e0; padding: 8px 12px; font-size: 13px; color: #333333;'>$($r.Server)</td>
        <td style='border: 1px solid #e0e0e0; padding: 8px 12px; font-size: 13px; color: #333333;'>$($r.DeviceID)</td>
        <td style='border: 1px solid #e0e0e0; padding: 8px 12px; font-size: 13px; color: #333333;'>$($r.SizeGB)</td>
        <td style='border: 1px solid #e0e0e0; padding: 8px 12px; font-size: 13px; color: #333333;'>$($r.UsedGB)</td>
        <td style='border: 1px solid #e0e0e0; padding: 8px 12px; font-size: 13px; color: #333333;'>$($r.FreeGB)</td>
        <td style='border: 1px solid #e0e0e0; padding: 8px 12px; font-size: 13px; color: #333333;'>$($r.PercentFree)</td>
        <td style='border: 1px solid #e0e0e0; padding: 8px 12px; font-size: 13px; $statusStyle'>$($r.Status)</td>
    </tr>"
}

$html = @"
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body style="margin: 0; padding: 20px; background-color: #f4f6f8; font-family: 'Segoe UI', Arial, sans-serif;">

    <!-- Main Container Table to center all sections with 700px width -->
    <table align="center" width="700" border="0" cellpadding="0" cellspacing="0" style="width: 700px; margin: 0 auto;">
        
        <!-- HEADER SECTION -->
        <tr>
            <td style="background-color: #2c3e50; border-radius: 8px; padding: 16px 20px; text-align: center;">
                <h2 style="color: #ffffff; margin: 0; font-size: 18px; font-weight: 600;">
                    Windows Server Drive Space Report - $(Get-Date -Format 'yyyy-MM-dd')
                </h2>
            </td>
        </tr>

        <!-- SPACER -->
        <tr><td height="16" style="height: 16px;"></td></tr>

        <!-- RESULTS TABLE SECTION -->
        <tr>
            <td align="center">
                <table width="100%" border="0" cellpadding="0" cellspacing="0" style="width: 100%; border-collapse: collapse; background-color: #ffffff; border-radius: 8px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
                    <thead>
                        <tr style="background-color: #34495e; color: #ffffff;">
                            <th style="padding: 10px 12px; text-align: left; font-size: 12px; font-weight: 600;">Server</th> 
                            <th style="padding: 10px 12px; text-align: left; font-size: 12px; font-weight: 600;">DeviceID</th> 
                            <th style="padding: 10px 12px; text-align: left; font-size: 12px; font-weight: 600;">Size (GB)</th> 
                            <th style="padding: 10px 12px; text-align: left; font-size: 12px; font-weight: 600;">Used (GB)</th> 
                            <th style="padding: 10px 12px; text-align: left; font-size: 12px; font-weight: 600;">Free (GB)</th> 
                            <th style="padding: 10px 12px; text-align: left; font-size: 12px; font-weight: 600;">% Free</th> 
                            <th style="padding: 10px 12px; text-align: left; font-size: 12px; font-weight: 600;">Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        $($rows -join "`n")
                    </tbody>
                </table>
            </td>
        </tr>

        <!-- SPACER -->
        <tr><td height="16" style="height: 16px;"></td></tr>

        <!-- SIGNATURE SECTION -->
        <tr>
            <td style="background-color: #2c3e50; border-radius: 8px; padding: 14px 20px; text-align: left;">
                <p style="color: #ecf0f1; margin: 0; font-size: 13px; line-height: 1.5;">
                    Regards,<br>
                    <strong style="color: #ffffff; font-size: 14px;">SQL DBA Team</strong><br>
                    <span style="color: #bdc3c7;">Database Administration</span>
                </p>
            </td>
        </tr>

    </table>

</body>
</html>
"@


# ---- SEND EMAIL ----

Send-MailMessage -From $from -To $to -Subject $subject -Body $html -BodyAsHtml -SmtpServer $smtpServer


Labels: ,
This is the most recent post.
Older Post

Post a Comment

[blogger]

Author Name

Contact Form

Name

Email *

Message *

Powered by Blogger.