Description

Not sure if someone already added something like this.

But i took and prebuilt script and tweaked some of the setting and added in a RAM section.
Tried adding in CPU usage as well, but dont think thats really that usefull.

Change/create the following:

  • Change recipient
  • create report path folders
  • create txt file with list of server names
  • change SMTP setting at bottom of script.
  • save as .ps1
  • run manually: powershell.exe -noexit c:\scripts\diskreport.ps1
  • run auto, add a schedules task.

Source Code

# Continue even if there are errors 
$ErrorActionPreference = "Continue"; 
 
# Set your warning and critical thresholds 
$percentWarning = 100; 
$percentCritcal = 15; 
 
# EMAIL PROPERTIES 
 # Set the recipients of the report. 
  $users = "me@spiceworks.com" 
 
# REPORT PROPERTIES 
 # Path to the report 
  $reportPath = "\\SERVER\DRIVE\Scripts\Jobs\DiskSpaceQuery\Reports\"; 
 
 # Report name 
  $reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html"; 
 
# Path and Report name together 
$diskReport = $reportPath + $reportName 
 
#Set colors for table cell backgrounds 
$redColor = "#FF0000" 
$orangeColor = "#FBB917" 
$whiteColor = "#FFFFFF" 
 
# Count if any computers have low disk space.  Do not send report if less than 1. 
$i = 0; 
 
# Get computer list to check disk space 
$computers = Get-Content "\\SERVER\DRIVE\Scripts\servers.txt"; 
$datetime = Get-Date -Format "MM-dd-yyyy_HHmmss"; 
 
# Remove the report if it has already been run today so it does not append to the existing report 
If (Test-Path $diskReport) 
    { 
        Remove-Item $diskReport 
    } 
 
# Cleanup old files.. 
$Daysback = "-7" 
$CurrentDate = Get-Date; 
$DateToDelete = $CurrentDate.AddDays($Daysback); 
Get-ChildItem $reportPath | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item; 
 
# Create and write HTML Header of report 
$titleDate = get-date -uformat "%m-%d-%Y - %A" 
$header = " 
  <html> 
  <head> 
  <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'> 
  <title>DiskSpace Report</title> 
  <STYLE TYPE='text/css'> 
  <!-- 
  td { 
   font-family: Calibri; 
   font-size: 12px; 
   border-top: 1px solid #999999; 
   border-right: 1px solid #999999; 
   border-bottom: 1px solid #999999; 
   border-left: 1px solid #999999; 
   padding-top: 0px; 
   padding-right: 0px; 
   padding-bottom: 0px; 
   padding-left: 0px; 
  } 
  body { 
   margin-left: 5px; 
   margin-top: 5px; 
   margin-right: 0px; 
   margin-bottom: 10px; 
   table { 
   border: thin solid #000000; 
  } 
  --> 
  </style> 
  </head> 
  <body> 
  <table width='100%'> 
  <tr bgcolor='#548DD4'> 
  <td colspan='7' height='30' align='center'> 
  <font face='calibri' color='#003399' size='4'><strong>Daily Morning Report for $titledate</strong></font> 
  </td> 
  </tr> 
  </table> 
" 
 Add-Content $diskReport $header 
 
# Create and write Table header for report 
 $tableHeader = " 
 <table width='100%'><tbody> 
 <tr bgcolor=#548DD4> 
 <td width='10%' align='center'>Server</td> 
 <td width='5%'  align='center'>Drive</td> 
 <td width='15%' align='center'>Drive Label</td> 
 <td width='10%' align='center'>Total Capacity(GB)</td> 
 <td width='10%' align='center'>Used Capacity(GB)</td> 
 <td width='10%' align='center'>Free Space(GB)</td> 
 <td width='5%'  align='center'>Freespace %</td> 
 <td width='5%'  align='center'>RAM %</td>
  <td width='5%'  align='center'>CPU %</td>
 </tr> 
" 
Add-Content $diskReport $tableHeader 
  
# Start processing disk space 
  foreach($computer in $computers) 
 {  
 $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3" 
 $computer = $computer.toupper() 
  foreach($disk in $disks) 
 {         
  $deviceID = $disk.DeviceID; 
        $volName = $disk.VolumeName; 
  [float]$size = $disk.Size; 
  [float]$freespace = $disk.FreeSpace;  
  $percentFree = [Math]::Round(($freespace / $size) * 100); 
  $sizeGB = [Math]::Round($size / 1073741824, 2); 
  $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2); 
        $usedSpaceGB = $sizeGB - $freeSpaceGB; 
        $color = $whiteColor; 
# Start processing RAM 		
  $RAM = Get-WmiObject -ComputerName $computer -Class Win32_OperatingSystem
	$RAMtotal = $RAM.TotalVisibleMemorySize;
	$RAMAvail = $RAM.FreePhysicalMemory;
		$RAMpercent = [Math]::Round(($RAMavail / $RAMTotal) * 100);
		
# Set background color to Orange if just a warning 
 if($percentFree -lt $percentWarning)       
  { 
    $color = $orangeColor  
 
# Set background color to Orange if space is Critical 
      if($percentFree -lt $percentCritcal) 
        { 
        $color = $redColor 
       }         
  
 # Create table data rows  
    $dataRow = " 
  <tr> 
        <td width='10%'>$computer</td> 
  <td width='5%' align='center'>$deviceID</td> 
  <td width='10%' >$volName</td> 
  <td width='10%' align='center'>$sizeGB</td> 
  <td width='10%' align='center'>$usedSpaceGB</td> 
  <td width='10%' align='center'>$freeSpaceGB</td> 
  <td width='5%' bgcolor=`'$color`' align='center'>$percentFree</td> 
  <td width='5%' align='center'>$RAMpercent</td>
  <td width='5%' align='center'>$CPUpercent</td>
  </tr> 
" 
Add-Content $diskReport $dataRow; 
Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree"; 
    $i++   
  } 
 } 
} 

