Powershell – Getting a Drive Space Report from Powershell about a list of Servers

Powershell

The other day I built a little script that showed the free space on a computer in Powershell with the ability to email a warning if it crossed over a margin.

Now the nice thing with Powershell, with a MINOR change, I can now Query Active Directory for a list of computers, all or specific by OU or any other method I choose and have a system MONITOR those servers for free space.  And I don’t even need to touch the servers themselves.

So right now, let’s pretend I am a smart Admin and keep all my servers in their own private OU since I control my Server updates manually and they have their own configuration TOTALLY different from my workstations.   I’ve put them under the OU Computers/Servers.  This script also requires Powershell V2 if you wish to use the “SEND-MAILMESSAGE” option

—————- Monitor Remote C Drive ——————–

# Get a list of computers from Active Directory

$Computerlist=GET-QADCOMPUTER –searchroot ‘Contoso.local/Computers/Servers’

Foreach ($Server in $Computerlist) {

# Change C: to whichever letter you’d like monitored

$driveinfo=get-wmiobject win32-volume –computername $Server.name | where { $-.driveletter -eq ‘C:’ } | select-object freespace, capacity, drivetype, driveletter

# 10 percent 10/100

$Percent=.10

# XXX % of maximum space is our warning level

$WarningLevel=$driveinfo.capacity *  $Percent

if ($driveinfo.freespace -lt $WarningLevel)

{

$Free=$driveinfo.freespace
$Servername=$Server.name

$Emailfrom="$Servername <$Servername@contoso.local>"
$EmailSubject="$Servername is running low on space"
$Emailbody="Hello Mr. Admin.  This is Server $Servername…I have $free bytes leftover."

send-mailmessage -from $Emailfrom -to "NetworkAdmin <networkadmin@contoso.local>" -subject $Emailsubject -body $Emailbody -priority High -dno onSuccess, onFailure -smtpServer smtp.contoso.local

}

}

—————- Monitor Remote C Drive ——————–

There wasn’t that easy?  Mind you it’s only monitoring the C: drive and next time I’ll look at a script that monitors all the drives.  It’s not as robust as SCOM but it has one thing SCOM doesn’t

It’s FREE.

Seize Powershell and Seize the Day!

Sean
the Energized Tech

Leave a Reply