Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I got a Powershell script from here which will export Disk Space usage to html file. I want the scritp to give usage in timely intervals say each 15 mins or 30 mins. is there any way we can mod this script to the same.....

I know this can be achieved via scehduling a task re-running script 15 every mins and refreshing html page will give desired result although is there any other way of it....

Thanks in Advance....

Script....

$freeSpaceFileName = "c:\script\FreeSpace.htm"
$serverlist = "C:\slist.txt"
$warning = 30
$critical = 10
New-Item -ItemType file $freeSpaceFileName -Force
# Getting the freespace info using WMI
#Get-WmiObject win32_logicaldisk  | Where-Object {$_.drivetype -eq 3} | format-table DeviceID, VolumeName,status,Size,FreeSpace 

| Out-File FreeSpace.txt
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString('dd/mm/yyyy')
Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName '<title>All Servers DiskSpace Report</title>'
add-content $fileName '<STYLE TYPE="text/css">'
add-content $fileName  "<!--"
add-content $fileName  "td {"
add-content $fileName  "font-family: Tahoma;"
add-content $fileName  "font-size: 11px;"
add-content $fileName  "border-top: 1px solid #999999;"
add-content $fileName  "border-right: 1px solid #999999;"
add-content $fileName  "border-bottom: 1px solid #999999;"
add-content $fileName  "border-left: 1px solid #999999;"
add-content $fileName  "padding-top: 0px;"
add-content $fileName  "padding-right: 0px;"
add-content $fileName  "padding-bottom: 0px;"
add-content $fileName  "padding-left: 0px;"
add-content $fileName  "}"
add-content $fileName  "body {"
add-content $fileName  "margin-left: 5px;"
add-content $fileName  "margin-top: 5px;"
add-content $fileName  "margin-right: 0px;"
add-content $fileName  "margin-bottom: 10px;"
add-content $fileName  ""
add-content $fileName  "table {"
add-content $fileName  "border: thin solid #000000;"
add-content $fileName  "}"
add-content $fileName  "-->"
add-content $fileName  "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"
add-content $fileName  "<table width='100%'>"
add-content $fileName  "<tr bgcolor='#CCCCCC'>"
add-content $fileName  "<td colspan='7' height='25' align='center'>"
add-content $fileName  "<font face='tahoma' color='#003399' size='4'><strong>All Servers DiskSpace Report - 

$date</strong></font>"
add-content $fileName  "</td>"
add-content $fileName  "</tr>"
add-content $fileName  "</table>"
}

# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)

Add-Content $fileName "<tr bgcolor=#CCCCCC>"
Add-Content $fileName "<td width='10%' align='center'>Drive</td>"
Add-Content $fileName "<td width='50%' align='center'>Drive Label</td>"
Add-Content $fileName "<td width='10%' align='center'>Total Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Used Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Free Space(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Freespace %</td>"
Add-Content $fileName "</tr>"
}

Function writeHtmlFooter
{
param($fileName)

Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}

Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace - $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
 if ($freePercent -gt $warning)
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$devid</td>"
 Add-Content $fileName "<td>$volName</td>"

 Add-Content $fileName "<td>$totSpace</td>"
 Add-Content $fileName "<td>$usedSpace</td>"
 Add-Content $fileName "<td>$frSpace</td>"
 Add-Content $fileName "<td>$freePercent</td>"
 Add-Content $fileName "</tr>"
 }
 elseif ($freePercent -le $critical)
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$devid</td>"
 Add-Content $fileName "<td>$volName</td>"
 Add-Content $fileName "<td>$totSpace</td>"
 Add-Content $fileName "<td>$usedSpace</td>"
 Add-Content $fileName "<td>$frSpace</td>"
 Add-Content $fileName "<td bgcolor='#FF0000' align=center>$freePercent</td>"
 #<td bgcolor='#FF0000' align=center>
 Add-Content $fileName "</tr>"
 }
 else
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$devid</td>"
 Add-Content $fileName "<td>$volName</td>"
 Add-Content $fileName "<td>$totSpace</td>"
 Add-Content $fileName "<td>$usedSpace</td>"
 Add-Content $fileName "<td>$frSpace</td>"
 Add-Content $fileName "<td bgcolor='#FBB917' align=center>$freePercent</td>"
 # #FBB917
 Add-Content $fileName "</tr>"
 }
}
Function sendEmail
{ param($from,$to,$subject,$smtphost,$htmlFileName)
$from=New-Object System.Net.Mail.MailAddress "drvspc@heatancontrol.com.au"
$to= New-Object System.Net.Mail.MailAddress "wbuntin@heatandcontrol.com.au"
$subject="Servers Disk space report - $Date" 
$smtphost="BRISBANE-EX03.int.heatandcontrol.com"
$body = Get-Content $htmlFileName
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)

}

