HTML output for foreach statement

Hi guys,

could do with some help please, below is some code that goes through all the servers in a list and reports on their disk space but the output is in the wrong format. I want o have each disk on their own line and then highlight those that are over 90% in red… Poutputease help.

Function Get-DiskSpace 
{ 

Get-WmiObject -Class win32_volume -cn $ServerName |
Select-Object @{LABEL='Hostname';EXPRESSION={$s}}, 
driveletter, label,
@{LABEL="Capacity (GB)"; Expression = {"{0:N2}" -f ($_.capacity / 1GB)}}, 
@{LABEL='Free Space (GB)';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}},
<# @{LABEL="Percent Free (%)";Expression = {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100) }},#> 
@{LABEL="Percent Usage (%)";Expression = {"{0:N2}" -f ((($_.Capacity - $_.freespace)/$_.Capacity)*100) }} 
}


$Result = @()
Foreach($ServerName in $ServerList)
{
$drives = get-wmiobject -ComputerName $ServerName -Class win32_LogicalDisk

Foreach ($drive in $drives){

$disk = Get-DiskSpace -servers $drive

$Result += New-Object PSObject -Property @{

Hostname = $ServerName
DriveLetter = $disk.driveletter
Label = $disk.label
CapacityGB = $disk."Capacity (GB)"
FreeSpaceGB = $disk."Free Space (GB)"
PerecntUsage = $disk."Percent Usage (%)"

}
}
}

