PowerShell Script to gather information from Remote server and email it.

Here is a PowerShell script that gathers CPU utilization, memory usage, disk space, and uptime information from a remote server, formats it into an HTML table, and sends it via email to IT Support. Make sure to run this script with appropriate permissions and update the placeholders with your specific details.
# Define variables $RemoteServer = "RemoteServerName" # Replace with the remote server name or IP $EmailFrom = "your-email@example.com" $EmailTo = "itsupport@example.com" $SMTPServer = "smtp.example.com" $SMTPPort = 587 $SMTPUsername = "your-email@example.com" $SMTPPassword = "YourPassword" # Gather system information $CPU = Get-WmiObject -ComputerName $RemoteServer -Class Win32_Processor | Select-Object -ExpandProperty LoadPercentage $Memory = Get-WmiObject -ComputerName $RemoteServer -Class Win32_OperatingSystem | ForEach-Object { [PSCustomObject]@{ TotalMemory = [math]::Round($_.TotalVisibleMemorySize / 1MB, 2) FreeMemory = [math]::Round($_.FreePhysicalMemory / 1MB, 2) UsedMemory = [math]::Round(($_.TotalVisibleMemorySize - $_.FreePhysicalMemory) / 1MB, 2) } } $DiskSpace = Get-WmiObject -ComputerName $RemoteServer -Class Win32_LogicalDisk -Filter "DriveType=3" | ForEach-Object { [PSCustomObject]@{ Drive = $_.DeviceID FreeSpace = [math]::Round($_.FreeSpace / 1GB, 2) TotalSpace = [math]::Round($_.Size / 1GB, 2) UsedSpace = [math]::Round(($_.Size - $_.FreeSpace) / 1GB, 2) } } $Uptime = (Get-WmiObject -ComputerName $RemoteServer -Class Win32_OperatingSystem).LastBootUpTime $UptimeFormatted = (Get-Date) - ([Management.ManagementDateTimeConverter]::ToDateTime($Uptime)) # Create HTML report $HTML = @" <html> <head> <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid black; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } </style> </head> <body> <h2>Server Monitoring Report - $RemoteServer</h2> <h3>CPU Utilization</h3> <p>CPU Load: $CPU%</p> <h3>Memory Utilization</h3> <table> <tr><th>Total Memory (GB)</th><th>Used Memory (GB)</th><th>Free Memory (GB)</th></tr> <tr><td>$($Memory.TotalMemory)</td><td>$($Memory.UsedMemory)</td><td>$($Memory.FreeMemory)</td></tr> </table> <h3>Disk Space</h3> <table> <tr><th>Drive</th><th>Total Space (GB)</th><th>Used Space (GB)</th><th>Free Space (GB)</th></tr> "@ foreach ($Disk in $DiskSpace) { $HTML += "<tr><td>$($Disk.Drive)</td><td>$($Disk.TotalSpace)</td><td>$($Disk.UsedSpace)</td><td>$($Disk.FreeSpace)</td></tr>" } $HTML += @" </table> <h3>Uptime</h3> <p>Last Boot Time: $UptimeFormatted</p> </body> </html> "@ # Send email Send-MailMessage -From $EmailFrom -To $EmailTo -Subject "Server Monitoring Report - $RemoteServer" ` -BodyAsHtml -Body $HTML -SmtpServer $SMTPServer -Port $SMTPPort ` -Credential (New-Object PSCredential $SMTPUsername, (ConvertTo-SecureString $SMTPPassword -AsPlainText -Force))
Instructions:
- Replace placeholders like
RemoteServerName
,your-email@example.com
,smtp.example.com
, etc., with your actual server and email details. - Ensure the SMTP server supports authentication and the credentials are valid.
- Run the script with administrative privileges.
- Install any required PowerShell modules if prompted.
This script will generate an HTML report and email it to the specified recipient