Forum Discussion

Fgarpol's avatar
Fgarpol
Copper Contributor
Apr 03, 2020

script to check free space on drives has a fixed value

Hi there,

 

I have a script that checks on my environment if any of the drives on any server is below 10GB of free space, and then fires and email accordingly.

the problem i am facing is that some servers have partitions that are actually just 10GB big so, basically every time they get checked an email is fired up.

 

I don`t know if the trigger criteria would be better to be a percentage instead of a fixed value, but i am not sure how to do it, or maybe set a different script for those servers holding those 10Gb size partitions.

 

I have attached a copy of the script, maybe somebody can help me out.

 

thank you

  • Hello , Fgarpol .

    My first option would be to ignore those drives with less than the Min Size. In the same way, you can add other filters and options depending on what you want to do with them. 

     

     

    param(
    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
    [string] $minSize = 10GB,
    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
      [string] $hosts = $null,
    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
     $volumes = $null,
    # - email_to : if specified, will send a low-disk-space warning email to the given colon-separated addresses.
    #              Example: $email_to = "my@email.com:your@email.com";
    #              Default is $null (no e-mail will be sent). Replace it with your@email.com if you don't want to set it from the CLI.
    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
    [string] $email_to = "support@mycompany.com",
     
    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
    $email_username = "administrador@mycompany.com",
    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
    $email_password = "#########",
    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
    $email_smtp_host = "smtp.office365.com",
    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
    $email_smtp_port = 587,
    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
    $email_smtp_SSL = 1,
    [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)]
    $email_from = "Monitor Admin <administrador@mycompany.com>"
    )
    
    $sep = ":";
    $hosts = Get-Content -Path c:\scripts\computers.txt
    
    # if there are no $cur_hosts set, set the local computer as host. 
    if (!$hosts) { $hosts = $env:computername; }
    
    foreach ($cur_host in $hosts.split($sep))
    {
        # converts IP to hostNames
        if (($cur_host -As [IPAddress]) -As [Bool]) {
            $cur_host = [System.Net.Dns]::GetHostEntry($cur_host).HostName
        }
    
    	Write-Host ("");
    	Write-Host ("");
    	Write-Host ("----------------------------------------------");
    	Write-Host ($cur_host);
    	Write-Host ("----------------------------------------------");
        $drives_to_check = @();
    
        if ($null -eq $volumes) {
       	    $volArr = 
              If ($cur_host -eq $env:computername) { Get-WMIObject win32_volume }
              Else { Invoke-Command -ComputerName $cur_host -ScriptBlock { Get-WMIObject win32_volume } }
    
    	    $drives_to_check = @();
            foreach ($vol in $volArr | Sort-Object -Property DriveLetter) {
    	        if ($vol.DriveType -eq 3 -And $null -ne $vol.DriveLetter  ) {
      		        $drives_to_check += $vol.DriveLetter[0];
    		    }
    	    }
        }
        Else { $drives_to_check = $volumes.split($sep) }
    
      foreach ($d in $drives_to_check) {
    	# Write-Host ("");
    	# Write-Host ("  Checking drive " + $d + " ...");
    	$disk = If ($cur_host -eq $env:computername) { Get-PSDrive $d }
        Else { Invoke-Command -ComputerName $cur_host -ScriptBlock { Get-PSDrive $using:d } }
    
    	if ($disk.Free -lt $minSize -and $($disk.Free + $Disk.Used) -gt $minSize) {
    		Write-Host "  - [" -noNewLine
            Write-Host "KO" -noNewLine -ForegroundColor Red
            Write-Host "] " -noNewLine
            Write-Host ("Drive " + $d + " has less than " + ($minSize/1GB).ToString(".00") `
    			+ " GB free (" + ($disk.Free/1GB).ToString(".00") + " GB)") -noNewLine
             
            if ($email_to) {
                Write-Host(": sending e-mail..."); 
    	
    		    $message = new-object Net.Mail.MailMessage;
    		    $message.From = $email_from;
    		    foreach ($to in $email_to.split($sep)) {
    			    $message.To.Add($to);
    		    }
    		    $message.Subject = 	("Alert: " + $cur_host + " drive " + $d);
    		    $message.Subject +=	(" has less than " + ($minSize/1GB).ToString(".00") + " GB free ");
    		    $message.Subject +=	(" (" + ($disk.Free/1GB).ToString(".00") +" GB)");
    		    $message.Body =		"Hello, `r`n`r`n";
    		    $message.Body += 	"this is an automatic e-mail message ";
    		    $message.Body += 	"sent by the Monitor Admin ";
    		    $message.Body += 	("to inform you that " + $env:computername + " drive " + $d + " ");
    		    $message.Body += 	"is running low on free space. `r`n`r`n";
    		    $message.Body += 	"--------------------------------------------------------------";
    		    $message.Body +=	"`r`n";
    		    $message.Body += 	("Machine HostName: " + $env:computername + " `r`n");
    		    $message.Body += 	"Machine IP Address(es): ";
    		    $ipAddresses = Get-NetIPAddress -AddressFamily IPv4;
    		    foreach ($ip in $ipAddresses) {
    		        if ($ip.IPAddress -like "127.0.0.1") {
    			        continue;
    			    }
    		        $message.Body += ($ip.IPAddress + " ");
    		    }
    		    $message.Body += 	"`r`n";
    		    $message.Body += 	("Used space on drive " + $d + ": " + ($disk.Used/1GB).ToString(".00") +" GB. `r`n");
    		    $message.Body += 	("Free space on drive " + $d + ": " + ($disk.Free/1GB).ToString(".00") +" GB. `r`n");
    		    $message.Body += 	"--------------------------------------------------------------";
    		    $message.Body +=	"`r`n`r`n";
    		    $message.Body += 	"This warning will fire when the free space is lower ";
    		    $message.Body +=	("than " + ($minSize/1GB).ToString(".00") + " GB `r`n`r`n");
    		    $message.Body += 	"Sincerely, `r`n`r`n";
    		    $message.Body += 	"-- `r`n";
    		    $message.Body += 	"Monitor Admin`r`n";
                      
    
    		    $smtp = new-object Net.Mail.SmtpClient($email_smtp_host, $email_smtp_port);
    		    $smtp.EnableSSL = $email_smtp_SSL;
    		    $smtp.Credentials = New-Object System.Net.NetworkCredential($email_username, $email_password);
    		    $smtp.send($message);
    		    $message.Dispose();
    		    Write-Host " E-Mail sent!" ; 
            }
            Else {
                Write-Host(".");
            }
    	}
       ElseIf($($disk.Free + $Disk.Used) -lt $minSize)
        {
         Write-Host "  - [" -noNewLine
         Write-Host "Warning" -noNewLine -ForegroundColor Yellow
         Write-Host "] " -noNewLine
    	 Write-Host ("Drive " + $d + " has less than " + ($minSize/1GB).ToString(".00") + " bytes Total Space: nothing to do.")
        }
    	Else {
    		Write-Host "  - [" -noNewLine
            Write-Host "OK" -noNewLine -ForegroundColor Green
            Write-Host "] " -noNewLine
    		Write-Host ("Drive " + $d + " has more than " + ($minSize/1GB).ToString(".00") + " bytes free: nothing to do.")
    	}
      }
    }

Resources