Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Creating an HTML report


In this recipe, we will create an HTML report based on the system's services.

How to do it...

These are the steps required to create an HTML report using PowerShell:

  1. Open PowerShell ISE as an administrator.

  2. Add the following script and run it:

    #simple CSS Style
    $style = @"
    <style type='text/css'>
      td {border:1px solid gray;} 
      .stopped{background-color: #E01B1B;}
    </style>
    "@ 
    
    #let's get content from Get-Service
    #and output this to a styled HTML 
    Get-Service | 
    ConvertTo-Html -Property Name, Status -Head $style | 
    Foreach-Object { 
        #if service is running, use green background
        if ($_ -like "*<td>Stopped</td>*") 
        {
          $_ -replace "<tr>", "<tr class='stopped'>"
        } 
        else 
        { 
          #display normally
          $_ 
        } 
      } | 
    Out-File "C:\Temp\sample.html" -force
    
    #open the page in Internet Explorer
    Set-Alias ie "$env:programfiles\Internet Explorer\iexplore.exe"
    ie "C:\Temp\sample.html"

The following screenshot shows...