PowerShell script to email disk spaces (including HA Clusters)


Every now and again we don’t rely on the tools that companies such as ManageEngine & SpiceWorks force down our throats and just go back to good old fashioned scripts… that email themselves with cool data in them.

Have you ever needed to have all your servers email you there current space? How about your cluster? How about in the same report? Well, if you’re clever enough and already done it then you can go back to picking your nose and eyeing up your bosses secretary (that is such a sexy outfit today, eh?).

Here ya go folks; bastardise this to your hearts discontent:

# Email disk space to the IT Gods of Peace
#
# Make sure you create a credential file by doing a "read-host -assecurestring | convertfrom-securestring | out-file C:\scripts\crendentials.txt"
# so you can run this script automated.

# Pull in the failover cluster modules
import-module failoverclusters

$smtpaddress = "[email protected]"
$smtpfrom = "[email protected]"
$smtpserver = "10.12.13.11"
$username = "MyGroovyCompany\administrator"
$password = cat C:\scripts\credentials.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$diskfile = "c:\scripts\disks.txt"

# Add new server names or IPs to $s line below
$s = "ServerName1", "ServerName2", "ServerName3"
$ClusterName = "MyClusterName"

# Local server needs to be accessed without credentials so I had to use this line
get-wmiobject win32_logicaldisk -Filter "Drivetype=3" |
ft SystemName,DeviceID,VolumeName,@{Label="Total Size";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize |
out-file $diskfile -encoding ASCII -width 120

# Do the other servers, hard baby, yeah!
Foreach ( $server in $s ) {
get-wmiobject win32_logicaldisk -Credential $cred -ComputerName $server -Filter "Drivetype=3" |
ft SystemName,DeviceID,VolumeName,@{Label="Total Size";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize |
out-file $diskfile -append -encoding ASCII -width 120
}

# Do the Cluster, nice and slow... yeah, it loves that...
$objs = @()

$csvs = Get-ClusterSharedVolume -Cluster $ClusterName
foreach ( $csv in $csvs )
{
$csvinfos = $csv | select -Property Name -ExpandProperty SharedVolumeInfo
foreach ( $csvinfo in $csvinfos )
{
$obj = New-Object PSObject -Property @{
Name = $csv.Name
Path = $csvinfo.FriendlyVolumeName
Size = $csvinfo.Partition.Size
FreeSpace = $csvinfo.Partition.FreeSpace
UsedSpace = $csvinfo.Partition.UsedSpace
PercentFree = $csvinfo.Partition.PercentFree
}
if ($obj.PercentFree -le $SpaceThreshold) {$alert++}

$objs += $obj
}
}
Write-Host "Cluster: " $ClusterName
$objs | ft -auto Name,Path,@{ Label = "Size(GB)" ; Expression = { "{0:N2}" -f ($_.Size/1024/1024/1024) } },@{ Label = "FreeSpace(GB)" ; Expression = { "{0:N2}" -f ($_.FreeSpace/1024/1024/1024) } },@{ Label = "UsedSpace(GB)" ; Expression = { "{0:N2}" -f ($_.UsedSpace/1024/1024/1024) } },@{ Label = "PercentFree" ; Expression = { "{0:N2}" -f ($_.PercentFree) } } |
out-file $diskfile -append -encoding ASCII -width 120

# Email the output file
Send-MailMessage -To $smtpaddress -From $smtpfrom -Subject "Servers Disk Space" -Attachments $diskfile -SmtpServer $smtpserver -priority High -dno onSuccess, onFailure

Ah, so satisfying! Go impress your bosses with this bad code that works great! Shout out to MSDN folks and The Scripting Guy for the code I stole before cutting it to little pieces.

Leave a comment