anonit

CreateHTMLwithImages.ps1

Jan 2nd, 2016
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## CreateHTMLWithImages.ps1
  2.  
  3. ## Creates a HTML file with 2 columns.
  4. ## First column is path to image, second column is the image
  5.  
  6. ## Usage:
  7. ## CreateHTMLWithImages.ps1 -website %WEBSITE% -sourcepath %SOURCEPATH% -HTMLfile %HTMLFILE% [-YSize %VERTICALSIZE% -XSize %HORIZONTALSIZE%]
  8. ## -website is the URL to replace the source path with
  9. ## -sourcepath is the path to the images (local or UNC)
  10. ## -HTMLFile is the path and name to the output HTML file.  If this file already exists it will be overwritten
  11. ## -YSize (optional) is the size of the vertical axis in pixels - if not entered default will be 347
  12. ## -XSize (optional) is the size of the vertical axis in pixels - if not entered default will be 554
  13.  
  14. ## eg:
  15. ## CreateHTMLWithImages.ps1 -website http://www.anonit.net/images/2016/1/3/ -sourcepath \\web03\blog\images\2016\1\3\ -HTMLFile \\web03\blog\20160103.html -YSize 100 -XSize 100
  16.  
  17. ## Also uses "ConvertTo-AdvHTML" function from Martin Pugh
  18. ## Twitter:            @thesurlyadm1n
  19. ## Spiceworks:         Martin9700
  20. ## Blog:               www.thesurlyadmin.com
  21. ## http://thesurlyadmin.com/convertto-advhtml-help/
  22. ## http://community.spiceworks.com/scripts/show/2448-create-advanced-html-tables-in-powershell-convertto-advhtml
  23.  
  24.  
  25.  
  26. param(
  27.     [string]$website=$(throw "-website parameter is required"),
  28.     [string]$sourcepath=$(throw "-sourcepath parameter is required"),
  29.     [string]$HTMLfile=$(throw "-HTMLfile parameter is required"),
  30.     [int]$YSize=554,
  31.     [int]$XSize=347
  32. )
  33.  
  34. Function ConvertTo-AdvHTML
  35. {   <#
  36.     .SYNOPSIS
  37.         Advanced replacement of ConvertTo-HTML cmdlet
  38.     .DESCRIPTION
  39.         This function allows for vastly greater control over cells and rows
  40.         in a HTML table.  It takes ConvertTo-HTML to a whole new level!  You
  41.         can now specify what color a cell or row is (either dirctly or through
  42.         the use of CSS).  You can add links, pictures and pictures AS links.
  43.         You can also specify a cell to be a bar graph where you control the
  44.         colors of the graph and text that can be included in the graph.
  45.        
  46.         All color functions are through the use of imbedded text tags inside the
  47.         properties of the object you pass to this function.  It is important to note
  48.         that this function does not do any processing for you, you must make sure all
  49.         control tags are already present in the object before passing it to the
  50.         function.
  51.        
  52.         Here are the different tags available:
  53.        
  54.         Syntax                          Comment
  55.         ===================================================================================
  56.         [cell:<color>]<optional text>   Designate the color of the cell.  Must be
  57.                                         at the beginning of the string.
  58.                                         Example:
  59.                                             [cell:red]System Down
  60.                                            
  61.         [row:<color>]                   Designate the color of the row.  This control
  62.                                         can be anywhere, in any property of the object.
  63.                                         Example:
  64.                                             [row:orchid]
  65.                                            
  66.         [cellclass:<class>]<optional text>  
  67.                                         Designate the color, and other properties, of the
  68.                                         cell based on a class in your CSS.  You must
  69.                                         have the class in your CSS (use the -CSS parameter).
  70.                                         Must be at the beginning of the string.
  71.                                         Example:
  72.                                             [cellclass:highlight]10mb
  73.                                            
  74.         [rowclass:<class>]              Designate the color, and other properties, of the
  75.                                         row based on a class in your CSS.  You must
  76.                                         have the class in your CSS (use the -CSS parameter).
  77.                                         This control can be anywhere, in any property of the
  78.                                         object.
  79.                                         Example:
  80.                                             [rowclass:greyishbold]
  81.                                            
  82.         [image:<height;width;url>]<alternate text>
  83.                                         Include an image in your cell.  Put size of picture
  84.                                         in pixels and url seperated by semi-colons.  Format
  85.                                         must be height;width;url.  You can also include other
  86.                                         text in the cell, but the [image] tag must be at the
  87.                                         end of the tag (so the alternate text is last).
  88.                                         Example:
  89.                                             [image:100;200;http://www.sampleurl.com/sampleimage.jpg]Alt Text For Image
  90.                                            
  91.         [link:<url>]<link text>         Include a link in your cell.  Other text is allowed in
  92.                                         the string, but the [link] tag must be at the end of the
  93.                                         string.
  94.                                         Example:
  95.                                             blah blah blah [link:www.thesurlyadmin.com]Cool PowerShell Link
  96.                                            
  97.         [linkpic:<height;width;url to pic>]<url for link>
  98.                                         This tag uses a picture which you can click on and go to the
  99.                                         specified link.  You must specify the size of the picture and
  100.                                         url where it is located, this information is seperated by semi-
  101.                                         colons.  Other text is allowed in the string, but the [link] tag
  102.                                         must be at the end of the string.
  103.                                         Example:
  104.                                             [linkpic:100;200;http://www.sampleurl.com/sampleimage.jpg]www.thesurlyadmin.com
  105.                                            
  106.         [bar:<percent;bar color;remainder color>]<optional text>
  107.                                         Bar graph makes a simple colored bar graph within the cell.  The
  108.                                         length of the bar is controlled using <percent>.  You can
  109.                                         designate the color of the bar, and the color of the remainder
  110.                                         section.  Due to the mysteries of HTML, you must designate a
  111.                                         width for the column with the [bar] tag using the HeadWidth parameter.
  112.                                        
  113.                                         So if you had a percentage of 95, say 95% used disk you
  114.                                         would want to highlight the remainder for your report:
  115.                                         Example:
  116.                                             [bar:95;dark green;red]5% free
  117.                                        
  118.                                         What if you were at 30% of a sales goal with only 2 weeks left in
  119.                                         the quarter, you would want to highlight that you have a problem.
  120.                                         Example:
  121.                                             [bar:30;darkred;red]30% of goal
  122.     .PARAMETER InputObject
  123.         The object you want converted to an HTML table
  124.     .PARAMETER HeadWidth
  125.         You can specify the width of a cell.  Cell widths are in pixels
  126.         and are passed to the parameter in array format.  Each element
  127.         in the array corresponds to the column in your table, any element
  128.         that is set to 0 will designate the column with be dynamic.  If you had
  129.         four elements in your InputObject and wanted to make the 4th a fixed
  130.         width--this is required for using the [bar] tag--of 600 pixels:
  131.        
  132.         -HeadWidth 0,0,0,600
  133.     .PARAMETER CSS
  134.         Designate custom CSS for your HTML
  135.     .PARAMETER Title
  136.         Specifies a title for the HTML file, that is, the text that appears between the <TITLE> tags.
  137.     .PARAMETER PreContent
  138.         Specifies text to add before the opening <TABLE> tag. By default, there is no text in that position.
  139.     .PARAMETER PostContent
  140.         Specifies text to add after the closing </TABLE> tag. By default, there is no text in that position.
  141.     .PARAMETER Body
  142.         Specifies the text to add after the opening <BODY> tag. By default, there is no text in that position.
  143.     .PARAMETER Fragment
  144.         Generates only an HTML table. The HTML, HEAD, TITLE, and BODY tags are omitted.
  145.     .INPUTS
  146.         System.Management.Automation.PSObject
  147.         You can pipe any .NET object to ConvertTo-AdvHtml.
  148.     .OUTPUTS
  149.         System.String
  150.         ConvertTo-AdvHtml returns series of strings that comprise valid HTML.
  151.     .EXAMPLE
  152.         $Data = @"
  153. Server,Description,Status,Disk
  154. [row:orchid]Server1,Hello1,[cellclass:up]Up,"[bar:45;Purple;Orchid]55% Free"
  155. Server2,Hello2,[cell:green]Up,"[bar:65;DarkGreen;Green]65% Used"
  156. Server3,Goodbye3,[cell:red]Down,"[bar:95;DarkGreen;DarkRed]5% Free"
  157. server4,This is quite a cool test,[cell:green]Up,"[image:150;650;http://pughspace.files.wordpress.com/2014/01/test-connection.png]Test Images"
  158. server5,SurlyAdmin,[cell:red]Down,"[link:http://thesurlyadmin.com]The Surly Admin"
  159. server6,MoreSurlyAdmin,[cell:purple]Updating,"[linkpic:150;650;http://pughspace.files.wordpress.com/2014/01/test-connection.png]http://thesurlyadmin.com"
  160. "@
  161.         $Data = $Data | ConvertFrom-Csv
  162.         $HTML = $Data | ConvertTo-AdvHTML -HeadWidth 0,0,0,600 -PreContent "<p><h1>This might be the best report EVER</h1></p><br>" -PostContent "<br>Done! $(Get-Date)" -Title "Cool Test!"
  163.        
  164.         This is some sample code where I try to put every possibile tag and use into a single set
  165.         of data.  $Data is the PSObject 4 columns.  Default CSS is used, so the [cellclass:up] tag
  166.         will not work but I left it there so you can see how to use it.
  167.     .NOTES
  168.         Author:             Martin Pugh
  169.         Twitter:            @thesurlyadm1n
  170.         Spiceworks:         Martin9700
  171.         Blog:               www.thesurlyadmin.com
  172.          
  173.         Changelog:
  174.             1.0             Initial Release
  175.     .LINK
  176.         http://thesurlyadmin.com/convertto-advhtml-help/
  177.     .LINK
  178.         http://community.spiceworks.com/scripts/show/2448-create-advanced-html-tables-in-powershell-convertto-advhtml
  179.     #>
  180.     #requires -Version 2.0
  181.     [CmdletBinding()]
  182.     Param (
  183.         [Parameter(Mandatory=$true,
  184.             ValueFromPipeline=$true)]
  185.         [Object[]]$InputObject,
  186.         [string[]]$HeadWidth,
  187.         [string]$CSS = @"
  188. <style>
  189. TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
  190. TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;font-size:120%;}
  191. TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
  192. </style>
  193. "@,
  194.         [string]$Title,
  195.         [string]$PreContent,
  196.         [string]$PostContent,
  197.         [string]$Body,
  198.         [switch]$Fragment
  199.     )
  200.    
  201.     Begin {
  202.         If ($Title)
  203.         {   $CSS += "`n<title>$Title</title>`n"
  204.         }
  205.         $Params = @{
  206.             Head = $CSS
  207.         }
  208.         If ($PreContent)
  209.         {   $Params.Add("PreContent",$PreContent)
  210.         }
  211.         If ($PostContent)
  212.         {   $Params.Add("PostContent",$PostContent)
  213.         }
  214.         If ($Body)
  215.         {   $Params.Add("Body",$Body)
  216.         }
  217.         If ($Fragment)
  218.         {   $Params.Add("Fragment",$true)
  219.         }
  220.         $Data = @()
  221.     }
  222.    
  223.     Process {
  224.         ForEach ($Line in $InputObject)
  225.         {   $Data += $Line
  226.         }
  227.     }
  228.    
  229.     End {
  230.         $Html = $Data | ConvertTo-Html @Params
  231.  
  232.         $NewHTML = @()
  233.         ForEach ($Line in $Html)
  234.         {   If ($Line -like "*<th>*")
  235.             {   If ($Headwidth)
  236.                 {   $Index = 0
  237.                     $Reg = $Line | Select-String -AllMatches -Pattern "<th>(.*?)<\/th>"
  238.                     ForEach ($th in $Reg.Matches)
  239.                     {   If ($Index -le ($HeadWidth.Count - 1))
  240.                         {   If ($HeadWidth[$Index] -and $HeadWidth[$Index] -gt 0)
  241.                             {   $Line = $Line.Replace($th.Value,"<th style=""width:$($HeadWidth[$Index])px"">$($th.Groups[1])</th>")
  242.                             }
  243.                         }
  244.                         $Index ++
  245.                     }
  246.                 }
  247.             }
  248.        
  249.             Do {
  250.                 Switch -regex ($Line)
  251.                 {   "<td>\[cell:(.*?)\].*?<\/td>"
  252.                     {   $Line = $Line.Replace("<td>[cell:$($Matches[1])]","<td style=""background-color:$($Matches[1])"">")
  253.                         Break
  254.                     }
  255.                     "\[cellclass:(.*?)\]"
  256.                     {   $Line = $Line.Replace("<td>[cellclass:$($Matches[1])]","<td class=""$($Matches[1])"">")
  257.                         Break
  258.                     }
  259.                     "\[row:(.*?)\]"
  260.                     {   $Line = $Line.Replace("<tr>","<tr style=""background-color:$($Matches[1])"">")
  261.                         $Line = $Line.Replace("[row:$($Matches[1])]","")
  262.                         Break
  263.                     }
  264.                     "\[rowclass:(.*?)\]"
  265.                     {   $Line = $Line.Replace("<tr>","<tr class=""$($Matches[1])"">")
  266.                         $Line = $Line.Replace("[rowclass:$($Matches[1])]","")
  267.                         Break
  268.                     }
  269.                     "<td>\[bar:(.*?)\](.*?)<\/td>"
  270.                     {   $Bar = $Matches[1].Split(";")
  271.                         $Width = 100 - [int]$Bar[0]
  272.                         If (-not $Matches[2])
  273.                         {   $Text = "&nbsp;"
  274.                         }
  275.                         Else
  276.                         {   $Text = $Matches[2]
  277.                         }
  278.                         $Line = $Line.Replace($Matches[0],"<td><div style=""background-color:$($Bar[1]);float:left;width:$($Bar[0])%"">$Text</div><div style=""background-color:$($Bar[2]);float:left;width:$width%"">&nbsp;</div></td>")
  279.                         Break
  280.                     }
  281.                     "\[image:(.*?)\](.*?)<\/td>"
  282.                     {   $Image = $Matches[1].Split(";")
  283.                         $Line = $Line.Replace($Matches[0],"<img src=""$($Image[2])"" alt=""$($Matches[2])"" height=""$($Image[0])"" width=""$($Image[1])""></td>")
  284.                     }
  285.                     "\[link:(.*?)\](.*?)<\/td>"
  286.                     {   $Line = $Line.Replace($Matches[0],"<a href=""$($Matches[1])"">$($Matches[2])</a></td>")
  287.                     }
  288.                     "\[linkpic:(.*?)\](.*?)<\/td>"
  289.                     {   $Images = $Matches[1].Split(";")
  290.                         $Line = $Line.Replace($Matches[0],"<a href=""$($Matches[2])""><img src=""$($Image[2])"" height=""$($Image[0])"" width=""$($Image[1])""></a></td>")
  291.                     }
  292.                     Default
  293.                     {   Break
  294.                     }
  295.                 }
  296.             } Until ($Line -notmatch "\[.*?\]")
  297.             $NewHTML += $Line
  298.         }
  299.         Return $NewHTML
  300.     }
  301. }
  302.  
  303.  
  304.  
  305.  
  306.  
  307. $listoffiles=get-childitem $sourcepath
  308.  
  309. $HTMLObject=@()
  310. foreach ($file in $listoffiles)
  311. {
  312.     $websitepath=$website+$file
  313.     $imagepath="[image:$XSize;$YSize;$websitepath]"
  314.     $object=new-object -TypeName PSObject
  315.     $object | add-member -MemberType NoteProperty -name WebsitePath -Value $websitepath
  316.     $object | add-member -MemberType NoteProperty -name Image -value $imagepath
  317.     $HTMLObject += $object
  318. }
  319. $HTMLobject | convertto-Advhtml > $HTMLFile
Add Comment
Please, Sign In to add comment