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
    Posts
    • #187552
      Participant
      Topics: 1
      Replies: 0
      Points: 13
      Rank: Member

      Hi,

      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 = “<br/><br/><font face=verdana size=-2>Script $PSScriptRoot running from $env:COMPUTERNAME</font>”
      $ScriptCommonName = “VM Snapshot Check”
      $timestamp = Get-Date -format dd-MM-yyyy
      $MaintenanceCount = 0
      $UnregisteredCount = 0

      #Create HTML Header
      $head = ‘<style>
      META{http-equiv:refresh content:30;}
      BODY{font-family:Verdana;}
      TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
      TH{font-size:10px; border-width: 1px;padding: 6px;border-style: solid;border-color: black;background-color:LightSteelBlue}
      TD{font-size:10px; border-width: 1px;padding: 6px;border-style: solid;border-color: black;background-color:GhostWhite}
      </style>’

      ## Assign email(s) to $sendto and SMTP server to $SMTPsrv
      $sendto = “abc@xyz.co”
      $SMTPsrv = “mail.server.com”
      $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 $credential.host -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 = “scriptcheck@xyz.co”
      To = $sendto
      SmtpServer = “$SMTPsrv”
      }

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

    • #187624
      Moderator
      Topics: 3
      Replies: 218
      Points: 1,187
      Helping Hand
      Rank: Community Hero

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

      https://leanpub.com/creatinghtmlreportsinwindowspowershell/read

      Thank you.

    • #187912
      Participant
      Topics: 1
      Replies: 20
      Points: 109
      Helping Hand
      Rank: Participant

      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><head><title>$reportTitle$titleDate</title> 
      
      <STYLE TYPE='text/css'>
      
          table{border-width:0px;width:100%;}
      
          th, tr, td{font-family:Calibri;font-size:12px;text-align:left;vertical-align:middle;
      
          border-style:solid;border-color:$tableBorderColor;border-width:1px;border-radius:3px 0px 3px 0px;padding:3px;}
      
          tr:nth-child(odd){background-color:$rowColorOdd;}
      
          tr:nth-child(even){background-color:$rowColorEven;}
      
          body{margin:5px 5px 0px 10px;background-color:$bodyBackgroundColor;}
      
          .tdtitle{background-color:$titleBackgroundColor;font-size:30px;font-weight:bold;text-align:left;vertical-align:middle;color:$titleFontColor;}
      
      </style></head> 
      
      <body>
      
      $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
  • The topic ‘How to convert to HTML, help please!’ is closed to new replies.