Ying Li at myITforum.com

PowerShell & System Center

Powershell script to check free disk spaces for servers

Here is a powershell script to check free spaces for the drives against a list of servers and write the results to excel.

$erroractionpreference = "SilentlyContinue"
$a = New-Object -comobject Excel.Application
$a.visible = $True

$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = "Machine Name"
$c.Cells.Item(1,2) = "Drive"
$c.Cells.Item(1,3) = "Total size (MB)"
$c.Cells.Item(1,4) = "Free Space (MB)"
$c.Cells.Item(1,5) = "Free Space (%)"

$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$d.EntireColumn.AutoFit($True)

$intRow = 2

$colComputers = get-content C:\Myworkplace\Clientlist.txt
foreach ($strComputer in $colComputers)
{
$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3"
foreach ($objdisk in $colDisks)
{
 $c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
 $c.Cells.Item($intRow, 2) = $objDisk.DeviceID
 $c.Cells.Item($intRow, 3) = "{0:N0}" -f ($objDisk.Size/1MB)
 $c.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.FreeSpace/1MB)
 $c.Cells.Item($intRow, 5) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
$intRow = $intRow + 1
}
}

Posted: Jan 09 2007, 10:20 AM by yli628 | with no comments
Filed under:

Comments

No Comments