The Multifunctioning DBA

Oct 22 2008   10:11PM GMT

Powershell Script to Monitor Disk Space

Colin Smith Colin Smith Profile: Colin Smith

So today I got alerted by one of my old VBScripts about a disk having less than 5 Gigs free. I had this script in place so that the SQL servers that I manage do not run out of space on the disk that the mdf and ldf files are on. When I logged into see what the problem was I noticed that on both servers the system drive was out of space. I went to the Windows team and let them know and they asked if I could put a script together to notify them if the situation happened again. So I thought I am sure I can and this time I am going to do it in Powershell and make the script better then my VBScript. I got a list of the servers that they wanted me to monitor for them and then I started scripting and here is what I did.

##########################################

###        Gather Disk Information     ###

##########################################

Clear-Content “D:\Scripts\Powershell\PAC\lowdisk.txt”

$i = 0

$users = “email@email.com”, ” email@email.com ”

$computers = Get-Content “D:\Scripts\Powershell\PAC\computerlistall.txt”

echo “ServerName        Drive Letter    Drive Size    Free Space    Percent Free” >> “D:\Scripts\Powershell\PAC\lowdisk.txt”

echo “———-        ————    ———-    ———-    ————” >> “D:\Scripts\Powershell\PAC\lowdisk.txt”

foreach ($computer in $computers)

{

    $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}

    foreach($drive in $drives)

{

        $size1 = $drive.size / 1GB

        $size = “{0:N2}” -f $size1

        $free1 = $drive.freespace / 1GB

        $free = “{0:N2}” -f $free1

        $ID = $drive.DeviceID

        $a = $free1 / $size1 * 100

        $b = “{0:N2}” -f $a

##############################################

##    Determine if any disks low    ##

##############################################

        if (($ID -eq “D:”) -or ($ID -eq “S:”) -or ($ID -eq “T:”) -or ($ID -eq “C:”) -and ($free1 -lt 1))

        {

            echo “$computer        $ID            $size        $free        $b” >> “D:\Scripts\Powershell\PAC\lowdisk.txt”

            $i++

        }

    }

}

####################################################

##    Send Notification if alert $i is greater then 0         ##

####################################################

if ($i -gt 0)

{

    foreach ($user in $users)

{

        echo “Sending Email notification ro $user”

        $smtpServer = “smtp server”

        $smtp = New-Object Net.Mail.SmtpClient($smtpServer)

        $emailFrom = “fromuser@domain.com”

       $subject = “Email Subject”

        foreach ($line in Get-Content “D:\Scripts\Powershell\PAC\lowdisk.txt”)

{

            $body += “$line `n”

        }

        $smtp.Send($EmailFrom,$user,$subject,$body)

        $body = “”

    }

}

So if we break this down a bit here is essentially what I am doing. First I read in the servers to monitor from the file computerlistall.txt and then I give the scripts the names of the administrators that want to get the notifications. Then I set up my output file with some formatting. Now the good stuff, I make a WMI connection to the server and get a list of all the drives on the server. I then have to eliminate the CDROM and any other drive that is not actually a physical hard drive on the server. Then I get the disk size divide that by a gig and then I round that to two decimal places. Then I do the same for the free space. I am only rounding the disk free and size for the output. I am doing my conditional logic on the actual numbers to get the most accurate results possible. After I have all the variables then I do the conditional logic. I am saying that if it is any of the drive letters that I specified and the free space is less than 1Gig then output that to a file and increment my counter $i. So now I do a bit more logic and check the value of my counter. If $i is greater than 0 then I know that I have something that I need to send out a notification on. So I now send out the notifications as needed.

I set this up to run once a day but you can certainly do it more often. I set it up in the windows scheduler and it works just fine. Please let me know if you have any questions about this or need any help with it. You can also head over to my website at sysadminsmith.com to get help on pretty much anything.

1  Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • doron200
    Excellent post, however how do i run this on a network of 40 servers or so? thanks 
    10 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: