Skip to content

Geekphreek

Geeks, not nerds

PowerShell script to email disk spaces (including HA Clusters)

Posted on May 1, 2015March 27, 2017 By Geekphreek No Comments on 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 = "administrator@MyGroovyCompany.com"
$smtpfrom = "ThatAmazinServer@MyGroovyCompany.com"
$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.

Hyper-V, Windows

Post navigation

Previous Post: Forced Closed Captions (CC) on for YouTube video
Next Post: Funky icons required?

Related Posts

Distribute fonts using GPO and a share Windows
Windows Cannot Find The File Specified Windows delete folder/file/directory with trailing white space Windows
Use Exchange Calendar and Contacts with Cortana Exchange
Office 365 and onmicrosoft as primary SMTP Office 365
Powershell: Email event logs to X Powershell
Windows 10 dreaded “This does not appear to be a valid font” Windows

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Monterey and VirtualBox
  • Auto-encode library with FFmpeg
  • Exchange Upgrade CU23 Fail with Cert Error
  • Changing image sizes in place in bulk
  • Download Wistia Embedded Video

Recent Comments

  • sha0z13 on EditShare Project Backup automation
  • Ilek82 on EditShare Project Backup automation
  • celewin on EditShare Project Backup automation
  • Geekphreek on EditShare Project Backup automation
  • LeoLick on EditShare Project Backup automation

Archives

Copyright © 2025 Geekphreek.

Powered by PressBook Grid Dark theme