Saturday 10 September 2011

Automated Disk Usage Reports With Powershell

Just a quick one today, a simple script to produce a nicely formatted email showing disk space usage.

When I set out to do this, I assumed it would be easy and expected to find hundreds of examples, but couldn't find any that did what I wanted.

This script uses Powershell and du.exe from Sysinternals (now part of Microsoft).


# uses du.exe from sysinternals
# http://technet.microsoft.com/en-us/sysinternals/bb896651.aspx

$dir="C:\users"
$smtpserver = "mail.example.local"
$sender = "sender@example.local"
$server = "file.example.local"
$recipient = "recipient@example.local"

$du=C:\utilities\du.exe -accepteula -l 1 $dir | sort -desc

$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$msg = new-object Net.Mail.MailMessage

$msg.From = $sender
$msg.To.Add($recipient)
$msg.Subject = "$server Disk Usage Report"
$msg.Body = "<p>$server Disk Usage Report in KB</p>"

$msg.Body += "<table>"
$du[4 .. $du.length] | forEach-Object {
$fields = $_.split(" ",[StringSplitOptions]::RemoveEmptyEntries)
$msg.Body += "<tr><td>$($fields[0]) </td><td>$($fields[1 .. $fields.length]) <td></tr>"
}
$msg.Body += "</table>"

$msg.IsBodyHTML = $true
$smtp.Send($msg)

5 comments:

  1. To use a path with blank for du try this:
    $du = & "C:\Program Files\Du\du.exe" -accepteula -l 3 -q $dir

    ReplyDelete
  2. Thanks Nathan, good catch. I should probably have put that in quotes anyway to make it easier for people using different paths.

    Thank you for taking the time to comment.

    ReplyDelete
  3. I'm way out of my depth here but what (if anything) do I enter for $server, Net.Mail.SmtpClient and Net.Mail.Message? I'm not completely useless as I realise $smtpserver is something like smtp.gmail.com. Just a little confused as I've never even heard of Powershell before! Forgive my ignorance please! Thanks and Merry Christmas.

    ReplyDelete
  4. Got the script working. Problem is I can't get it working with smtp.gmail.com. I think I need to authenticate and have tried $smtp.Credentials in a number of ways without success. HELP!

    ReplyDelete
  5. Hi Jason, as you are using GMail for your SMTP server it is not quite so straightforward as Google requires SSL and a different port to connect to.

    I've written some quick code below, but haven't tested it. I've crimped from some C# code here (Powershell uses .Net objects so try Googling for that if you can't find PowerShell specific examples): http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail - but hopefully this should help you out.

    Add the following lines under "$smtp = new-object Net.Mail.SmtpClient($smtpserver)"

    $smtp.Host = "smtp.gmail.com"
    $smtp.Port = 587
    $smtp.EnableSsl = true
    $smtp.DeliveryMethod = SmtpDeliveryMethod.Network
    $smtp.UseDefaultCredentials = false
    $smtp.Credentials = new-object Net.Mail.NetworkCredential(fromAddress.Address, fromPassword)

    (that last line might need to be Net.NetworkCredentials...)

    Merry Christmas

    ReplyDelete