Sql Services State Report with Colour Coding in Html file

I tried to look out in google regarding Sql Services State Report with Colour Coding where if the instance State is Running then it should indicate GREEN and if the instance state is Stopped then it should indicate with RED .

Today I have got the script by looking it into multiple sites and combined those scripts into one and finally got the output which is needed and it helps me to identify easily which instance is Running or Stopped.

Thanks for the original script writers for the script lines in PowerShell which makes DBA’s life easier , Here using CSS code for colour coding ,CIM Cmdlets to get the all Sql Services State then converting to HTML code in a table and it is stored in a Variable. The reports will be gathered and combined into a Single HTML Report ,finally the output of the report is generated to an HTML file which will be stored in physical Location from the Directory where we are executing.

Find the Script below :

#CSS code
$header = @"
<style>

    h1 {

        font-family: Arial, Helvetica, sans-serif;
        color: #e68a00;
        font-size: 28px;

    }

    
    h2 {

        font-family: Arial, Helvetica, sans-serif;
        color: #000099;
        font-size: 16px;

    }

    
    
   table {
		font-size: 12px;
		border: 0px; 
		font-family: Arial, Helvetica, sans-serif;
	} 
	
    td {
		padding: 4px;
		margin: 0px;
		border: 0;
	}
	
    th {
        background: #395870;
        background: linear-gradient(#49708f, #293f50);
        color: #fff;
        font-size: 11px;
        text-transform: uppercase;
        padding: 10px 15px;
        vertical-align: middle;
	}

    tbody tr:nth-child(even) {
        background: #f0f0f2;
    }
    


    #CreationDate {

        font-family: Arial, Helvetica, sans-serif;
        color: #ff3300;
        font-size: 12px;

    }



    .StopStatus {

        color: #ff0000;
    }
    
  
    .RunningStatus {

        color: #008000;
    }




</style>
"@

#The command below will get the name of the computer
$ComputerName = "<h1>Computer name: $env:computername</h1>"

#The command below will get SQL services information, convert the result to HTML code as table and store it to a variable
$ServicesInfo = Get-CimInstance -ComputerName DESKTOP-02JIB76 -ClassName Win32_Service -Filter "Name like '%sql%'"  |ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "<h2>Services Information</h2>"
$ServicesInfo = $ServicesInfo -replace '<td>Running</td>','<td class="RunningStatus">Running</td>'
$ServicesInfo = $ServicesInfo -replace '<td>Stopped</td>','<td class="StopStatus">Stopped</td>'

  
#The command below will combine all the information gathered into a single HTML report
$Report = ConvertTo-HTML -Body "$ComputerName $ServicesInfo" -Head $header -Title "Computer Information Report" -PostContent "<p id='CreationDate'>Creation Date: $(Get-Date)</p>"

#The command below will generate the report to an HTML file
$Report | Out-File .\sql-Services-Status-Report.html

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.