Skip to content

Instantly share code, notes, and snippets.

@brianbunke
Created March 1, 2019 04:09
Show Gist options
  • Save brianbunke/ec8520eb4647c8bd7f547f9008ae9e1b to your computer and use it in GitHub Desktop.
Save brianbunke/ec8520eb4647c8bd7f547f9008ae9e1b to your computer and use it in GitHub Desktop.
WITP-20190301
# 2019-03-01
# Whatcom IT Professionals meetup
# https://www.meetup.com/Whatcom-IT-Professionals/events/259166239/
# Why and How to Get Started With Automated Documentation
function Get-TheComputerThings ([switch]$Again) {
[PSCustomObject]@{
Name = 'LTAlpha'
OS = 'Windows 10 Enterprise'
CPU = '2'
MemGB = '16'
IP = '172.19.20.21'
LastUpdate = '2019-02-28'
Uptime = '2019-02-28'
Created = '2018-09-15'
Owner = 'Sales'
BackupJob = '(none)'
}
[PSCustomObject]@{
Name = 'PCBravo'
OS = 'Windows 7 Professional'
CPU = '2'
MemGB = '8'
IP = '10.10.10.10'
LastUpdate = '2019-01-19'
Uptime = '2019-02-22'
Created = '2013-12-11'
Owner = 'Accounting'
BackupJob = '(none)'
}
[PSCustomObject]@{
Name = 'ServerCharlie'
OS = 'Windows Server 2016 Standard'
CPU = '4'
MemGB = '16'
IP = '192.168.0.1'
LastUpdate = If ($Again) {'2019-03-01'} Else {'2017-07-04'}
Uptime = If ($Again) {'2019-03-01'} Else {'2017-07-04'}
Created = '2016-07-04'
Owner = 'Facilities'
BackupJob = 'silver'
}
}
# Download the ConfluencePS module from the PowerShell Gallery
# Find-Module ConfluencePS
# Install-Module ConfluencePS
# Import the module, review the available commands, and use the help documentation
Import-Module ConfluencePS
Get-Command -Module ConfluencePS
Get-Help about_ConfluencePS
Get-Help Set-ConfluenceInfo -Examples
# Set your wiki's base address and valid credentials for this PowerShell session
Set-ConfluenceInfo -BaseURi 'https://brianbunke.atlassian.net/wiki/' -PromptCredentials
# Get the list of Confluence spaces to test the address/credentials
Get-ConfluenceSpace
# Create a new WITP space for this demo
New-ConfluenceSpace -SpaceKey WITP -Name 'Whatcom IT Professionals'
# Store the ID of the automatically created page in the new space in $Parent
Get-ConfluencePage -SpaceKey WITP -Title 'Whatcom IT Professionals Home'
$Parent = (Get-ConfluencePage -SpaceKey WITP -Title 'Whatcom IT Professionals Home').ID
$Parent
# Pretend you connected to your infrastructure and formatted computer details
Get-TheComputerThings
# How do those computers look when converted to Confluence table formatting?
ForEach ($Thing in Get-TheComputerThings) {
$Thing | Select-Object * -ExcludeProperty Name | ConvertTo-ConfluenceTable
}
# Create a brand new page in the demo WITP space for each computer
ForEach ($Thing in Get-TheComputerThings) {
$Table = $Thing | Select-Object * -ExcludeProperty Name | ConvertTo-ConfluenceTable
New-ConfluencePage -SpaceKey WITP -ParentID $Parent -Title $Thing.Name -Body $Table -Convert
}
# You have created a new space and new pages!
# For advanced tips on updating, arranging, and dynamic sortable tables, continue
# Create a new "Computer Inventory" blank page with the "Page Properties Report" macro
# Label = inventory
# Title column heading = Name
# "Computer Inventory" is now our new desired parent page. Store its ID
$Parent = (Get-ConfluencePage -SpaceKey WITP -Title 'Computer Inventory').ID
$Parent
# Confluence is a "structured wiki."
# Move your three pages underneath the new "Computer Inventory" parent page
ForEach ($Thing in Get-TheComputerThings) {
$PageID = (Get-ConfluencePage -SpaceKey WITP -Title $Thing.Name).ID
Set-ConfluencePage -PageID $PageID -ParentID $Parent
}
# You scan your infrastructure again and find some small changes
Get-TheComputerThings -Again
# Update your pages one more time
ForEach ($Thing in Get-TheComputerThings -Again) {
# Store the current $Thing's page ID
$PageID = (Get-ConfluencePage -SpaceKey WITP -Title $Thing.Name).ID
# Create $Thing's table without the name, as before
$Table = $Thing | Select-Object * -ExcludeProperty Name | ConvertTo-ConfluenceTable
# Insert "placeholder" lines above and below your table
$Body = 'Placeholder1' + "`n"
$Body += $Table | Out-String
$Body += 'Placeholder2' + "`n"
# Convert the body to Confluence's storage format (modified XHTML)
$Body = $Body | ConvertTo-ConfluenceStorageFormat
# After conversion, replace our placeholders (interpreted as text) with the "Page Properties" macro's real HTML
$Body = $Body -replace '<p>Placeholder1</p>','<ac:structured-macro ac:name="details" ac:schema-version="1" ac:macro-id="e6f8199d-806f-4024-9b80-4fe2806bf24c"><ac:rich-text-body>'
$Body = $Body -replace '<p>Placeholder2</p>','</ac:rich-text-body></ac:structured-macro>'
# For the current page, set the new body, then add the "inventory" label to be included in the Page Properties Report
Set-ConfluencePage -PageID $PageID -Body $Body
Add-ConfluenceLabel -PageID $PageID -Label 'inventory'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment