Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

PSHTML

published: June 7, 2019 author: Tinu tags: PowerShell categories: PowerShell-Module


Table of Contents

PSHTML

PSHTML is a cross platform Powershell module to generate HTML markup language written by Stéphane van Gulick. You can find PSHTML documentation here.

PSHTML processes report

Installation

Download from PSGallery

Import Module:

Import-Module PSHTML

List all ExportedCommands:

Get-Command -Module PSHTML

Example PSHTML processes

<#
    https://getbootstrap.com/
#>

function Get-MWAProcess{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [Int] $count
    )

    return Get-Process | Where-Object Name -ne 'Memory Compression' | `
        Sort-Object WorkingSet64 -Descending | `
        Select-Object -First $count | `
        Select-Object Id,Name,Starttime,Path,PrivateMemorySize64,WorkingSet64
}

function Format-MWADataSet{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$false)]
        [Object] $process
    )

    $array = foreach ($item in $process) {
        [pscustomobject]@{
            PID                   = $item.Id
            Name                  = $item.Name
            Starttime             = $item.Starttime
            Path                  = $item.Path
            PrivateMemorySize64KB = ([math]::Round(($item.PrivateMemorySize64)/1kb))
            WorkingSet64KB        = ([math]::Round(($item.WorkingSet64)/1kb))
            PrivateMemorySize64MB = ([math]::Round(($item.PrivateMemorySize64)/1mb))
            WorkingSet64MB        = ([math]::Round(($item.WorkingSet64)/1mb))
        }
    }
    return $array
}

if(-not(Get-Module -Name PSHTML)){
    Import-Module PSHTML
}

$PieCanvasID      = "piecanvas"
$DoughnutCanvasID = "Doughnutcanvas"
$BarCanvasID      = "barcanvas"

$HTML = html {

    head{

        title "PSHTML-Process"
        Write-PSHTMLAsset -Name Jquery
        Write-PSHTMLAsset -Name BootStrap
        Write-PSHTMLAsset -Name Chartjs

        div -Class "container" {
            p -Class "border" {
                h1 "PSHTML process report"
            }
        }
    } 

    body{

        $process = Get-MWAProcess -count 5
        $process | ForEach-Object {
            $total += $_.WorkingSet64
        }

        div -Class "container" {

            p {
                "List most resource-intensive processes"
            }

            p {
                h2 "Summary"
            }

            div -class "container" {
                div -Class "row" {
                    div -Class "col-sm" -Style "allign-center" {
                        canvas -Height 300px -Width 300px -Id $PieCanvasID {}
                    }
                    div -Class "col-sm" -Style "allign-center" {
                        canvas -Height 300px -Width 300px -Id $BarCanvasID {}
                    }
                    div -Class "col-sm" -Style "allign-center" {
                        canvas -Height 300px -Width 300px -Id $DoughnutCanvasID {}
                    }
                }
            }

            script -content {

                $data   = $process | Group-Object Name
                $counts = $data | ForEach-Object {$_.Count}
                $labels = $data.Name
                $colors = @("yellow","red","green","orange","blue")
                
                $dsp1   = New-PSHTMLChartPieDataSet -Data $counts -BackgroundColor $colors
                New-PSHTMLChart -type pie -DataSet $dsp1 -title "Count of most resource-intensive process" -Labels $labels -CanvasID $PieCanvasID 

                $bardata   = Format-MWADataSet -process $process
                $barcounts = $bardata.WorkingSet64MB
                $barlabels = $bardata.Name

                $dsb1 = New-PSHTMLChartBarDataSet -Data $barcounts -label "WorkingSet64 in MB" -backgroundColor 'blue' -hoverBackgroundColor 'red' -borderColor 'red' -hoverBorderColor 'red'
                New-PSHTMLChart -type bar -DataSet $dsb1 -title "Amount of allocated memory in MB" -Labels $barlabels -CanvasID $BarCanvasID 

                $dsd1 = New-PSHTMLChartDoughnutDataSet -Data $barcounts -backgroundcolor $colors -hoverbackgroundColor $Colors
                New-PSHTMLChart -Type doughnut -DataSet $dsd1 -title "Amount of allocated memory in MB" -Labels $barlabels -CanvasID $DoughnutCanvasID 

            }
        
            p {
                h2 "Details"
            }

            div {

                Table -Class "table table-responsive table-sm table-hover" -content {

                    Thead -Class "thead-dark" {

                        Th {"PID"}
                        Th {"Name"}
                        Th {"Path"}
                        Th {"StartTime"}
                        Th {"Commit (KB)"}
                        Th {"Working Set (KB)"}

                    }

                    Tbody {

                        Format-MWADataSet -process $process | ForEach-Object {

                            tr {
                                td {$_.PID}
                                td {$_.Name}
                                td {$_.Path}
                                td {$_.StartTime}
                                td {"{0:N0}" -f ($_.PrivateMemorySize64KB)}
                                td {"{0:N0}" -f ($_.WorkingSet64KB)}
                            }

                        }

                    }

                    #ConvertTo-PSHTMLtable -Object $process
                }

                p -Class "font-weight-bold" {
                    "This $($process.count) processes use {0:N0} MB Memory" -f ([math]::Round(($total)/1mb))
                }

            }

        }

    }

    Footer {

        div -Class "container" {
            p {
                a "Visite me on github.io" -href "https://tinuwalther.github.io/" -Target "_blank" 
                a " | Bootstrap help" -href "https://getbootstrap.com/" -Target "_blank" 
            }
        }

    }
}

$Root = Split-Path -parent $PSCommandPath
$Path = "$($Root)\example.html"
$Html | Out-File -FilePath $Path -Encoding utf8
Start-Process $Path 

← Previous Post [ Top ] Copyright © 2024 by tinuwalther [ Blog ] Next Post →