Monday, January 4, 2010

finding out disk space and free space percentage for the servers using powershell

It is necessary for the server administrators to keep an eye on the disk space issues that come up now and then. Its nearly impossible to check the disk space by logging into the servers if there are about 50-100 of them, on a daily basis and verify the same. I have written a powershell script, that will help to keep a track of the disk space and also in terms of percentage the free space available and take actions accordingly.
------------------------------


del "C:\Powershell\lowdisk.xls"
echo "ServerName DriveName DriveSize FreeSpace PercentFree" >> "C:\Powershell\lowdisk.xls"
$computers = Get-Content "c:\Powershell\computerlistall.txt"
foreach ($computer in $computers)
{
$a = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk Where-Object{$_.DriveType -eq 3}
if ($a.DeviceID -eq "C:")
{
$FSize = ($a.FreeSpace)/1GB
$TSize = ($a.Size)/1GB
$PerFree = ($FSize/$TSize)* 100
#echo "ServerName DriveName DriveSize FreeSpace PercentFree" >> "C:\Powershell\lowdisk.xls"
echo "$computer $driveName $TSize $FSize $PerFree" >> "C:\Powershell\lowdisk.xls"
}
else
{
$i = $a.Count
#echo "ServerName DriveName DriveSize FreeSpace PercentFree" >> "C:\Powershell\lowdisk.xls"
for ($t = 0)
{
$driveName = ($a[$t].DeviceID)
$FSize = ($a[$t].Freespace)/ 1GB
$TSize = ($a[$t].Size)/1GB
$PerFree = ($FSize/$TSize)* 100


echo "$computer $driveName $TSize $FSize $PerFree" >> "C:\Powershell\lowdisk.xls"
$t++
if ($t -eq $i)
{
break
}
}
}
if ($PerFree -lt "40.00")
{
Write-Host $PerFree -BackgroundColor Red >> "C:\Powershell\lowdisk.xls"
}
Write-Host "disk space check completed for " $computer
}
----------------

1 comment:

  1. Life is full of many challenges. Challenges that will make you or break you depending on how you handle it.
    Visit my site for more updates. God Bless to your site.

    n8fan.net

    www.n8fan.net

    ReplyDelete