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
}
PowerShell includes some great capabilities for working with two common forms of structured data: HTML and XML.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With