(Auto Refresh Using JS 🙂 ) Let’s make it simple using PSHTML
In one of my blog post I demoed about auto refresh HTML using JavaScript. Now, let me show how to replicate the same using PSHTML without JavaScript and for this I used Polaris as a web framework. In short, we need to use the META tag in the HTML header with the content value as integer (10 = 10 seconds) and give value for httpequiv attribute as REFRESH.
meta -httpequiv "refresh" -content "10"
Very familiar right? So, now we use the same snippet in PowerShell with the help of PSHTML.
head { Title "Auto Refresh" meta -httpequiv "refresh" -content "10" }
That’s it – rest all story of yours! Check the code here which retrieves windows services and refresh the page every 10 seconds.
New-PolarisGetRoute -Path "/Service" -Scriptblock { $HTML = html { head { Title "Auto Refresh" meta -httpequiv "refresh" -content "10" } body { hr h1 "Get-Service" hr Table -ChildItem { tr -Content { Th -Content "Name" Th -Content "Status" } tr -Content { foreach ($Service in (Get-Service | Select -First 10)) { tr -Content { td -Content { $Service.Name } if ($Service.Status -eq "Running") { td -Content { $Service.Status } -Style "color:GREEN" } else { td -Content { $Service.Status } -Style "color:RED" } } } } -Id "customers" -Attributes @{"border"="1"} } } -Style "font-family:Candara" } $Response.SetContentType('text/html') $Response.Send($HTML) }
Here is the Server.ps1
Using module Polaris .\routes\home.ps1 .\routes\Service.ps1 Start-Polaris -Port 8080
What we get? A Page with Service status and it get refreshed in 10 seconds interval (If ALG service starts it turns red with test Running).