6

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..

$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
}
1
  • 2
    Use ConvertTo-Html. Commented Aug 17, 2016 at 8:50

1 Answer 1

5

Something like this should work.

Single table

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.

# 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 "<h2>$ComputerName</h2>" -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 (<table></table>), this should be removed.
$htmlDocument -replace '<table>\r?\n</table>' | Out-File C:\Dv\Report.html

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:

$HtmlHead = '<style>
    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;
    }
</style>'

# 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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Chris :) Its working.. can you tell me how to print Computer name on top of Table.
Are you wanting one table per computer (with the computer as a title)? If so, it has to change a little to make that feasible.
Yes Chris, Script will run for Multiple Servers so i need one table for each Server services and server name on top of table.
Got it. Editing post above, it's a bit longer. One tick.
Got it..Thanks Chris

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.