# Create table at end of report showing legend of colors for the critical and warning 
 $tableDescription = " 
 </table><br><table width='20%'> 
 <tr bgcolor='White'> 
    <td width='10%' align='center' bgcolor='#FBB917'>No Warning</td> 
 <td width='10%' align='center' bgcolor='#FF0000'>Critical less than 10% free space</td> 
 </tr> 
" 
 Add-Content $diskReport $tableDescription 
 Add-Content $diskReport "</body></html>" 
 
# Send Notification if alert $i is greater then 0 
if ($i -gt 0) 
{ 
    foreach ($user in $users) 
{ 
        Write-Host "Sending Email notification to $user" 
   
  $smtpServer = "SERVER.DOMAINl" 
  $smtp = New-Object Net.Mail.SmtpClient($smtpServer) 
  $msg = New-Object Net.Mail.MailMessage 
  $msg.To.Add($user) 
        $msg.From = "Me@Spiceworks.com" 
  $msg.Subject = "DiskSpace Report for $titledate" 
        $msg.IsBodyHTML = $true 
        $msg.Body = get-content $diskReport 
  $smtp.Send($msg) 
        $body = "" 
    } 
  } 

Screenshots

14 Spice ups

Very nice! It is working perfect, I would like know if you have new version as add CPU usage on report?

If you want to include CPU in the script, add this below the RAM properties: # Start processing CPU $CPUpercent = Get-WmiObject -ComputerName $computer -Class win32_processor | Measure-Object -property LoadPercentage -Average | Select-Object -ExpandProperty Average Should be good to go from there.

Perfect thanks! I added something similar in the script a while ago, didnt work as well though :stuck_out_tongue:

I altered it to below, this also checks all CPU’s on he machine individually. # Start processing CPU $CPU = Get-WmiObject -ComputerName $computer -Class Win32_Processor $CPUcurrent = $CPU.CurrentClockSpeed; $CPUmax = $CPU.maxclockspeed; $CPUPercent = $CPU.LoadPercentage;

hello sir, im having RPC issues with multiple ip address from servers.txt any suggestion thanks in advance

Do you get a response at all? Added the servername into the txt file?

Conor, Great PS script man. Thanks for this. Keep up the good work.

Thanks for sharing this awesome script! @Connor I tried your method for CPU but it generates output like “16 0 7 29”. Does it mean its reporting the CPU usage on all 4 CPUs on the server? kimberly’s method seems to generate the desired output (I think).

It does, if your server has multiple CPU’s it will report all of them separately.

Is it possible to add a code to list top 5 processes currently using the most CPU and Ram in above PowerShell?

I would say it def possible, but this currently just reads the server names in order of the txt file it grabs them from.

Thx Conor, Do you have Powershell to get Top 5 application CPU and Memory Usage and the result show as below? Top 3 Running Processes by Memory Name User Name CPU Usage Memory Usage sqlServr SYSTEM 10% 849MB TrustedInstaller SYSTEM 6% 485MB Service.exe domain\user 49% 459MB Top 3 Running Processes by CPU Name User Name CPU Usage Memory Usage firefox.exe domain\user 49% 459MB sqlServr SYSTEM 10% 849MB TrustedInstaller SYSTEM 6% 485MB

I dont, but i can add it on to the list of projects.

Awesome! And it made me look better to staff and management. Could not ask for more. Thanks Conor. :slight_smile:

Script is very handy… how to monitor mount points ? Thanks.

You may need to add another column for Mount, and filter on “DriveType” which runs through all the HDDs

Could someone please tell me how the output is coming in the form of a table as shown in the screenshot? I ran the script and was able to see just a normal output in the power shell window. Please help.

You either run the task manually as it says above or as a scheduled task. This will send an Email to your chosen address: Have you added the folder path? SMTP settings? email address? etc?

Thanks Conor. This script saved lot more time of us. Is it possible to get the total RAM Size also along with the RAM Usage. So that we can compare it effectively. Looking forward for your updated script.