HTML output for foreach statement

Welcome Forums General PowerShell Q&A HTML output for foreach statement

Viewing 10 reply threads
  • Author
    Posts
    • #183444
      Participant
      Topics: 1
      Replies: 6
      Points: 27
      Rank: Member

      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 = ''
      
      $HTML += ""
      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)
      " $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

       

       

    • #183459
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      Wow ... my eyes hurt ... 😉 ... 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

      • #183495
        Participant
        Topics: 1
        Replies: 6
        Points: 27
        Rank: Member

        Sorry first time, hope thats better

      • #183498
        Participant
        Topics: 1
        Replies: 6
        Points: 27
        Rank: Member

        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 = '

        '

        $HTML += "

        "
        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)

        "

        $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

    • #183507
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

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

    • #183834
      Participant
      Topics: 6
      Replies: 108
      Points: 301
      Helping Hand
      Rank: Contributor

      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?

    • #183933
      Naw
      Participant
      Topics: 4
      Replies: 45
      Points: 90
      Rank: Member

      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
      
    • #183936
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      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.

      Naw,

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

    • #184314
      Participant
      Topics: 1
      Replies: 6
      Points: 27
      Rank: Member

      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

    • #184568
      Participant
      Topics: 6
      Replies: 108
      Points: 301
      Helping Hand
      Rank: Contributor

      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)
    • #184616
      Participant
      Topics: 1
      Replies: 6
      Points: 27
      Rank: Member

      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

    • #184640
      Participant
      Topics: 10
      Replies: 117
      Points: 456
      Helping Hand
      Rank: Contributor

      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) } },
                  < # @{LABEL="Percent Free (%)";Expression = {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100) }},#>
                  @{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 (%)"
              }
          }
      }
    • #185255
      Participant
      Topics: 1
      Replies: 6
      Points: 27
      Rank: Member

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

       

      Rich

Viewing 10 reply threads
  • You must be logged in to reply to this topic.