Check disk space remotely with PowerShell

I work with a customer with a large VDI environment.  They had asked for a weekly report to generate with an overview of VDI disk free space.  I have created the script below to do just that.

Effectively it has three parameters (ie Get-DiskSpaceReport.ps1 – To userreceivingthereport@company.com – SMTPServer smtp.company.com -From monitoringserver@company.com).  It runs against the AD Computer filter specified in the script (in my case it’s computers with the name VDI in it).   After the report is ran, you receive an e-mail with the results sorted by % Free:

 

This is the result:

Param (
$To,
$SMTPServer,
$From
)

Import-Module ActiveDirectory
$VDISearch = get-adcomputer -filter * | Where-Object Name -Like *VDI* #Change this filter to suit your needs

$VDIs = $VDISearch.Name
$Results = @()
foreach ($VDI in $VDIs){
$disk = Get-WmiObject win32_logicaldisk -ComputerName $VDI -Filter “Drivetype=3” -ErrorAction SilentlyContinue
$Results += New-Object PSObject -Property @{
‘VDI’ = $Disk.SystemName
‘Drive Letter’ = $Disk.DeviceID + ‘\’
‘Free Space (GB)’ = ((“{0:N1}” -f ($Disk.FreeSpace/1GB)) + ‘ GB’)
‘% Free’ = ((“{0,6:P0}” -f (($Disk.FreeSpace/1GB)/($Disk.Size/1GB)) ))
}
}

$Header = @”
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
“@

$splat = @{
From = $From
To = $To
SMTPServer = $SMTPServer
Subject = “VDI Disk Space Report Generated by $env:computername”

}
$Results = $Results | Select ‘VDI’, ‘Drive Letter’, ‘Free Space (GB)’,’% Free’ | Sort ‘% Free’

$Body = $Results | ConvertTo-Html -Head $Header | Out-String

Send-MailMessage @splat -Body $Body -BodyAsHTML

 

About AJ McKean

Based in sunny Tauranga, New Zealand, AJ McKean is a Senior Systems Engineer in Mt Maunganui. With over 15 years of professional IT experience working in both New Zealand and the United States, he holds several certifications including MCSE(2000-2003), MCITP:Enterprise(2008), MCSA(2012), VMware VCP-DCV5.5, CompTIA A+ & is an HP Storage Architect. He is passionate about all things IT, especially virtualization, automation & cloud technologies.

Leave a Reply

Your email address will not be published. Required fields are marked *