Powershell script to find freespace available on disk and send email

Avyayah 1,271 Reputation points
2024-05-16T19:51:18.0666667+00:00

Here is the code and I am getting email The F: drive is below 150 GB and only at 0 GB - Please resolve, but that is not true. F drive is not low in space

$disksize = $null

$limitGB = $null

$freeSpaceGB = $null

Parameter

$limitGB = 150

$disksize = Get-WmiObject -ComputerName servername-SharepointDB -Class Win32_LogicalDisk | Where-object Name -eq "F:"

$freeSpaceGB = [math]::round($disksize.FreeSpace/1GB, 2)

If ($freeSpaceGB -lt $limitGB) {

$options = @{

'SmtpServer' = "server.smtp.com" 

'To' = "Recipient"

'From' = "sender" 

'Subject' = "F: Drive space threshold reached - $freeSpaceGB GB Free" 

'Body' = "The F: drive is below 150 GB and only at $freeSpaceGB GB - Please resolve" 

 }

Send-MailMessage @options

}

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,216 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Andreas Baumgarten 99,371 Reputation points MVP
    2024-05-16T20:08:34.01+00:00

    Hi @Avyayah ,

    is there an "F:-Drive"?

    Please check the value of $disksize in your script if there is a "F:-Drive" found:

    $disksize = $null
    $limitGB = 150
    $disksize = Get-WmiObject -ComputerName localhost -Class Win32_LogicalDisk  | Where-object Name -eq "F:"
    if ($disksize) {
        $freeSpaceGB = [math]::round($disksize.FreeSpace/1GB, 2)
        if ($freeSpaceGB -lt $limitGB) {
            Write-Output "running out of space - $freeSpaceGB GB free"
        } else {
            Write-Output "enough space - $freeSpaceGB GB free "
        }
    } else {
        Write-Output "No F: drive found "
    }
    

    You can run this to get the disks and check which drives are found:

    Get-WmiObject -ComputerName localhost -Class Win32_LogicalDisk
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten


  2. Avyayah 1,271 Reputation points
    2024-05-17T21:16:12.6+00:00

    With this code it execute email that 702 GB left. But I guess when disk space is down it should alert me

    0 comments No comments

  3. Andreas Baumgarten 99,371 Reputation points MVP
    2024-05-17T21:37:09.58+00:00

    Hi @Avyayah ,

    not tested but it should help (you can increase the limitGB value for testing):

    $disksize = $null
    $limitGB = 150
    $disksize = Get-WmiObject -ComputerName localhost -Class Win32_LogicalDisk  | Where-object Name -eq "F:"
    if ($disksize) {
        $freeSpaceGB = [math]::round($disksize.FreeSpace/1GB, 2)
        if ($freeSpaceGB -lt $limitGB) {
            Write-Output "running out of space - $freeSpaceGB GB free"
            
            $sendMailMessage = @{
                From = 'User01 <user01@fabrikam.com>'
                To = 'User02 <user02@fabrikam.com>'
                Subject = 'F: Drive space threshold reached - $freeSpaceGB GB Free'
                Body = "The F: drive is below 150 GB and only at $freeSpaceGB GB - Please resolve"
                SmtpServer = 'server.smtp.com'
            }
            Send-MailMessage @sendMailMessage
    
        } else {
            Write-Output "enough space - $freeSpaceGB GB free "
        }
    } else {
        Write-Output "No F: drive found "
    }
    
    
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten

    0 comments No comments

  4. Plamen Peev 80 Reputation points
    2024-05-20T10:48:28.53+00:00

    Hi @Avyayah , try this:

    $Drive = "C:"
    $FreeSpace = (Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='$Drive'").FreeSpace /1gb -as [int]
    $Limit = 10
    if ($FreeSpace -lt $Limit) {
    
        $EmailFrom = "youremail@server.com"
        $EmailTo = "youremail@server.com"
        $Creds = Get-Credential -UserName $EmailFrom
    
        $Subject = “Your subject text here”
        $Body = "Your drive has {0}GB of free space left" -f $FreeSpace
    
        $SMTPServer = “smtp.yourserver.com”
        $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
        $SMTPClient.EnableSsl = $true
        $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Creds.UserName, $Creds.Password)
        $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
        $SMTPClient.Dispose()
    }
    

    You can also implement try-catch on the $SMTPClient.Send line if you would like to check whether the email has been sent. Tested with smtp.outlook.com.

    0 comments No comments