writeHtmlHeader $freeSpaceFileName
foreach ($server in Get-Content $serverlist)
{
 Add-Content $freeSpaceFileName "<table width='100%'><tbody>"
 Add-Content $freeSpaceFileName "<tr bgcolor='#CCCCCC'>"
 Add-Content $freeSpaceFileName "<td width='100%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong> 

$server </strong></font></td>"
 Add-Content $freeSpaceFileName "</tr>"

 writeTableHeader $freeSpaceFileName

 $dp = Get-WmiObject win32_logicaldisk -ComputerName $server |  Where-Object {$_.drivetype -eq 3}
 foreach ($item in $dp)
 {
 Write-Host  $item.DeviceID  $item.VolumeName $item.FreeSpace $item.Size
 writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size

 }
}
writeHtmlFooter $freeSpaceFileName
$date = ( get-date ).ToString('yyyy/MM/dd')
sendEmail xxx@test.com xxx@test.com "Disk Space Report - $Date" hub1 $freeSpaceFileName
share|improve this question
1  
Why scheduling a task could be an issue ? Also, you could consider adding a "refresh" button so that the user, once he has loaded the page for the first time, he could be able to refresh it manually whenever he wants. Maybe you could consider the power of a real web server with dynamic content (aspx in your case) to refresh the page at given intervals. Your code here just provides static html content. –  krisFR Feb 5 at 5:23
    
There are plenty of other ways to do it, but using a scheduled task to run this script every X minutes seems to be the easiest by far. Set it up by GPO to run on all your servers, and call it a day. Making big modifications to the script or approaching it from a different angle just seems to be adding extra effort for no benefit.... unless, of course, there's a reason that a scheduled task isn't an option, but I can't think of one. –  HopelessN00b Feb 5 at 5:49
    
Yeah I agree with both you guys.....although I'm open for any another way.....however we can improve that by adding <META HTTP-EQUIV="refresh" CONTENT="15"> auto refresh tag for auto reloading which could work.........THanks @user2196728 –  CrashOverride Feb 5 at 6:03
1  
This script looks familiar. You should give credit as to where you got it from unless you wrote it. Up to you. –  Shawn Melton Feb 5 at 23:21
    
Yes @ShawnMelton that is correct.....Updated the question....Thanks –  CrashOverride Feb 6 at 5:25

2 Answers 2

You asked for other suggestions, I have a few things to offer..

You can use Performance Monitor to do this, but if it was me, I'd use the Task Scheduler and simply dump the info to someplace central as the overhead for Performance Monitor is rather large for what you are trying to do.

Are you just trying to make sure the drive has space? If so, it might be easier to add a task to the scheduler that is event driven watching for "low disk space" (Log:SYSTEM, Source:SRV, EventID:2013), warnings and sending you an email instead. (I am making an assumption that is the intended purpose for this report, ignore this if not). I use something similar watching for Windows Backup Success and Fail events to let me know that yet once again, the backup failed. This way might be better if it fits the bill so you aren't getting emails every 15 minutes.

Products like MRTG can monitor disk space usage too with a "little tweaking" and give you pretty graphs and a central place to check. (http://www.castellan.net/MRTG.asp)

share|improve this answer

The only thing I don't like about that script is the constant lines of Add-Content, they are not needed. I would have just generated all that HTML like so:


$htmlHeader = @"
....
td {
font-family: Tahoma;
font-size: 11px;
border-top: 1px solid #999999;
border-right: 1px solid #999999;
border-bottom: 1px solid #999999;
border-left: 1px solid #999999;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
body {
margin-left: 5px;
margin-top: 5px;
margin-right: 0px;
margin-bottom: 10px;
add-content $fileName  
table {
border: thin solid #000000;
}
....
"@

Then once I want to add content for the body of the HTML you could use variable $htmlBody and then at the end you combine it all together with $htmlHeader + $htmlBody.

To get your HTML page to refresh you can add a meta-tag in the HTML side that should do the trick for you. This would refresh the page every 30 seconds: meta http-equiv="refresh" content="30" NOTE: SF did not like the tags around this text so it does require the < and /> around it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.