Question:
i want to know services stopped and which are set to automatic and output file goes to HTML page. In that HTML output i want to create table for stopped services on top of that table server name. Can someone tell me..
1 2 3 4 5 6 7 8 9 |
$ServerListFile = "C:\Dv\Server_List.txt" $ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue foreach ($Computername in $ServerList) { Write-Host "Automatic Services Stopped :" $Computername Get-wmiobject win32_service -computername $Computername -Filter "startmode = 'auto' AND state != 'running'" | Select DisplayName,Name,State,startmode | Format-Table -auto | Out-File C:\Dv\Report.html } |
Answer:
Something like this should work.
Single table
1 2 3 4 5 6 7 8 |
Get-Content "C:\Dv\Server_List.txt" -ErrorAction SilentlyContinue | ForEach-Object { Write-Host "Automatic Services Stopped :" $_ Get-WmiObject Win32_Service -ComputerName $_ -Filter "startmode = 'auto' AND state != 'running'" } | Select-Object DisplayName, Name, State, StartMode | ConvertTo-Html | Out-File C:\Dv\Report.html |
Multiple tables
This version creates a sequence of tables using ConvertTo-Html -Fragment
. Each table is preceded by a HTML header (h2 in the example) with the computer name. The individual tables are concatenated and merged into a single HTML document at the end using a bare (no input) call to ConvertTo-Html
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Generate the content which should be inserted as a set of HTML tables $preContent = Get-Content "C:\Dv\Server_List.txt" -ErrorAction SilentlyContinue | ForEach-Object { $ComputerName = $_ Write-Host "Automatic Services Stopped :" $ComputerName # A table (and heading) will only be generated for $ComputerName if there are services matching the filter. Get-WmiObject Win32_Service -ComputerName $ComputerName -Filter "startmode = 'auto' AND state != 'running'" | Select-Object DisplayName, Name, State, StartMode | ConvertTo-Html -PreContent " $ComputerName" -Fragment} # Generate the document which holds all of the individual tables. $htmlDocument = ConvertTo-Html -Head $htmlHead -PreContent $preContent | Out-String # Because the document has no input object it will have an empty table ( $htmlDocument -replace ' |
Styling
You’ll find the HTML generated by these to be pretty raw. One of the better ways to tackle that is to use CSS, here’s a fragment of mine that makes HTML tables look prettier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
$HtmlHead = ' body { background-color: white; font-family: "Calibri"; } table { border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse; width: 100%; } th { border-width: 1px; padding: 5px; border-style: solid; border-color: black; background-color: #98C6F3; } td { border-width: 1px; padding: 5px; border-style: solid; border-color: black; background-color: White; } tr { text-align: left; } ' # Use the Head parameter when calling ConvertTo-Html ... | ConvertTo-Html -Head $HtmlHead | ... |
Note: The Head only applies when the Fragment parameter is not supplied with ConvertTo-Html.