Output to a web browser? But why?
I was fishing around in the PowerShell Studio Snippets and a ran across one Snippet:called Trace.
I wanted to use this snippet to upgrade the output of my troubleshooting tool, ServerPulse. An update of a script that I originally wrote to give text output. With this connection to Internet Explorer, I could create something that was a bit easier on the eyes and stop the junior engineers from puckering when they looked at it. ServerPulse was a text-based self-updating dashboard which I thought was rather functional. however, I could see a bit of aesthetics to making it browser output. I think there is a lot of possibilities with formatting the output as HTML and then it could be delivered by file, email, or to a web browser.
This update would give me time to rethink what and how I was gathering the data. Keeping the live data refreshing feature of the troubleshooting tool would be important.
I did some tweaking on the base snippet.
I played with this on my workstation, and when I tried it on a server I got some weird errors.
Problem is this requires a certain DLL to function - "Microsoft.mshtml.dll"
It may not be loaded on a server or workstation in some conditions. I have read that MS office installs it. Here is a more detailed article in how to add it if you get the error "Property 'innerhtml' cannot be found on this object; make sure it exists and is settable."
Check out - http://www.dlldownloader.com/microsoft-mshtml-dll/ with a detailed explanation of the problem.
After fixing that, I was able to use it any computer that had the dll, I read that the Microsoft Office suite added this dll, which was the cause of the initial confusion with the server version, Which had no reason for it to have MS office installed.
So I created the following function from the snippet to post things to the Web page. You can repeatedly call this function to update information if you want to refresh or expand on the output. The flexibility of the Web browser interface is almost without limit.
But... You have to feed it HTML.
Besides hello world, I thought a basic clock would be in order.
Clock code to HTML
A little adjustment to the function like so:
And when feed the output the clock code to the function it pops up a window like this:
" Out-IE ($report) "
IE WINDOW:
Hard to look at and kind of cryptic. But re-using some old MS script example, I can append a block of formatting to make it a little prettier.
* NOTE: If your playing with this on the Shell prompt, Make sure you get the $IE = ""In there someplace between sessions or you won't get a new window... which was confusing at first for me.
And this is a feature for being able to update the data.
The Refined IE output portion now looks like:
Now some more example of use.
Building on that code I added another table and gleaned some additional info from the local system. Then you can loop the code to keep it updating the output on IE. The next step was to add some more useful info: And I added some more useful info. An example finished script:
Output of this looks like:

And this version updates the info on the screen, you can see the values change, and the clock ticking.
I was fishing around in the PowerShell Studio Snippets and a ran across one Snippet:called Trace.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ie = "" # Clear object connection from previous run, | |
function Out-IE ([string]$text) | |
{ | |
if ($ie -eq "") | |
{ | |
$script:ie = New-Object -comobject "InternetExplorer.Application" | |
$script:ie.navigate("about:blank") | |
$script:ie.top = 10 | |
$script:ie.left = 10 | |
$script:ie.height = 400 | |
$script:ie.width = 600 | |
$script:ie.toolbar = $false | |
$script:ie.visible = $true | |
} | |
# if window is closed, exit out of loop calling this. Break does not close pswindow. | |
else | |
{ | |
$body = $script:ie.document.body.innerhtml | |
If (!$body) { Break } | |
} | |
$body = "$text<br>" | |
$script:ie.document.body.innerhtml = $body | |
} |
This update would give me time to rethink what and how I was gathering the data. Keeping the live data refreshing feature of the troubleshooting tool would be important.
I did some tweaking on the base snippet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ie = "" | |
function Trace([string]$text) { | |
if ($ie -eq "") { | |
$script:ie = New-Object -comobject "InternetExplorer.Application" | |
$script:ie.navigate("about:blank") | |
$script:ie.top = 10 | |
$script:ie.left = 10 | |
$script:ie.height = 400 | |
$script:ie.width = 600 | |
$script:ie.toolbar = $false | |
$script:ie.visible = $true | |
} | |
# if window is closed, exit out of loop calling this. Break does not close pswindow. | |
else | |
{ | |
$body = $script:ie.document.body.innerhtml | |
If (!$body) { Break } | |
} | |
$body = "$text<br>" | |
$script:ie.document.body.innerhtml = $body | |
} |
Problem is this requires a certain DLL to function - "Microsoft.mshtml.dll"
It may not be loaded on a server or workstation in some conditions. I have read that MS office installs it. Here is a more detailed article in how to add it if you get the error "Property 'innerhtml' cannot be found on this object; make sure it exists and is settable."
Check out - http://www.dlldownloader.com/microsoft-mshtml-dll/ with a detailed explanation of the problem.
After fixing that, I was able to use it any computer that had the dll, I read that the Microsoft Office suite added this dll, which was the cause of the initial confusion with the server version, Which had no reason for it to have MS office installed.
So I created the following function from the snippet to post things to the Web page. You can repeatedly call this function to update information if you want to refresh or expand on the output. The flexibility of the Web browser interface is almost without limit.
But... You have to feed it HTML.
Besides hello world, I thought a basic clock would be in order.
Clock code to HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$report = Get-Date | | |
select @{ Name = "Date"; Expression = { ($_.ToShortDateString()) } }, | |
kind, | |
@{ Name = "Time"; Expression = { ($_.ToLongTimeString()) } }, | |
@{ Name = "DST"; Expression = { $_.IsDaylightSavingTime() } }, | |
@{ Name = "UTC"; Expression = { $_.ToUniversalTime() } } | | |
ConvertTo-Html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ie = "" | |
function Trace([string]$text) { | |
if ($ie -eq "") { | |
$script:ie = New-Object -comobject "InternetExplorer.Application" | |
$script:ie.navigate("about:blank") | |
$script:ie.top = 10 | |
$script:ie.left = 10 | |
$script:ie.height = 400 | |
$script:ie.width = 600 | |
$script:ie.toolbar = $false | |
$script:ie.visible = $true | |
} | |
# if window is closed, exit out of loop calling this. Break does not close pswindow. | |
else | |
{ | |
$body = $script:ie.document.body.innerhtml | |
If (!$body) { Break } | |
} | |
$body = "$text<br>" | |
$script:ie.document.body.innerhtml = $body | |
} |
" Out-IE ($report) "
IE WINDOW:
Hard to look at and kind of cryptic. But re-using some old MS script example, I can append a block of formatting to make it a little prettier.
* NOTE: If your playing with this on the Shell prompt, Make sure you get the $IE = ""In there someplace between sessions or you won't get a new window... which was confusing at first for me.
And this is a feature for being able to update the data.
The Refined IE output portion now looks like:
Now some more example of use.
Building on that code I added another table and gleaned some additional info from the local system. Then you can loop the code to keep it updating the output on IE. The next step was to add some more useful info: And I added some more useful info. An example finished script:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.NOTES | |
=========================================================================== | |
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.124 | |
Created on: 1/6/2017 5:09 PM | |
Created by: Rich Stoddart | |
=========================================================================== | |
.DESCRIPTION | |
A demonstration of output to IE. | |
#> | |
$ie = "" # Clear object connection from previous run, | |
function Out-IE ([string]$text) | |
{ | |
if ($ie -eq "") | |
{ | |
$script:ie = New-Object -comobject "InternetExplorer.Application" | |
$script:ie.navigate("about:blank") | |
$script:ie.top = 10 | |
$script:ie.left = 10 | |
$script:ie.height = 400 | |
$script:ie.width = 600 | |
$script:ie.toolbar = $false | |
$script:ie.visible = $true | |
} | |
# if window is closed, exit out of loop calling this. Break does not close pswindow. | |
else | |
{ | |
$body = $script:ie.document.body.innerhtml | |
If (!$body) { Break } | |
} | |
$body = "$text<br>" | |
$script:ie.document.body.innerhtml = $body | |
} | |
$H = "<html> | |
<title>Computer: $env:COMPUTERNAME $env:USERDNSDOMAIN </title> | |
<style> | |
BODY{background: # CAD3CC; font-family: Arial; font-size: 8pt;} | |
H1{font-size: 32px;} | |
H2{font-size: 20px;} | |
H3{font-size: 24px;} | |
H3.Pass{color: green} | |
H3.Fail{color: red} | |
TABLE{border: 1px solid black; border-collapse: collapse; font-size: 8pt;} | |
TH{border: 1px solid black; background: #dddddd; padding: 5px; color: #000000;} | |
TD{border: 1px solid black; padding: 5px; } | |
td.Pass{background: #7FFF00;} | |
td.warn{background: #FFE600;} | |
td.Fail{background: #FF0000; color: #ffffff;} | |
</style> | |
" | |
$x = 100 | |
while ($X) | |
{ | |
$SysInfo = Get-WmiObject Win32_OperatingSystem #https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx | |
$ProcrInfo = Get-WmiObject Win32_Processor #https://msdn.microsoft.com/en-us/library/aa394373(VS.85).aspx | |
$DiskValue = Get-WmiObject -Class Win32_logicaldisk | where-Object { $_.DriveType -eq 3 } | |
[String]$RebootPending = "" | |
if (Get-Service -Name CcmExec -ErrorAction SilentlyContinue) | |
{ | |
$CCMClientSDK = Invoke-WmiMethod -NameSpace 'ROOT\ccm\ClientSDK' -Class 'CCM_ClientUtilities' -Name 'DetermineIfRebootPending' -ErrorAction SilentlyContinue | |
If ($CCMClientSDK.IsHardRebootPending) { $RebootPending += "CCM-HRP " } | |
If ($CCMClientSDK.RebootPending) { $RebootPending += "CCM-RP " } | |
} #end IF CcmExec | |
IF (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") { $RebootPending += "AU " } | |
IF (Test-Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations") { $RebootPending += "PFR" } | |
IF (!$RebootPending) { $RebootPending = $false } | |
$obj = New-Object -TypeName PSObject -Property ( | |
@{ | |
"CPU-Load-%" = "$($ProcrInfo.LoadPercentage)%" | |
"MemoryUtilized" = "$(([math]::Round(((($SysInfo.TotalVisibleMemorySize - $Sysinfo.FreePhysicalMemory)/$SysInfo.TotalVisibleMemorySize) * 100), 1))) %" | |
"Uptime" = "$([math]::Round(((Get-Date) - ($SysInfo.ConvertToDateTime($SysInfo.lastbootuptime))).totalhours, 1)) Hrs" | |
"Reboot Pending" = $RebootPending; | |
"Disk Space" = ($DiskValue | foreach { "$($_.Name) $(([math]::Round((($_.freespace/$_.size) * 100), 2)))%" }).ToString(); | |
}) | |
$report = Get-Date | | |
select @{ Name = "Date"; Expression = { ($_.ToShortDateString()) } }, | |
kind, | |
@{ Name = "Time"; Expression = { ($_.ToLongTimeString()) } }, | |
@{ Name = "DST"; Expression = { $_.IsDaylightSavingTime() } }, | |
@{ Name = "UTC"; Expression = { $_.ToUniversalTime() } } | | |
ConvertTo-Html -Fragment | |
$report += "<HR><BR>" | |
$report += $obj | ConvertTo-Html -Fragment -As List | |
out-IE (ConvertTo-Html -Head $H -Body $report) | |
Start-Sleep -Milliseconds 500 | |
$x-- | |
Clear-Variable ProcrInfo, SysInfo, DiskValue | |
[System.GC]::Collect() | |
} | |
And this version updates the info on the screen, you can see the values change, and the clock ticking.
No comments:
Post a Comment