Archive for December 2008

Disk information via PowerShell & WMI

December 12, 2008

Sorry for taking so much time off in between posts. Lot’s of stuff happening. So, without further waiting, here we go – the long-promised Disk Info via PowerShell & WMI.

Note, there is a filter being applied to Get-WMIObject [“Drive Type = 3”] is a local disk drive.

##Create ServerList.txt file:
sqlcmd /Lc > F:/temp/ServerList.txt

#Populate $Server_List
$Server_List = Get-Content “F:\Temp\ServerList.txt”

##Loop through $Server_List & print disks with (% FreeSpace) < X: # foreach ($row in $Server_List) { $colItems = Get-WmiObject -computername $row -class Win32_LogicalDisk -filter "DriveType = 3" Write-Host "Checking: "$row "...." Write-Host foreach ($objItem in $colItems) { [decimal]$Size = [math]::Round( (($objItem.Size) / 1.00gb) ,2) # cast to [decimal] or [long] due to [decimal]$FreeSpace = [math]::Round( (($objItem.FreeSpace) / 1.00gb) ,2) # PSH inability to cope [decimal]$Percent = [math]::Round( (($FreeSpace / $Size) * 100.00) ,2) # with UInt64 types coming at it. #if ($Percent -le 90) #{ Write-Host " Mount Point: " $objItem.DeviceID Write-Host " Volume Name: " $objItem.VolumeName Write-Host " Total Size: " $Size "GB" Write-Host " Free Space: " $FreeSpace "GB" Write-Host Write-Host " Percent Free: " $Percent"%" Write-Host #} } Write-Host Write-Host }[/sourcecode]