duffney
(Duffney)
1
Description
This is a simple powershell script that runs against servers checking their drive space and then outputs the results to a text file and emails it to you. You will need to create a .txt file with the names of the computers you’d like to get reports from. Call the .txt file serverlist.txt to match the script.
Source Code
function getDiskFreeSpace
{
Get-WmiObject Win32_Volume -computername $args -filter "drivetype = 3" |
Select-Object __SERVER, Name, @{Name="Size(GB)";Expression={"{0:N1}" -f($_.Capacity/1gb)}},@{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}},@{Name="FreeSpacePerCent";Expression={"{0:P0}" -f($_.freespace/$_.capacity)}} |
Where-Object -FilterScript {$_.FreeSpacePerCent -lt 95} |
Sort-Object -property "FreeSpacePerCent" |
Format-Table
}
ForEach($s in Get-Content c:\serverlist.txt)
{
getDiskFreeSpace $s | Out-file c:\diskFreeSpaceResults.txt -append
}
Send-MailMessage -to "Destination Email Address" -from "Source Email Address" -subject "Servers Disk Free Space Report" -Attachment "C:\diskFreeSpaceResults.txt" -SmtpServer "Mail Server Name w\ Full domain name"
Clear-Content c:\diskFreeSpaceResults.txt
Screenshots

4 Spice ups
chrisseiter
(Chris Seiter (LBFF))
2
Huzzah! It’s always a good feeling when you get one done that’s for public consumption.
Great little script. I checked it out and it works as advertised. Only thing I noticed that I would love to see changed. I ran the script a few times and noticed that it appends the diskfreespaceresults.txt file rather than overwrites it. I would love to see it overwritten personally or have it delete the file after it emails it, either way. Just my 2 cents. Other that that it works great.
Works like a charm! Good job, bro. I like the email addition, personally.
duffney
(Duffney)
5
Justin005, Thanks for pointing that out, I actually left out a line that removes the contents after the email is sent, to prevent it from holding on to old data. Deleting the file would work as well, I’m sure. 
its not working for me. how to run this explain me
onecogmind
(onecogmind)
7
Thanks for that! I didn’t want it as an attachment so I updated it to do this on the Send-MailMessage: -body $body Based on this: $body= (Get-Content “C:\diskFreeSpaceResults.txt” | out-string) That puts the results in the body of the email instead of an attachment. I’m going to work on the formatting next as its slightly out of alignment.