PowerShell

PSWriteWord – Version 0.5.1

During last couple of weeks I've spent most of my time developing PSWinDocumentation project. While building that I've came across different problems that needed solutions and consistent output. In this release 0.5.X I've addressed some of them and also built  what I call a building blocks. While not necessary a functionality of Word it allows me to easily built PSWinDocumentation with just one command.

PSWriteWord Information
Please notice this article contains parts of information (still useful) and may not reflect all functionalities of this module. For download, source code and so on you should refer to the dedicated PSWriteWord module page. After reading this one… of course! It contains useful informationexamples and know-how.
Building blocks

While I'm not sure it will please everyone or will it be functional outside of my personal need it's there for you to use. Keep in mind that this will be further enhanced, changed so may need some revisits later on. Right now there are 5 functions but you should really worry about one… New-WordBlock.

  • New-WordBlock
  • New-WordBlockList
  • New-WordBlockPageBreak
  • New-WordBlockParagraph
  • New-WordBlockTable
What is block

Block is combination of multiple Word elements that can be added or not. Seems weird I know… but it's essentially a combination of multiple elements that play well together. For example a TOC Element, a TEXT and TABLE after it is one building block. But that building block doesn't have to have a TOC Element. It can be just TEXT and TABLE. It can also be a TOC, TEXT,TABLE, EMPTY PARAGRAPH, PAGE BREAK and CHART as one building block. In other words if you pass data to function you will get it displayed, if you don't… it will not appear. This allows you to create Word Document (docx) with documentation of your environment in no time. You want Text and a Table great – pass two variables and it's all good. But wait.. there's more. If you use TextNoData element it will display alternative text to the one you defined in Text field if TableData, ListData turns up empty. This allows you to build general documentation without actually knowing what will the output be.

Can be a TOC element (or lack of it)
Can be a Text (or lack of it)
Can be a chart (or lack of it)
Can be a page break (or lack of it, or multiple of those) – just add 5 to have 5 page breaks. Before or After..

It generally looks like this in code. One command.. that can generate multiple, different type of blocks.

        $WordDocument | New-WordBlock `
            -TocGlobalDefinition $Section.TocGlobalDefinition`
            -TocGlobalTitle $Section.TocGlobalTitle `
            -TocGlobalSwitches $Section.TocGlobalSwitches `
            -TocGlobalRightTabPos $Section.TocGlobalRightTabPos `
            -TocEnable $Section.TocEnable `
            -TocText $TocText `
            -TocListLevel $Section.TocListLevel `
            -TocListItemType $Section.TocListItemType `
            -TocHeadingType $Section.TocHeadingType `
            -TableData $TableData `
            -TableDesign $Section.TableDesign `
            -TableTitleMerge $Section.TableTitleMerge `
            -TableTitleText $TableTitleText `
            -TableMaximumColumns $Section.TableMaximumColumns `
            -Text $Text `
            -TextNoData $TextNoData `
            -EmptyParagraphsBefore $Section.EmptyParagraphsBefore `
            -EmptyParagraphsAfter $Section.EmptyParagraphsAfter `
            -PageBreaksBefore $Section.PageBreaksBefore `
            -PageBreaksAfter $Section.PageBreaksAfter `
            -TextAlignment $Section.TextAlignment `
            -ListData $ListData `
            -ListType $Section.ListType `
            -ListTextEmpty $Section.ListTextEmpty `
            -ChartEnable $Section.ChartEnable `
            -ChartTitle $ChartTitle `
            -ChartKeys $ChartKeys `
            -ChartValues $ChartValues `
            -ListBuilderContent $ListBuilderContent `
            -ListBuilderType $Section.ListBuilderType `
            -ListBuilderLevel $Section.ListBuilderLevel
Block A

Block B

Block C

Block D

Block E

Block F

Block G

And those are just examples. Of course it takes a moment to get used to it but when it does…

Format-TransposeTable / Add-WordTable

Another addition is Format-TransposeTable function. Transpose (rotate) data is nicely described by Microsoft in their article here.

Transpose (rotate) data from rows to columns or vice versa

Now what the function does in PSWriteWord it allows you to easily convert any HashTable/OrderedDictionary to PSCustomObject and vice versa. How this is useful?

$myitems0 = @(
    [pscustomobject]@{name = "Joe"; age = 32; info = "Cat lover"},
    [pscustomobject]@{name = "Sue"; age = 29; info = "Dog lover"},
    [pscustomobject]@{name = "Jason another one"; age = 42; info = "Food lover"
    }
)
$myitems0 | Format-Table -AutoSize

$Transposed = Format-TransposeTable -Object $myitems0
$Transposed | Format-Table -AutoSize

$TransposedBack = Format-TransposeTable -Object $Transposed
$TransposedBack | Format-Table -AutoSize

And final output… (I know, I know it changed order… )

This allows you to easily create HashTables or OrderedDictionaries and still display them differently (which is useful for Word/Excel tables). This command is part of Add-WordTable with switch -Transpose

Import-Module PSWriteWord -Force
Import-Module ActiveDirectory

$FilePath = "$Env:USERPROFILE\Desktop\PSWriteWord-Example-Tables1.docx"

# Get basic AD information
$ADSnapshot = @{}
$ADSnapshot.RootDSE = $(
    $Info = Get-ADRootDSE
    $Info | Select-Object `
    @{label = 'Configuration Naming Context'; expression = { $_.configurationNamingContext }},
    defaultNamingContext, dnsHostName, domainControllerFunctionality, domainFunctionality,
    forestFunctionality, supportedLDAPPolicies, subschemaSubentry, supportedLDAPVersion, supportedSASLMechanisms
)
$ADSnapshot.ForestInformation = $(
    Get-ADForest | Select-Object DomainNamingMaster, Domains, ForestMode, Sites
)
$ADSnapshot.DomainInformation = $(Get-ADDomain)
# Get basic Ad information end

