Using Powershell to report on disk space

I have several Windows servers serving as our Oracle front end servers, unfortunately, these servers have limited disk space on their OS and Data drives.
Each morning these need to be checked to ensure that the drives don’t fill which result in problems.
So, to save logging on to each server each morning I investigated the use of Powershell to connect to a specified list of servers and report back on the disk freespace in HTML format.
To follow will be the contents of the Powershell ps1 file that I now access via a shortcut on my desktop.
Listing for diskspacepercent.ps1

$outData = @(“”)
$dataFromServer = Get-WmiObject Win32_Volume -ComputerName (get-content c:\PowershellScripts\servers.txt) Select-Object SystemName,Label,Name,DriveLetter,DriveType,Capacity,Freespace
foreach ($currline in $dataFromServer) {
if ((-not $currline.name.StartsWith(“\\”)) -and ($currline.Drivetype -ne 5)) {
#calculate percent of freespace
[float]$tempfloat = ($currline.Freespace / 1048576) / ($currline.Capacity / 1048576)
$temppercent = [math]::round(($tempfloat * 100),2)
#calculate free space in MB
[float]$tempfreespace = ($currline.Freespace / 1048576)
$tempfreespace = [math]::round($tempfreespace,2)
#calculate capacity in MB
[float]$tempcapacity = ($currline.Capacity / 1048576)
$tempcapacity = [math]::round($tempcapacity,2)
#add custom fields to output string
add-member -InputObject $currline -MemberType NoteProperty -name FreePercent -value “$temppercent %”
add-member -InputObject $currline -MemberType NoteProperty -name FreeSpaceMB -value “$tempfreespace”
add-member -InputObject $currline -MemberType NoteProperty -name CapacityMB -value “$tempcapacity”
#update the output string
$outData = $outData + $currline
}
}
$outData Select-Object SystemName,Label,Name,CapacityMB,FreeSpaceMB,FreePercent convertto-html out-file c:\PowerShellScripts\report.html
Invoke-Item c:\PowerShellScripts\report.html

The next logical step would be to schedule the execution of this script, perhaps also to have it email me the report so I can keep on top of things when off site.
Further note: Will look at appending the run-date to the filename. This could be handy as the reports can be archived and I’ll have a basic picture of disk usage.
Updated: 17/03/2010
Have amended the script slightly, the end of the file (after the last curly brace) looks like this:

$filename = ‘c:\PowerShellScripts\DiskSpace_’ + (get-date -format ‘dd-MM-yyyy’) + ‘.html’$filename = $filename.ToString().Replace(“-“, “”)
$outData Select-Object SystemName,Label,Name,CapacityMB,FreeSpaceMB,FreePercent convertto-html out-file $filename
Invoke-Item $filename

This now automatically adds an underscore and the date (in UK format) to the end of the filename (DiskSpace) before appending the html file extension. I have used the Replace method of the ToString function to get rid of the hypen in the date.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *