lac.fi

Notes to self
› PowerShell, WMI: Remote disks

Using PowerShell: Domain Computers as a base, this script queries logical drives of all active domain computers and shows the amount of free space in each.

$Root = "LDAP://dc=domainname,dc=local"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry($Root)
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = "(objectCategory=Computer)"
$objSearcher.SearchScope = "Subtree"

$colProplist = "name", "distinguishedname"
foreach ($s in $colProplist) { $objSearcher.PropertiesToLoad.Add($s) | out-null }

# Get a list of computers and loop through it
$colResults = $objSearcher.FindAll()
Write-Host "Found $($colResults.count) computers"
foreach ($objResult in $colResults) {
	$objItem = $objResult.Properties

	$objComputer = [ADSI]"LDAP://$($objItem.distinguishedname)"
	# Make sure the computer account isn't disabled
	if (!$objComputer.PsBase.InvokeGet("AccountDisabled")) {
		# Do a test to see if the computer responds to WMI queries
		$Test = Get-WmiObject -query "Select Caption From Win32_ComputerSystem" -computername $($objItem.name) -errorAction silentlyContinue
		if (-not $Test) {
			Write-Host "$($objItem.name) is not responding" -ForegroundColor Gray
		} else {
			Write-Host $objItem.name

			# Get a list of logical drives that are on fixed disks and loop through it
			# More information about this WMI class at http://msdn.microsoft.com/en-us/library/aa394173%28v=vs.85%29.aspx
			$LogicalDisks = Get-WmiObject -computername $($objItem.name) -class Win32_LogicalDisk -filter "MediaType=12" | Select-Object Name, Size, FreeSpace
			foreach ($LogicalDisk in $LogicalDisks) {
				$percentage = [math]::round($LogicalDisk.FreeSpace / $LogicalDisk.Size * 100)
				$size = [math]::round($LogicalDisk.Size / 1GB)
				Write-Host "  $($LogicalDisk.Name) $($percentage) % free of $($size) GB"
			}
		}
	}
}