I'm trying to generate the report using PowerShell for list of services in list of computers. I'm able to generate the report but it was not what i expected.
Here is my code:
param ([string[]] $ServerPath,
[string[]] $ServicePath,
[string] $ENVName,
[string] $DataPath,
$csv=@()
)
$ServiceReport= "D:\Test\ServiceReport.htm"
New-Item -ItemType file $ServiceReport -Force
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($ServiceReport)
$date = ( get-date ).ToString('yyyy/MM/dd')
Add-Content $ServiceReport "<html>"
Add-Content $ServiceReport "<head>"
Add-Content $ServiceReport "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $ServiceReport '<title>Service Status Report </title>'
add-content $ServiceReport '<STYLE TYPE="text/css">'
add-content $ServiceReport "<!--"
add-content $ServiceReport "td {"
add-content $ServiceReport "font-family: Tahoma;"
add-content $ServiceReport "font-size: 11px;"
add-content $ServiceReport "border-top: 1px solid #999999;"
add-content $ServiceReport "border-right: 1px solid #999999;"
add-content $ServiceReport "border-bottom: 1px solid #999999;"
add-content $ServiceReport "border-left: 1px solid #999999;"
add-content $ServiceReport "padding-top: 0px;"
add-content $ServiceReport "padding-right: 0px;"
add-content $ServiceReport "padding-bottom: 0px;"
add-content $ServiceReport "padding-left: 0px;"
add-content $ServiceReport "}"
add-content $ServiceReport "body {"
add-content $ServiceReport "margin-left: 5px;"
add-content $ServiceReport "margin-top: 5px;"
add-content $ServiceReport "margin-right: 0px;"
add-content $ServiceReport "margin-bottom: 10px;"
add-content $ServiceReport ""
add-content $ServiceReport "table {"
add-content $ServiceReport "border: thin solid #000000;"
add-content $ServiceReport "}"
add-content $ServiceReport "-->"
add-content $ServiceReport "</style>"
Add-Content $ServiceReport "</head>"
Add-Content $ServiceReport "<body>"
add-content $ServiceReport "<table width='100%'>"
add-content $ServiceReport "<tr bgcolor='#0000FF'>"
add-content $ServiceReport "<td colspan='4' height='25' align='center'>"
add-content $ServiceReport "<font face='tahoma' color='#7CFC00' size='4'><strong>Service Stauts Report - $date</strong></font>"
add-content $ServiceReport "</td>"
add-content $ServiceReport "</tr>"
add-content $ServiceReport "</table>"
}
# Function to write the HTML Header to the file
Function writeTableHeader
{
param($ServiceReport ,$ServerName)
Add-Content $ServiceReport "<tr bgcolor='#20B2AA'>"
Add-Content $ServiceReport "<td width='10%' align='center'><font face='tahoma' color='#7CFC00' size='4'>Service Name</font></td>"
Add-Content $ServiceReport "<td width='30%' align='center'><font face='tahoma' color='#7CFC00' size='4'>$ServerName</font></td>"
#Add-Content $ServiceReport "<td width='10%' align='center'><font face='tahoma' color='#7CFC00' size='4'>Status</font></td>"
Add-Content $ServiceReport "</tr>"
}
Function writeHtmlFooter
{
param($ServiceReport)
Add-Content $ServiceReport "</body>"
Add-Content $ServiceReport "</html>"
}
Function WriteServiceInfo
{
param($ServiceReport ,$Servicename,$Status)
Add-Content $ServiceReport "<tr bgcolor='#008B8B'>"
Add-Content $ServiceReport "<td bgcolor='#F0FFF0' align=left ><b><font face='tahoma' color='#003399' size='3'>$Servicename</font></td>"
#Add-Content $ServiceReport "<td bgcolor='#F0FFF0' align=left ><b><font face='tahoma' color='#003399' size='3'>$Servername</font></td>"
Add-Content $ServiceReport "<td bgcolor='#F0FFF0' align=center ><b><font face='tahoma' color='#003399' size='4'>$Status</font></td>"
Add-Content $ServiceReport "</tr>"
}
writeHtmlHeader $ServiceReport
Add-Content $ServiceReport "<table width='100%'><tbody>"
Add-Content $ServiceReport "<tr bgcolor='#008B8B'>"
Add-Content $ServiceReport "<td width='100%' align='center' colSpan=3><font face='tahoma' color='#003399' size='6'><strong> Service Status Report</strong></font></td>"
Add-Content $ServiceReport "</tr>"
writeTableHeader $ServiceReport
# import the CSV to get server list
$Serverlist = Import-Csv -path $ServerPath #| Where-Object {$_.IsActive -eq '1'}
$servers=$ServerList.ServerName
$ENV=$ServerList.ENV
# fetch the serverlist where service name match in the CSV
$ENVServer= (Import-Csv -path $ServerPath | Where-Object {$_.ENV -eq $ENVName}).ServerName
$ServicesList=(Import-Csv -Path $ServicePath).ServiceName
foreach($Server in $ENVServer){
$ServicesList=(Import-Csv -Path $ServicePath).ServiceName
ForEach($Service in $Serviceslist){
$servicestatus=Get-Service -ComputerName $Server | Where-Object {$_.ServiceName -like "$Service"}
$lenth=$servicestatus.length
#$state=$servicestatus.Status
if ($lenth -eq 0)
{
#do nothing
}
else {
writeTableHeader $ServiceReport $servicestatus.MachineName
WriteServiceInfo $ServiceReport $servicestatus.ServiceName $servicestatus.Status
}
}
}
Output:
Service Name Server Name Status
==================================
Service1 Server1 running
service2 server1 stopped
service1 server2 stoppped
Expected Output:
ServiceName Server1 server2 server3 .........
================================================
Service1 runing stopped running
ervice2 stopped running stopped
service3 running stopped stopped
...... ...... ..... .....
.......
Can you please help me if there is any chances like above. I've been trying but no luck from my side.
Thanks, Siva
You are looking for something like this.
uncomment the <#-ComputerName $Computer#> part to make it come alive ;) It was for testing on my own machine.
$Servicelist = @("WinRM", "Winmgmt", "DHCP")
$Computerlist = @("LocalHost", "Server1","Server2","Server3")
$ServiceStatus = @{}
Foreach ($Computer in $Computerlist)
{
$ServicesFound = Get-Service <#-ComputerName $Computer#> | Where-Object {$Servicelist -contains $_.Name} | Select Name, Status
Foreach ($Service in $ServicesFound)
{
If(!($ServiceStatus["$($Service.Name)"])) {
$ServiceStatus["$($Service.Name)"] = $Service | Select @{N='Service'; E={$($Service.Name)}}, @{N="$Computer"; E={$Service.Status}}
} else {
$ServiceStatus["$($Service.Name)"] = $ServiceStatus["$($Service.Name)"] | Select *, @{N="$Computer"; E={$Service.Status}}
}
}
}
$ServiceStatus.Values | ConvertTo-Html -Fragment
HTML Output
<table>
<colgroup><col/><col/><col/><col/><col/></colgroup>
<tr><th>Service</th><th>LocalHost</th><th>Server1</th><th>Server2</th><th>Server3</th></tr>
<tr><td>Dhcp</td><td>Running</td><td>Running</td><td>Running</td><td>Running</td></tr>
<tr><td>WinRM</td><td>Stopped</td><td>Stopped</td><td>Stopped</td><td>Stopped</td></tr>
<tr><td>Winmgmt</td><td>Running</td><td>Running</td><td>Running</td><td>Running</td></tr>
</table>
It looks like this
Service LocalHost Server1 Server2 Server3
=========================================
Dhcp Running Running Running Running
WinRM Stopped Stopped Stopped Stopped
Winmgmt Running Running Running Running