How to convert to HTML, help please!

Welcome Forums General PowerShell Q&A How to convert to HTML, help please!

Viewing 2 reply threads
  • Author
    • #187552
      Topics: 1
      Replies: 0
      Points: 13
      Rank: Member


      i have a script doing some extraction to check if there is snapshot, and show the date created, age of days..

      i need to convert it to html and email out. i ran the script and the email has totally white blank body.. not sure where is wrong / well, i am also not sure if i am doing it correctly..

      p.s: i edited an existing script to my own though..


      ## Get script path for later
      $PSScriptRoot = $MyInvocation.MyCommand.Definition
      $scriptinfo = "

      Script $PSScriptRoot running from $env:COMPUTERNAME"
      $ScriptCommonName = "VM Snapshot Check"
      $timestamp = Get-Date -format dd-MM-yyyy
      $MaintenanceCount = 0
      $UnregisteredCount = 0

      #Create HTML Header
      $head = '


      ## Assign email(s) to $sendto and SMTP server to $SMTPsrv
      $sendto = ""
      $SMTPsrv = ""
      $subject = "[CHECKS] Virtual machine snapshot — $timestamp"

      ## connecting to V-Center
      $credential = get-vicredentialstoreitem -file E:\script\credentialstored.cred
      set-powercliconfiguration -invalidcertificateaction ignore -confirm:$false
      Connect-VIServer -Server $ -user $credential.user -password $credential.password
      write-host "Connected"
      write-host "Checking VM Snapshot"
      Get-VM | Get-Snapshot | select vm, name, created, @{Label="Age";Expression={($_.created – [datetime]::Now).days}},@{Label="Size";Expression={[math]::Round($_.SizeGB,2)}} | sort @{expression="Age"} | format-table
      Write-host "Complete" -Foregroundcolor Green

      $messageParameters = @{
      Subject = $subject
      Body = $body + $scriptinfo
      From = ""
      To = $sendto
      SmtpServer = "$SMTPsrv"

      # Send e-mail with Server load data
      Send-MailMessage @messageParameters -BodyAsHtml

    • #187624
      Topics: 2
      Replies: 136
      Points: 714
      Helping Hand
      Rank: Major Contributor

      There is a book 'Creating HTML Reports in Windows PowerShell' by  Don Jones, and you can refer it online...

      Thank you.

    • #187912
      Topics: 0
      Replies: 11
      Points: 55
      Helping Hand
      Rank: Member

      Don't forget to format the HTML.  You can use OUT-GRIDVIEW to give yourself something to see, or you can get the module "OUT-HTMLVIEW", which is basically the same thing but in an html file.  Here is a snip of some HTML formatting stuff I use for some emailed reports.  Obviously, you'll not need most of this, but maybe it'll help get you going.  I keep this stuff in a global variables file and call it in the scripts using . sourcing.

      $redColor = "#FF0000" 
      $orangeColor = "#FBB917" 
      $whiteColor = "#FFF"
      $tableBorderColor = "#999"
      $titleBackgroundColor = "#B11B2F"
      $titleFontColor = "#EEE"
      $bodyBackgroundColor = "#FFF"
      $rowColorOdd = "#EEE"
      $rowColorEven = "#B0C8FE"
      #Set warning and critical thresholds to trigger the above cell color changes, useful for some types of reports.
      $percentWarning = 100; 
      $percentCritcal = 15; 
      $titleDate = get-date -uformat "Date %m-%d-%Y - %A" 
      #HTML Header of report, creates a nice big block with the report name and date, and some padding between the row cells.
      $header = " 
      < !DOCTYPE html>$reportTitle$titleDate 
      $reportData | Sort-Object "Name" | ConvertTo-HTML -Head $header | Out-File $reportPath$reportName
      Send-MailMessage -From $From -To $To -Subject "$reportTitle$(Get-Date -Format 'D')" -Body (Get-Content $reportPath$reportName | out-string ) -BodyAsHtml
Viewing 2 reply threads
  • You must be logged in to reply to this topic.