Iain Brighton has created a free PowerShell module called „PScribo“ that can be used to easily create documents and reports in text, html, or Word format.

To use this module, simply run this command:

Install-Module -Name PScribo -Scope CurrentUser -Force

In the previous tips, we explained how you can generate dynamic tables. Today, find out how you can highlight individual table cells to indicate conditions such as configuration errors.

For this, we use Set-Style to apply formats to individual properties. The example below selects all objects where Starttype is „Automatic“ but Status is „Stopped“, and applies the style „HighlightedService“ to the property „Status“:

# https://github.com/iainbrighton/PScribo
# help about_document

# create a folder to store generated documents
$OutPath = "c:\temp\out"
$exists = Test-Path -Path $OutPath
if (!$exists) { $null = New-Item -Path $OutPath -ItemType Directory -Force }

# generate document
Document 'HighlightedReport'  {

    # get the service data to use:
    # generate the service information to use
    # (requires PowerShell 5 because prior to PowerShell 5, Get-Service does not supply
    # StartType - alternative: use Get-WmiObject -Class Win32_Service, and adjust
    # property names)

    # IMPORTANT: run this information through Select-Object to get a cloned copy
    # of the original objects so that style information can be appended
    $services = Get-Service | 
          Select-Object -Property DisplayName, Status, StartType | 
          Sort-Object -Property DisplayName

    # write heading
    Paragraph "Services ($($services.Count) Services found):"
    
    # define style to use for highlighting
    Style -Name HighlightedService -Color Red -BackgroundColor Yellow -Bold

    # apply a different format to cell "Status" for all objects where
    # status is "Stopped" and StartType is "Automatic"
    $services | 
        Where { $_.Status -eq 'Stopped' -and $_.StartType -eq 'Automatic'} | 
        Set-Style -Property Status -Style HighlightedService
    
    # create the table    
    $services | 
        Table -Columns DisplayName,Status,StartType -Headers 'Display Name','Status','Startup Type' -Tabs 1
    
    
} | 
Export-Document -Path $OutPath -Format Word,Html,Text

# open the generated documents
explorer $OutPath

Twitter This Tip! ReTweet this Tip!

Anonymous