question

Adamster2-7707 avatar image
0 Votes"
Adamster2-7707 asked RichMatheisen-8856 commented

Help with PowerShell to Monitor Scripts

Hello,

I have the following script that Monitors the Windows Service and outputs on a webpage whether the service is running or not. I would like it to display as green if running and red if stopped or disabled. Any idea how I can achieve this? Thanks!

 $Var1 = get-service WindowsService1, WindowsService2, WindowsService3, WindowsService4  -ComputerName Service1,Service2 | ConvertTo-Html -Property Name, Status, MachineName| format-table Name,Status,Machinename -autosize
 $Var1 | Out-File C:\Script\Services.html



windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 commented

With the understanding that the use of "bgcolor" has been deprecated in favor of using CSS, here's a demonstration of doing what you asked:

 Get-Service AppInfo, AppIdSvc, AppInfo -ComputerName ws06 | 
     ConvertTo-Html -Property Name, Status, MachineName |
         ForEach-Object{
             $_ = $_ -replace '\<td\>Running\</td\>', '<td bgcolor="green">Running</td>'
             $_ = $_ -replace '\<td\>Stopped\</td\>', '<td bgcolor="red">Stopped</td>'
             $_
         } | Out-File c:\junk\x.html

FYI, saving the output for Format-Table to a variable hardly ever works the way you expect it to!

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This works, the only problem is that when I schedule the script to run every 10 minutes. The second time script runs, the webpage is blank and all is the title HTML Table in the browser. The first time it runs, I can actually see the webpage.

0 Votes 0 ·

If the file doesn't contain a HTML table the implication is that the Get-Service produced no output.

0 Votes 0 ·