Clear-Host

$WordDocument = New-WordDocument $FilePath

Add-WordText -WordDocument $WordDocument -Text "Active Directory Root DSE with Transpose" `
    -FontSize 15 -CapsStyle smallCaps -Alignment both -Supress $True
Add-WordParagraph -WordDocument $WordDocument -Supress $True
Add-WordTable -WordDocument $WordDocument -DataTable $ADSnapshot.RootDSE `
    -Design LightShading -Bold $true -Color Blue -Supress $True -Transpose
Add-WordSection -WordDocument $WordDocument -PageBreak -Supress $True


Add-WordText -WordDocument $WordDocument -Text "Active Directory Root DSE without Transpose" `
    -FontSize 15 -CapsStyle smallCaps -Alignment both -Supress $True
Add-WordParagraph -WordDocument $WordDocument -Supress $True
Add-WordTable -WordDocument $WordDocument -DataTable $ADSnapshot.RootDSE `
    -Design LightShading -Bold $true -Color Blue -Supress $True
Add-WordSection -WordDocument $WordDocument -PageBreak -Supress $True


Add-WordText -WordDocument $WordDocument -Text "Active Directory ", 'Domain', ' Forest Information' `
    -FontSize 12, 12, 12 -StrikeThrough none, strike, none -Alignment center -Supress $True
Add-WordParagraph -WordDocument $WordDocument -Supress $True
Add-WordTable -WordDocument $WordDocument -DataTable $ADSnapshot.ForestInformation `
    -Design LightShading -Italic $true, $false -Bold $true, $false -ContinueFormatting -Supress $True -Verbose
Add-WordSection -WordDocument $WordDocument -PageBreak -Supress $True
Add-WordText -WordDocument $WordDocument -Text "Active Directory Domain Information" `
    -FontSize 15 -Color Green -Supress $True
Add-WordParagraph -WordDocument $WordDocument -Supress $True
Add-WordTable -WordDocument $WordDocument -DataTable $ADSnapshot.DomainInformation `
    -Design LightShading -Supress $True -Transpose
Add-WordPageBreak -WordDocument $WordDocument -Supress $True
Add-WordTable -WordDocument $WordDocument -DataTable $ADSnapshot.DomainInformation `
    -Design LightShading -Supress $True -Transpose `
    -ExcludeProperty 'ComputersContainer', 'AllowedDNSSuffixes', 'Item', 'LinkedGroupPolicyObjects' #-Verbose

Save-WordDocument $WordDocument -Language 'en-US' -Supress $True

### Start Word with file
Invoke-Item $FilePath

It's clearly visible on the next screenshot (left HashTable), right Transpose (and Format-TransposeTable behind scenes) in action…

As you can see thanks to this option I was able to put far more data into Word Table than I would without it.

Other features / changes

There were far more functionality changes… but it's been a while since I added them… so well I forgot 🙂 Enjoy this new release… and report bugs as always as there are surely some…

PSWriteWord Information
Please notice this article contains parts of information (still useful) and may not reflect all functionalities of this module. For download, source code and so on you should refer to the dedicated PSWriteWord module page. After reading this one… of course! It contains useful informationexamples and know-how.

This post was last modified on 22 sierpnia, 2018 08:59

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

2 miesiące ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

3 miesiące ago

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

7 miesięcy ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

12 miesięcy ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

1 rok ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

1 rok ago