html - Powrshell Loop within Loops -
i have powershell script runs disk usage report (from http://gallery.technet.microsoft.com/disk-space-html-email-f8b6bbfe)
i'm trying change display folder size well, have gotten stuck. wrote works fine:
$volname = "d:\" $folders = (get-childitem $volname | ?{ $_.psiscontainer }) foreach ($folders in $folders) { $size = (get-childitem $folders.fullname -recurse | measure-object -property length -sum) write-host $folders.fullname ($size.sum / 1mb) }
but doesn't come out right when try insert it. have far:
# script generate disk space report exchange servers (daily, weekly, monthly) $erroractionpreference = "silentlycontinue"; $scriptpath = $myinvocation.mycommand.definition $dir = split-path $scriptpath #variables configure $percentwarning = 25; $percentcritcal = 15; $smtpserver = "blah@blah.com" $reportsender = "blah@blah.com" $users = "user1@mydomain.com", "user2@mydomain.com"; $mailsubject = "diskspace report $titledate" #no change needed here!!! $reportpath = "$dir\logs\" $reportname = "diskspacerpt_$(get-date -format ddmmyyyy).html"; $diskreport = $reportpath + $reportname $redcolor = "#ff0000" $orangecolor = "#fbb917" $whitecolor = "#ffffff" $greencolor = "#7fff00" $i = 0; $computers = $env:computername; $datetime = get-date -format "mm-dd-yyyy_hhmmss"; if (test-path $diskreport) { remove-item $diskreport } $titledate = get-date -uformat "%m-%d-%y - %a" $header = " <html> <head> <meta http-equiv='content-type' content='text/html; charset=iso-8859-1'> <title>diskspace report</title> <style type='text/css'> <!-- table { border: thin solid #666666; } 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; table { border: thin solid #000000; } --> </style> </head> <body> <table width='100%'> <tr bgcolor='#cccccc'> <td colspan='7' height='25' align='center'> <font face='tahoma' color='#003399' size='4'><strong>diskspace report $titledate</strong></font> </td> </tr> </table> " add-content $diskreport $header $tableheader = " <table width='100%'><tbody> <tr bgcolor=#cccccc> <td width='10%' align='center'>server</td> <td width='5%' align='center'>drive</td> <td width='25%' align='center'>folder</td> <td width='5%' align='center'>folder size</td> <td width='10%' align='center'>total capacity(gb)</td> <td width='5%' align='center'>used capacity(gb)</td> <td width='5%' align='center'>free space(gb)</td> <td width='5%' align='center'>freespace %</td> </tr> " add-content $diskreport $tableheader foreach($computer in $computers) { $disks = get-wmiobject -computername $computer -class win32_volume -filter "drivetype = 3" | where-object {$_.label -ne "system reserved" -and $_.driveletter -ne "c:"} $computer = $computer.toupper() foreach($disk in $disks) { $deviceid = $disk.label; $volname = $disk.name; $folders = (get-childitem $volname | ?{ $_.psiscontainer }); foreach ($folders in $folders) { $size = (get-childitem $folders.fullname -recurse | measure-object -property length -sum) [float]$size = $disk.capacity; [float]$freespace = $disk.freespace; $percentfree = [math]::round(($freespace / $size) * 100, 2); $sizegb = [math]::round($size / 1073741824, 2); $freespacegb = [math]::round($freespace / 1073741824, 2); $usedspacegb = [math]::round($sizegb - $freespacegb, 2); $color = $greencolor; if($percentfree -lt $percentwarning) { $color = $orangecolor if($percentfree -lt $percentcritcal) { $color = $redcolor } } $datarow = " <tr> <td width='10%'>$computer</td> <td width='5%' align='center'>$volname</td> <td width='25%' >$folders.fullname</td> <td width='5%' >$size</td> <td width='10%' align='center'>$sizegb</td> <td width='5%' align='center'>$usedspacegb</td> <td width='5%' align='center'>$freespacegb</td> <td width='5%' bgcolor=`'$color`' align='center'>$percentfree</td> </tr> " add-content $diskreport $datarow; write-host -foregroundcolor darkyellow "$computer $deviceid percentage free space = $percentfree"; $i++ } } } $tabledescription = " </table><br><table width='20%'> <tr bgcolor='white'> <td width='10%' align='center' bgcolor='#fbb917'>warning less $percentwarning% free space</td> <td width='10%' align='center' bgcolor='#ff0000'>critical less $percentcritcal% free space</td> </tr> " add-content $diskreport $tabledescription add-content $diskreport "</body></html>" if ($i -gt 0) { foreach ($user in $users) { write-host "sending email notification $user" $smtp = new-object net.mail.smtpclient($smtpserver) $msg = new-object net.mail.mailmessage $msg.to.add($user) $msg.from = $reportsender $msg.subject = $mailsubject $msg.isbodyhtml = $true $msg.body = get-content $diskreport $smtp.send($msg) $body = "" } }
and comes out looking this:
http://oi40.tinypic.com/ru9sfb.jpg
anyone know why folder name , folder size variables aren't working?
i think problem (or 1 of problems) line: foreach ($folders in $folders)
. should foreach ($folder in $folders)
, everywhere in loop refer current folder should use $folder
(not $folders
).
give try , see how looks.
Comments
Post a Comment