I was asked to write a script to monitor the health of the volumes for our web application farm. The original request was to monitor how much disk space was free on each volume, however I wanted the ability to monitor the integrity and fragmentation of the volume as well.
The first issue was we wanted to exclude the 100mb partition that Windows creates for the boot up process. This doesn't generally change and we didn't want it causing too much noise.
Checking the fragmentation status of a volume is available through WMI, through WMI you can easily ask Windows if that particular volume isfragment and get a True or False response.
File system integrity is a little different. NTFS has a dirty flag, which is used by check disk during boot to determine if it should perform a check on that volume. One annoying issue is there are two methods to check this flag, WMI or the CHKNTFS.EXE, WMI is considered to be more reliable, but there are many discussions on the Internet in regards to how reliable it is. CHKNTFS cannot run on a drive without a drive letter, so you need to be careful when scripting with it. In the end, my script will perform both tests where possible.
Using my usual template, the code below is for the body.
If you want to skip the 100mb system drives, set $thresholdexcludesystem to $true