PSWriteHTML is awesome

Our Score
Click to rate this post!
[Total: 0 Average: 0]

Recently I was playing with the excellent PSWriteHTML powershell module and ended up creating a dashboard from events. Since I had a hard time going through all of the dashboard option, I’ve decided to share what I did, may be it will help someone. The example script requires PSWriteHTML module installed. Event properties I got from here, don’t know is this the original author, anyway, I wish all good to the author, whoever he is!

I do not pretend this is the best code ever, however, IMHO it is doing the job right, It works out of the box, just adjust server names ($srv), taking the reboots for the last 30 days with max events limited to 50(pretty generous). There are comments here and there, if something is not clear, don’t be shy and post a comment.

Import-Module PSWriteHTML
#Get Servers List using Get-Adcomputer or by creating the list itself
$srv = Get-ADComputer -Filter {name -like "Serv1" -and name -notlike "Whatever"} | Sort-Object name | Select-Object -ExpandProperty name
#$srv = 'SERVER1', 'SERVER2', 'SERVER3'
#Set-Dates variables
$lastMonth = (Get-Date).AddDays(-30)
$today = Get-Date
$dataSet = @()
#Loop through events end get their properties readable, add everything to dataset array
foreach ($s in $srv) {
    Get-WinEvent -MaxEvents 50 -FilterHashtable @{logname = 'System'; id = 1074; StartTime = $lastMonth; EndTime = $today } -ComputerName $s -ErrorAction SilentlyContinue |
    ForEach-Object {
        $ev = New-Object PSObject | Select-Object Computer, Date, User, Action, Process, Reason, ReasonCode, Comment
        $ev.Computer = $_.Properties[1].Value
        $ev.Date = $_.TimeCreated
        $ev.User = $_.Properties[6].Value
        $ev.Process = $_.Properties[0].Value
        $ev.Action = $_.Properties[4].Value
        $ev.Reason = $_.Properties[2].Value
        $ev.ReasonCode = $_.Properties[3].Value
        $ev.Comment = $_.Properties[5].Value
        $dataset += $ev
    } | Select-Object Computer, Date, Action, Reason, User
   
}
#Create empty arrays
$rebootsChamp = @()
$tempArr = @()
#Create Dashboard and with loops 
Dashboard -Name 'Reboots Log' -FilePath $PSScriptRoot\Reboots.html {
    Tab -Name 'Reboots Log For Last 30 days'-IconSolid desktop -IconColor WhiteSmoke {
        TabOptions -SelectorColor YellowOrange -BorderRadius 5px -BackgroundColor AppleBlossom 
        for ($x = 0; $x -lt $dataSet.count; $x++) {
            #If last add it to the dashboard and break the loop
            if (($x) -eq $dataSet.count - 1) {
                Section -Name $dataSet[$x].Computer -HeaderBackGroundColor CarrotOrange -Collapsable{
                    Panel {
                        Table -HideFooter -DataTable $dataSet[$x]
                    }
                }
                $tempArr = @()
                $tempArr += , $dataSet[$x]
                ForEach-Object {
                    $properties = @{
                        Name    = $dataSet[$x].Computer                    
                        Reboots = $tempArr.Count                      
                    }                        
                    $o = New-Object psobject -Property $properties;
                    $rebootsChamp += , $o
                }
               
                break;
            }
            #compare the values with next computer name if same add to the array
            if ($dataSet[$x].Computer -eq $dataSet[$x + 1].Computer) {                                                       
                $tempArr += , $dataSet[$x]                
            }  
            #if not the same as previous add info to the arrays and create the panel
            if ($dataSet[$x].Computer -ne $dataSet[$x + 1].Computer) {
                $tempArr += , $dataSet[$x]
              
                Section -Name $dataSet[$x].Computer -Collapsable -HeaderBackGroundColor CarrotOrange {
                    Panel {
                        Table -HideFooter -DataTable $tempArr
                    }
                }
                ForEach-Object {
                    $properties = @{
                        Name    = $dataSet[$x].Computer                    
                        Reboots = $tempArr.Count                      
                    }                        
                    $o = New-Object psobject -Property $properties;
                    $rebootsChamp += , $o
                }
               
                $tempArr = @()
            }  
        } 
        #Finally Add a section with all servers using charts
        Section -Name 'Reboots Champions' -HeaderBackGroundColor CarrotOrange { 
            Panel { 
                Chart -Title "Reboots Champions" {
                    ChartBarOptions -Distributed 
                    ChartToolbar -Download
                    #   ChartAxisX -Name '$(Get-Culture).DateTimeFormat.GetMonthName((Get-Date).Month)'
                    ChartLegend -Name 'Reboot Counts'
                    for ($i = 0; $i -lt $rebootsChamp.Count; $i++) {
                        if ($rebootsChamp[$i].Name -ne ($rebootsChamp[$i + 1].Name)) {
                            ChartBar -Name $rebootsChamp[$i].Name -Value $rebootsChamp[$i].Reboots
                        }
                    }
                }
            }
            
        }             
    }  
}

#Copy html to a site where it will be hosted
Copy-Item C:\temp\Dashimo\Reboots.html -Destination '\\WebServer\D$\RebootDashboard'

Our Score
Click to rate this post!
[Total: 0 Average: 0]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.