Friday, December 16, 2016

How To Generate Services HTML Report - PowerShell

Overview

On Windows platform, listing services with windows PowerShell is easier than earlier. PowerShell provides cmdlet to list services for both local and remote servers. In order to list services you need to utilize “Get-Service” cmdlet.

In this guide we will get the list of services into a HTML file and display with a customized CSS.

Applies To

Tested on Windows 10, Windows 2008 R2, Windows 2012.

Pre-Requisites

To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or “Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.

Policy Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.

Current Execution Policy


To know the current run the PowerShell cmdlet; Get-ExecutionPolicy

To list execution policies that can be configured run the PowerShell cmdlet; Get-ExecutionPolicy -List



Get Service HTML – Customized CSS


This script will list all the services configured on the server along with different status into a html format file and a custom CSS associated to the html;

In custom CSS on mouse over, service information row is highlighted with different color.

PowerShell Snippet – Get Service Info Custom CSS


ConvertTo-HTML cmdlet argument "-CssUri" is passed with customized style sheet file "c:\temp\my.css"

#
# Declare variable for Output file
#
$GetServiceStatus = Join-Path "c:\temp\"GetServiceStatus.html
# 
# Associate CSS for Output file
#
Get-Service | Select-Object Status, Name, DisplayName | ConvertTo-Html -CssUri C:\Temp\my.css | Out-File $GetServiceStatus
#
# Open File
#
Invoke-Expression $GetServiceStatus 

Customized CSS - Snippet


Below Style sheet is utilized for mouse hovering and color changing on each row;copy the below css into a file.

/* Table Border and Padding setting */
table{
	border-collapse:collapse;
	padding:5px;
}

/* Table Odd and Even Row Color definition */
tr:nth-clild(even) {
	background:aqua;}
tr:nth-clild(odd){
background:green}

/* Table Header, data Color definition */
th,td {padding:5px;}
th{
background:maroon;
color:white;
}

/* Table Row hovering definition */
tr:hover{
	color:red;
	background:yellow;
	font-weight:bold;
}

/* Table Row transition definition */
tr{
	-webkit-trasition: color 1s ease;
	-ms-transition: color 1s ease;
	-moz-transition: color 1s ease;
	-o-transition: color 1s ease;
}

PowerShell Snippet – Result


Upon running the above PowerShell script; script will launch the webpage in the default browser as shown below;


PowerShell Snippet – Get Service Info Sorting


The below script will get services sorted based on Status in descending order;


#
# Declare variable for Output file
#
$GetServiceStatus = Join-Path "c:\temp\"GetServiceStatus.html
# 
# Associate CSS for Output file, sort by Status
#
Get-Service | Select-Object Status, Name, DisplayName | Sort-Object Status -Descending | ConvertTo-Html -CssUri C:\Temp\my.css | Out-File $GetServiceStatus
#
# Launch File
#
Invoke-Expression $GetServiceStatus 

PowerShell Snippet – Result


Upon running the above PowerShell script; script will launch the webpage in the default browser as shown below; listed based on current service status.




Slideshare Information

Step by step guide with screenshots is uploaded to slideshare.


No comments:

Post a Comment