Making HTML Reports

Defining some objects

Let's start by creating some students

class Student {
        [string]$firstName=""
        [string]$lastName=""
        [int]$score=3
        [System.DateTime]$birthday
        Student ([string]$firstName,[string]$lastName,[int]$score,[int]$day,[int]$month,[int]$year) {
            $this.firstName = $firstName
            $this.lastName = $lastName
            $this.score = $score
            $this.birthday = (get-date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year $year -Day $day -Month $month)
        }
        Student ([string]$firstName,[string]$lastName,[int]$score,[int]$day,[int]$month) {
            $this.firstName = $firstName
            $this.lastName = $lastName
            $this.score = $score
            $this.birthday = (get-date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year 2003 -Day $day -Month $month)
        }
        Student ([string]$firstName,[string]$lastName,[int]$score,[System.DateTime]$birthday) {
            $this.firstName = $firstName
            $this.lastName = $lastName
            $this.score = $score
            $this.birthday = $birthday
        }
        Student ([string]$firstName,[string]$lastName) {
            $this.firstName = $firstName
            $this.lastName = $lastName
            $this.score = (Get-Random -Minimum 1 -Maximum 10)
            $this.birthday = (get-date -Minute 0 -Second 0 -Millisecond 0 -Hour 0 -Year 2003 -Day (Get-Random -Minimum 1 -Maximum 28) -Month (Get-Random -Minimum 1 -Maximum 12))
        }
        [string] GetFullName( ) {
            return ("{0} {1}" -f $this.firstName,$this.lastName)
        }
        [string] GetRapportLine( ) {
            return ("{0} {1} - {2}/10" -f $this.firstName,$this.lastName,$this.score)
        }
        static [Student[]] ConvertFromSerialization([PSObject[]]$psostudents) {
            $students = @()
            foreach($s in $psostudents) {
                $students += new-object Student($s.firstName,$s.lastName,$s.score,$s.birthday)
            }
            return $students
        }
}
$students = @()
$students += new-object Student("Bob","Sinclair")
$students += new-object Student("Alice","Cooper")
$students += new-object Student("Freddie","Mercury")

Simple HTML Output

$htmlpath = "c:\d\students.html"
$students | ConvertTo-Html | set-content -path $htmlpath

Tweaking Headers

$htmlpath = "c:\d\students.html"
$columns = @()
$columns += @{Name="Name";Expression={("{0} {1}" -f $_.firstName,$_.lastName)}}
$columns += @{N="Score";E={(" {0}/20" -f $_.score)}}
$students | select  $columns | ConvertTo-Html | set-content -path $htmlpath

Adding some additional pre

$htmlpath = "c:\d\students.html"

$columns = @()
$columns += @{Name="Name";Expression={("{0} {1}" -f $_.firstName,$_.lastName)}}
$columns += @{N="Score";E={(" {0}/20" -f $_.score)}}

$students | select  $columns | ConvertTo-Html -title "Student Report Overview" -pre "<h1>Student Overview</h1>" | set-content -path $htmlpath

Adding some css (custom header)


$htmlpath = "c:\d\students.html"

$columns = @()
$columns += @{Name="Name";Expression={("{0} {1}" -f $_.firstName,$_.lastName)}}
$columns += @{N="Score";E={("{0}/20" -f $_.score)}}

$header = @"
<head>
    <title>{0}</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <style>
        table {{
            margin-left:5px;
        }}
        td {{
            padding-right:5px;
        }}
    </style>
</head>
"@ -f "Student Report Overview"

$students | select  $columns | ConvertTo-Html -head $header  -pre "<h1>Student Overview</h1>" | set-content -path $htmlpath

Making your server overview report


$header = @"
<head>
    <title>{0}</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <style>
        table {{
            margin-left:5px;
        }}
        td {{
            padding-right:5px;
        }}
    </style>
</head>
"@ -f "Server Overview"

$fragments = @()

$volumes = Get-Volume | select driveletter,size,sizeremaining,@{n="Full %";e={[Math]::Ceiling(($_.Size-$_.sizeRemaining)*100/$_.Size)}}
$fragments += $volumes | convertto-html -fragment -pre "<h1>Volumes</h1>"

$services = get-service  | select DisplayName,Name,StartType,Status
$fragments += $services | ConvertTo-Html -Fragment -pre "<h1>Services</h1>"

$runatboot = Get-Item -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
$runatboottab = $runatboot.GetValueNames() | % { $n = $_;new-object -type psobject -property @{Name=$_;ExecPath=(get-itempropertyvalue -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run -name $n)}}
$fragments += $runatboottab | ConvertTo-Html -Fragment -pre "<h1>Startup Processes</h1>"

$fragments += Get-Process | select Name,Path  | ConvertTo-Html -Fragment -pre "<h1>Snapshot Processes</h1>"

ConvertTo-Html -Body $fragments -Head $header | set-content "c:\d\serverreport.html"

Sending an email

$volumes = Get-Volume | select driveletter,size,sizeremaining,@{n="Full %";e={[Math]::Ceiling(($_.Size-$_.sizeRemaining)*100/$_.Size)}} 
$email =  ($volumes  | convertto-html -pre "<h1>Volumes</h1>") -join "`n"
$subject = "Volume Overview"
$from = "bob@powerstart.org"
$to = "alice@powerstart.org"
$smtpserver = "127.0.0.1"
$port = 25


Send-MailMessage -subject $subject -BodyAsHtml -body $email -to $to -from $from -smtpserver $smtpserver -port $port

A Special module for this series to make bootstrap html reports

Invoke-Expression $(Invoke-WebRequest 'https://raw.githubusercontent.com/tdewin/randomsamples/master/powerstarthtml/bootstrap.ps1').content
$path = ("powerstarthtml-test.html")

import-module PowerStartHTML
$ps = New-PowerStartHTML -title "Hello PowerStartHTML!"
(get-process) | select name,cpu,npm | Add-PowerStartHTMLTable -psHtml $ps -tableTitle "Processes" -tableClass "table table-striped"
$ps.Save($path)

explorer $path

```

Send Email https://smtp4dev.codeplex.com/

results matching ""

    No results matching ""