if($Result -ne $null)
{
$HTML = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #006400;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#006400;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'

$HTML += "<HTML><BODY>"
Foreach($Entry in $Result)
{
if($Entry."PercentUsage" -ge "90")
{
$HTML += ""
}
else
{
$HTML += ""
}
$HTML += "
"
}
$HTML += "
Hostname Drive Letter Label Capacity (GB) Free Space (GB) Perecnt Usage (%)
$($Entry.Hostname) $($Entry.DriveLetter) $($Entry.Label) $($Entry.CapacityGB) $($Entry.FreeSpaceGB) $($Entry.PerecntUsage)
</BODY></HTML>" $HTML | Out-File $OutputFile } $body = [System.IO.File]::ReadAllText($OutputFile) Send-MailMessage -To $to -From $from -Subject "Daily Checks - Regional Disk Space" -Body $body -BodyAsHtml -SmtpServer $smtp

 

 

Wow … my eyes hurt … :wink: … could you please reformat your post and format the code properly as code (described here: Read Me Before Posting! You’ll be Glad You Did!) … AND try to avoid posting that much white space.

Thanks

Sorry first time, hope thats better

although that has screwed up the html code, here it is raw…

Function Get-DiskSpace
{

Get-WmiObject -Class win32_volume -cn $ServerName |
Select-Object @{LABEL=‘Hostname’;EXPRESSION={$s}},
driveletter, label,
@{LABEL=“Capacity (GB)”; Expression = {"{0:N2}" -f ($.capacity / 1GB)}},
@{LABEL=‘Free Space (GB)’;EXPRESSION={"{0:N2}" -f ($
.freespace/1GB)}},
<# @{LABEL=“Percent Free (%)”;Expression = {"{0:N2}" -f (($.FreeSpace / $.Capacity)*100) }},#>
@{LABEL=“Percent Usage (%)”;Expression = {"{0:N2}" -f ((($.Capacity - $.freespace)/$_.Capacity)*100) }}
}

$Result = @()
Foreach($ServerName in $ServerList)
{
$drives = get-wmiobject -ComputerName $ServerName -Class win32_LogicalDisk

Foreach ($drive in $drives){

$disk = Get-DiskSpace -servers $drive

$Result += New-Object PSObject -Property @{

Hostname = $ServerName
DriveLetter = $disk.driveletter
Label = $disk.label
CapacityGB = $disk.“Capacity (GB)”
FreeSpaceGB = $disk.“Free Space (GB)”
PerecntUsage = $disk.“Percent Usage (%)”

}
}
}

if($Result -ne $null)
{
$HTML = ‘<style type=“text/css”>
#Header{font-family:“Trebuchet MS”, Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #006400;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#006400;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>’

$HTML += "<HTML><BODY>

" Foreach($Entry in $Result) { if($Entry."PercentUsage" -ge "90") { $HTML += "" } else { $HTML += "" } $HTML += " " } $HTML += "
Hostname Drive Letter Label Capacity (GB) Free Space (GB) Perecnt Usage (%)
$($Entry.Hostname) $($Entry.DriveLetter) $($Entry.Label) $($Entry.CapacityGB) $($Entry.FreeSpaceGB) $($Entry.PerecntUsage)
</BODY></HTML>"

$HTML | Out-File $OutputFile
}

$body = [System.IO.File]::ReadAllText($OutputFile)

Send-MailMessage -To $to -From $from -Subject “Daily Checks - Regional Disk Space” -Body $body -BodyAsHtml -SmtpServer $smtp

Ahh … ok … now I understand … you included html code. That’s not gonna work. You should post it as a Github Gist.

Never used Github before although i had an account, let me know if this is not accessible

 

https://github.com/richardbrown1983/Scripts/blob/master/disk%20alerts

We can get to the code now. Thanks.

What’s wrong with the output? At first glance, it looks like it will create what you’re asking for… Can you provide a scrubbed example of the raw HTML that is generated?

Hi Richard
Could I recommend you the “ConvertTo-Html” cmdlet? I would normally write like below for any basic Html output. it is a lot cleaner and readable.

$Head = @"        
    Put your style tag here.
"@
ConvertTo-Html -Head $Head -Body $YourBodyTable -Title "Your Title" | Out-File $OutputFile

Naw,

may I mention that Richard wanted to have some of the values in the output html colored in red? :wink:

Thank you for your help so far, as mentioned by Olaf i am trying to get the results that equal or greater than highlighted in red so would need to be HTML.

The main issue is that the output is listing all possible drives for each server in one line as opposed to each drive on a new line. A screen shot of the output can be found here.

 

https://github.com/richardbrown1983/Scripts/blob/master/diskspace.PNG

If the GitHub code is what you’re running, you’re going to kick yourself… Verify your spelling.

if($Entry."PercentUsage" -ge "90")
$($Entry.PerecntUsage)

Thanks very much Charles,

 

I hadn’t even picked up on the fact that the cells weren’t highlights good find… Is till have the issue where all the drives are bunched into one though and i really would have liked for the to be a drive per line… as shown below - thank you for picking up on that though Charles.

https://github.com/richardbrown1983/Scripts/blob/master/diskspace2.PNG

I noticed some things that would be causing the issues:

  1. Get-DiskSpace isn't really doing anything, and if it was supposed to, you would need to make it an advanced function for it to be more useful. It can be removed completely.
  2. Each row is returning the entire collection rather than just one object per row. For instance, the $Result += New-Object ... in the Foreach ($drive in $drives){...}, you're referencing $disk.driveletter (and so on) where $disk is the collection, not an individual disk object. You need to reference the $drive instead.
  3. Just assign the foreach to $Result instead of concatenating objects ($Result += New-Object).
Change the foreach ($ServerName in $ServerList) to this:
$Result = Foreach ($ServerName in $ServerList) {
$drives = Get-CimInstance -Class win32_volume -ComputerName $ServerName |
    Select-Object -Property @{LABEL = 'Hostname'; EXPRESSION = { $s } },
        driveletter, label,
        @{LABEL = "Capacity (GB)"; Expression = { "{0:N2}" -f ($_.capacity / 1GB) } },
        @{LABEL = 'Free Space (GB)'; EXPRESSION = { "{0:N2}" -f ($_.freespace / 1GB) } },
        &lt;# @{LABEL="Percent Free (%)";Expression = {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100) }},#&gt;
        @{LABEL = "Percent Usage (%)"; Expression = { "{0:N2}" -f ((($_.Capacity - $_.freespace) / $_.Capacity) * 100) } }

Foreach ($drive in $drives) {
    [pscustomobject]@{
        Hostname     = $ServerName
        DriveLetter  = $drive.driveletter
        Label        = $drive.label
        CapacityGB   = $drive."Capacity (GB)"
        FreeSpaceGB  = $drive."Free Space (GB)"
        PercentUsage = $drive."Percent Usage (%)"
    }
}

}

Thank you very much for your help Aaron, now you say it it makes perfect sense.

 

Rich