ShowHTML.ps1

Function Out-HTML
{
<#PSScriptInfo
 
.VERSION
1.0
 
.GUID
a279a77a-7c5b-4e9e-b85a-48604b8d93e1
 
.AUTHOR
edgenl
 
.Description
Convert a HTML string to an HTML file
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Show HTML
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>

   #.Description
   # Convert a HTML string to an HTML file
   #
   # If -HTMLFile is empty, a temporary file wil be created
   #
   #.Example
   # $Webpage = Invoke-WebRequest -Uri 'http://www.nu.nl'
   #
   # Out-HTML -HTML $Webpage.Content -URL 'http://www.nu.nl/' -HTMLFile "c:\temp\test.htm"
   #
   param(
      # The HTML content
      [Parameter(Mandatory=$True, ValueFromPipeline=$True)]
      [string]$HTML,

      # The URL to use as base for relative links
      [string]$URL,

      # The file to write to
      [string]$HTMLFile
   )
 
  If ($URL)
  {
    $UrlParts = $URL -split "/"
    $UrlRoot = $UrlParts[0] + "//" + $UrlParts[2] + "/"

    $HTML = $HTML -ireplace '<head>', ('<head><base href="' + $UrlRoot + '" />')
    $HTML = $HTML -ireplace '</head>', '</head>'
  }

  If (-not $HTMLFile)
  {
    $HTMLFile = ([System.IO.Path]::GetTempFileName()).Replace(".tmp",".htm")
  }

  $HTML | Out-File $HTMLFile

  Invoke-Item $HtmlFile
}