How to make html table.

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 7 months ago.

  • Author
    Posts
  • #15042

    Hello, I have made script for deleting old folder(log) from one drive.After deleting files, I need to check the current drive size and senddetails in HTMl format though mail. But somehow i am not able to figure thing out. Output in HTml is not correect. I am pasting my code .Please help me rectify the error.

    I am new to powershell scripting, teaching myself from here and there.

  • #15045
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You haven't posted your code yet. However, if you click on Resources -> Free eBooks on this site, you'll find one called "Creating HTML Reports in PowerShell" which has some good info on this topic, as well as an EnhancedHTML2 module that you can use to do quite a bit more than the built-in ConvertTo-Html cmdlet.

  • #15057

    Hello Dave,

    Thanks for the reply.I have attached my code in attachemnt . I am not sure if that works here. This is first post here. I am attaching here again.
    If that dont work , pasting code here directly.

    ##############Code#######################

    ## script to delete old logs##

    #—– define parameters —–#

    #...................................
    # Email Settings
    #...................................

    $smtpsettings = @{
    To = "123@xyz.com"
    From = "234@xyz.com"
    Subject = "Blackberry Server DriveSpace Alert "
    SmtpServer = "smtpserver.inter.rsag.site"
    }

    [int]$foldersize = 0

    #—– define function —–#

    function Get-Size
    {
    param([string]$path)
    "{0:n2}" -f ((gci -path $path -recurse | measure-object -property length -sum).sum /1mb)
    }

    #—– get current date —-#
    $Now = Get-Date

    #—– define amount of days —-#
    $Days = "31"

    #—– define folder where files are located —-#
    $TargetFolder = 'D:\Program Files (x86)\Research In Motion\BlackBerry Enterprise Server\Logs'

    #—– define extension —-#
    $Extension = "*.log"

    #—– define LastWriteTime parameter based on $Days —#
    $LastWrite = ($Now.AddDays(-$Days)).tostring()

    ##Get all folders from the $TargetFolder and apply the $LastWrite filter##

    $Folders = Get-Childitem $TargetFolder | ?{ $_.PSIsContainer } | Where {($_.LastWriteTime -le "$LastWrite")}

    ##calculating Size of folders which will be deleted##

    Foreach ($Folder in $Folders)
    {
    if ($folder -ne $NULL)
    {
    write-host "calculating Folder size of $Folder" -ForegroundColor "yellow"
    $foldersize += get-size D:\"Program Files (x86)"\"Research In Motion"\"BlackBerry Enterprise Server"\Logs\$folder
    }
    else
    {
    Write-Host "Empty Folder " -foregroundcolor "Orange"
    }
    }

    write-host "Total foldersize that will be deleted :$foldersize MB"

    ##Deleting folder one by one##

    foreach ($Folder in $Folders)
    {
    if ($folder -ne $NULL)
    {
    write-host "Deleting Folder $Folder" -ForegroundColor "DarkRed"
    Remove-Item $Folder.FullName | out-null
    }
    else
    {
    Write-Host "No more folder to delete!" -foregroundcolor "Green"
    }
    }

    ## Getting total free space on all drive of server##

    $PresentSize = gwmi win32_volume -Filter 'drivetype = 3' | select driveletter,freespace,capacity

    ###############################################################

    ##Converting disk space output to HTMl table##
    $tableHtml = $null

    #Begin Summary table HTML header
    $header= "

    Disk Name Free Space Total Disk Capacity

    "

    $tablehtml+= $header

    #Begin rows of table##

    $tablehtml+=

    foreach($u in $presentsize)
    {
    $tablehtml += "

    "

    $tablehtml+=

    "$_.driveletter" "$_.freespace" "$_.capacity"

    }

    ##Sending Notification mail to Team##

    $body = "Hello Team,

    Logs older than $LastWrite in path $TargetFolder are deleted.

    Total foldersize deleted is: $foldersize MB.

    Now free space in disks of server blackberry server are as follows:

    $tablehtml

    Thanks,

    Hasin Choudhary

    ."

    Send-MailMessage @smtpsettings -Body $body -bodyasHtml -Encoding ([System.Text.Encoding]::UTF8)

    #########################Code complete#####################################################################

  • #15067
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You have to rename .PS1 attachments to .TXT to upload them here, but I can work with what you've posted into the thread. I had to replace a bunch of smart quotes with normal single / double quotation marks to get the code to display properly in the ISE (and most of those probably came from the forum software), but once I did that, this part of the code jumps out as having a few problems:

    ##Converting disk space output to HTMl table##
    $tableHtml = $null
     
    #Begin Summary table HTML header
    $header= " 
    Disk Name Free Space Total Disk Capacity
    " $tablehtml+= $header #Begin rows of table## foreach($u in $presentsize) { $tablehtml += " "$_.driveletter" "$_.freespace" "$_.capacity" " $tablehtml+= }

    First off, I would suggest using either ConvertTo-Html, or even better, the functions in the EnhancedHTML2 module that I mentioned in my last post, rather than creating the HTML strings yourself. However, for now, I'll just focus on how to correct your existing code.

    • You're missing quotation marks around
      and

      on both of those lines.

    • You're appending inside your foreach loop, instead of after it completes.
    • You also have a hard-coded tag inside your $header string, which shouldn't be there.
    • You also have duplicate
      opening tags (one in your $header string, one added after the #Begin rows comment).
    • The row string in your foreach loop is malformed. If you want to append $_.DriveLetter , etc into a string, you need to either use the + operators to concatenate the strings, use the subexpression operator inside the double-quoted string, or use the format operator. I usually use the second option, unless I want to change the formatting of data in some way, and then I use the format operator.

    Here's a modification of that bit of code that should probably work (though I haven't tested it):

    ##Converting disk space output to HTMl table##
    $tableHtml = $null
     
    #Begin Summary table HTML header
    $header= " "
     
    $tablehtml+= $header 
    
    #Begin rows of table##
      
    foreach($u in $presentsize)
    {
        $tablehtml += ""
     
    }
    
    $tablehtml+= '
    Disk Name Free Space Total Disk Capacity
    $($_.driveletter) $($_.freespace) $($_.capacity)
    '

    Now, an even better option, as I mentioned earlier, would be to simply include head and style tags in your HTML body, put CSS code into that header (similar to what you did with

    ), and then build the table itself by doing something like this:

    $selectProperties = @(
        @{ Name = 'Disk Name'; Expression = { $_.DriveLetter } }
        @{ Name = 'Free Space'; Expression = { $_.FreeSpace } }
        @{ Name = 'Total Disk Capacity'; Expression = { $_.Capacity } }
    )
    
    $tablehtml = $PresentSize | Select-Object $selectProperties | ConvertTo-Html -Fragment | Out-String
    

    That will produce a completely basic HTML table string, with no embedded formatting whatsoever. That's where the CSS code in your HTML body comes in, to control how the table is displayed. If you download the EnhancedHTML2 module and read the free eBook I mentioned, it makes some of that even easier (and more fully featured, such as automatically tagging alternating HTML rows with different classes / styles, etc.)

You must be logged in to reply to this topic.