Automatic Document & Report Generation (Part 2)

by Jul 13, 2018

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

Today, we are going to generate a document with dynamic table content:

# 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 'ServiceReport'  {
    # 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)
    $services = Get-Service | Select-Object -Property DisplayName, Status, StartType

    Paragraph -Style Heading1 "System Inventory for $env:computername"
    Paragraph -Style Heading2 "Services ($($services.Count) Services found):"
    
    # generate a table with one line per service
    $services | 
        # select the properties to display, and the header texts to use
        Table -Columns DisplayName, Status, StartType -Headers 'Service Name','Current State','Startup Type' -Width 0
    
} | 
Export-Document -Path $OutPath -Format Word,Html,Text

# open the generated documents
explorer $OutPath 

When you run this code, it yields three files called ServiceReport.docx/html/txt. As you see, the report contains your service list as a table.

Twitter This Tip! ReTweet this Tip!