Powershell. Get computers from Active Directory with low disk space.

We have float pool in VMware Horizon and often C: disk is running out of space.
Here is a quick script that I wrote to get all computers from AD with low disk space.

Of course you can wrap it into function, add email notification or make some peace of code better, but I’m to lazy to do it:)
Here is how it’s looks like:

You just need to change $lowspaceGB to any disk space you need and $OU to yout target ou.
Also this script required ActiveDirectory powershell module on computer which run this code and winRM ports must be opened on target computers.

This script will ping every pc in OU and if computer will reply then disk space will be evaluated and printed out to console.

$lowspaceGB = "19"
$OU = "OU=Horizon-VDI,OU=CR,DC=Domain,DC=com"

$allpcs = Get-ADComputer -SearchBase $OU -Filter *
$availablepcs = @()
$lowspacepc = @()
$allpcs | %{
if (Test-Connection $_.DNSHostName -Count 1 -ErrorAction SilentlyContinue){
$availablepcs += $_.DNSHostName
}
}
foreach($pc in $availablepcs){
$freespace = (Invoke-Command -ComputerName $pc -Command {Get-PSDrive C}).Free
if ($freespace/1GB -lt $lowspaceGB){
$obj = New-Object System.Object
$obj | Add-Member -Type NoteProperty -Name "DNSHostName" -Value $pc
$obj | Add-Member -Type NoteProperty -Name "FreeSpaceGB" -Value ([math]::truncate($freespace/1GB))
$lowspacepc += $obj
}
}
$lowspacepc

Download script.