I am new to PowerShell and was working on a script to grab the state of services on remote servers and produce an HTML report that displays ones that have a State of stopped and a StartMode of Auto. It works and does create an HTML page.
The problem is that is seems to create a separate HTML table for each server, so I end up with the column headers for every server and since they are treated as separate tables the formatting is all off.
I'd like to have just one table with ONE header row at the top followed by the information on each server below that. I want the entire report to be lined up.
Any help would be great. I'd like to use this as a template for other reports if I can get the output the way I want. Below is the script and the HTML output I'm getting.
Thanks!
$Style = @"<style>
TABLE {width:1200px;border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}</style>"@
$ServerList = Get-Content -path C:\files\pshell\commands\computers1.txt
$serviceList = &{$args} "StartMode='Auto' AND State='Stopped' and (Name like 'MED%' or Name like 'ANP%')"
$body = $ServerList | foreach { (Get-WmiObject win32_service -ComputerName $_ -Filter "$serviceList" ) |
select-object Systemname, Name, StartMode, State | ConvertTo-HTML -Fragment}<# possible values
[enum]::GetValues([System.ServiceProcess.ServiceControllerStatus])
Stopped
StartPending
StopPending
Running
ContinuePending
PausePending
Paused
the field tag looks like this <td>Running</td> we need it to look like this <td bgcolor="#00ff00">Running</td>
#>
$colorTagTable = @{Stopped = ' bgcolor="#ff0000">Stopped<';
Running = ' bgcolor="#00ff00">Running<'}
#get possible values look them in text sorrounded by > < and replace them with style (pun intended).
$colorTagTable.Keys | foreach { $body = $body -replace ">$_<",($colorTagTable.$_) }
ConvertTo-HTML -head $Style -body "<H2>Validation Completed</H2> $body" |
Out-File "C:\files\pshell\Scripts\srv.htm"