Get-RemoteServices

Remotely get services state and build a html report

<#
.SYNOPSIS
Get-RemoteServices - Remotely get services state and build a html report.
.NOTES
File Name : Get-RemoteServices.ps1
Author : Fabrice ZERROUKI - fabricezerrouki@hotmail.com
.EXAMPLE
PS D:\>Get-RemoteServices.ps1
#>
$date = (Get-Date)
$servers = Get-Content ".\servers.txt"
$i = 0

ForEach ($server in $servers)
{
$i = $i+1
Write-Progress -Activity "Collecting WMI data for $server. Please wait..." -Status "Progress:" -percentcomplete ($i/$servers.count*100)
$hw = Get-WmiObject Win32_ComputerSystem -ComputerName $server -ErrorAction SilentlyContinue
$hostname = $hw | % {$_.name}

$header = @"
$hostname - System Services - Configuration Report
"@

$tableHeader = @"
<table class="tableizer-table" width="100%">
<tbody>
<tr class="tableizer-firstrow">
<th colspan="6" align="center"><span style="color:#ffffff;font-size:small;"><strong>$hostname - System Services - Configuration Report - $date</strong></span></th>
</tr>
</tbody>
</table>
"@

$services = Get-WmiObject -Class Win32_Service -Computer $server | Sort-Object -Property DisplayName -ErrorAction SilentlyContinue

ForEach ($service in $services)
{
$displayname = $service | % {$_.DisplayName}
$name = $service | % {$_.Name}
$startmode_t = $service | % {$_.StartMode}
if ($startmode_t -eq "Auto") { $startmode = "<strong>$startmode_t</strong>" } else { $startmode = "$startmode_t" }
$startname = $service | % {$_.StartName}
$state_t = $service | % {$_.State}
if ($state_t -eq "Running") { $state = "<span style="color:green;">$state_t</span>" }
if ($state_t -eq "Stopped" -and $startmode_t -eq "Auto") { $state = "<span style="color:red;">$state_t</span>" }
if ($state_t -eq "Stopped" -and $startmode_t -eq "Disabled") { $state = "<span style="color:green;">$state_t</span>" }
if ($state_t -eq "Stopped" -and $startmode_t -eq "Manual") { $state = "<span style="color:orange;">$state_t</span>" }
if ($state_t -eq "Start Pending" -or $state_t -eq "Stop Pending" -or $state_t -eq "Continue Pending" -or $state_t -eq "Pause Pending") { $state = "<span style="color:orange;">$state_t</span>" }
if ($state_t -eq "Paused" -or $state_t -eq "Unknown") { $state = "<span style="color:orange;">$state_t</span>" }
if ($state_t -eq "Paused" -or $state_t -eq "Unknown" -and $startmode_t -eq "Auto") { $state = "<span style="color:red;">$state_t</span>" }
$status_t = $service | % {$_.Status}
if ($status_t -eq "OK" -or $status_t -eq "Degraded" -or $status_t -eq "Pred Fail") { $status = "<span style="color:green;">$status_t</span>" }
if ($status_t -eq "Error" -or $status_t -eq "Starting" -or $status_t -eq "Stopping" -or $status_t -eq "Service") { $status = "<span style="color:red;">$status_t</span>" }
if ($status_t -eq "Unknown") { $status = "<span style="color:orange;">$status_t</span>" }
$pathname = $service | % {$_.PathName}$html += @""@
}
$footer = "
<table class="tableizer-table" width="100%">
<tbody>
<tr class="tableizer-firstrow">
<th align="left" valign="top"><strong>Service Name</strong></th>
<th align="left" valign="top"><strong>Startup Type</strong></th>
<th align="left" valign="top"><strong>Log On As</strong></th>
<th align="left" valign="top"><strong>State</strong></th>
<th align="left" valign="top"><strong>Status</strong></th>
<th align="left" valign="top"><strong>Executable Path</strong></th>
</tr>
<tr>
<td align="left" valign="top">$displayname ($name)</td>
<td align="left" valign="top">$startmode</td>
<td align="left" valign="top">$startname</td>
<td align="left" valign="top">$state</td>
<td align="left" valign="top">$status</td>
<td align="left" valign="top">$pathname</td>
</tr>
</tbody>
</table>
<span style="font-size:xx-small;"><em>Report generated {0} by {1}\{2}</em></span>" -f (Get-Date),$env:userdomain,$env:username
$report += $tableHeader
$report += $html
$report += $footer

$outfile = "Services-" + "$server" + ".html"
ConvertTo-HTML -Body $report -Head $header | Out-File -FilePath $outfile -encoding UTF8
}

Leave a Reply

Scroll to Top