Automatic Document & Report Generation (Part 3)

by Jul 16, 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

In the previous tips, we explained how you can generate dynamic tables. Today, we’d like to show how easy it is to adjust tables, and display arbitrary data. Let’s take the service list from our previous example:

# 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

With just some very small changes, the same code yields a list of AD users (provided you have the ActiveDirectory module and access to an AD):

# 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 'ADUser'  {
    # get 40 AD user to illustrate
    $user = Get-ADUser -Filter * -ResultSetSize 40 -Properties mail | 
        Select-Object -Property Name, mail, SID

    Paragraph -Style Heading1 "AD User Liste"
    
    # generate a table with one line per user
    $user | 
        # select the properties to display, and the header texts to use
        Table -Columns Name, mail, SID -Headers 'Employee','Email','SID' -Width 0
    
} | 
Export-Document -Path $OutPath -Format Word

# open the generated documents
explorer $OutPath 

Twitter This Tip! ReTweet this Tip!