Documentation with PlatyPS

If you want to seamlessly integrate documentation into your code, the PowerShell module PlatyPS might be helpful. We will revisit it when we create our own module release pipeline. PlatyPS allows you to create markdown-based help for your code as well as the generic about topics:

# The PlatyPS module makes generating help a breeze
Set-Location .Ch10
Install-Module PlatyPS -Force -Scope CurrentUser

# If you want, review the module code first
Get-Content .VoiceCommandsVoiceCommands.psd1
Get-Content .VoiceCommandsVoiceCommands.psm1

# For an existing module, generate help
# WithModulePage generates an additional landing page
Import-Module .VoiceCommands
$param = @{
Module = 'VoiceCommands'
WithModulePage = $true
OutputFolder = '.MarkdownHelp'
}
New-MarkdownHelp @param

# The generated help content can be extended
psedit .MarkdownHelpOut-Voice.md

# After each commit to a specific branch
# or as a regular task, the help can be updated
# Existing documentation will be kept intact
Update-MarkdownHelp -Path .MarkdownHelp

# As a build task, you might want to generate the
# MAML help
New-ExternalHelp -Path .MarkdownHelp -OutputPath .VoiceCommandsen-us

The following screenshot shows VSCode with a live preview of the markdown help file generated for the Out-Voice cmdlet that you created in the previous code sample:

The generated markdown files render beautifully and can be used with GitHub pages or any other service capable of rendering markdown. Additionally, you can export this markdown help to a cab file, which can be used with Update-Help, and an XML file, which can be packaged with your module.

Once the cabinet and XML files have been generated, you can upload them to any web server. Update-Help will:

  1. Examine your module manifest and locate the HelpInfoURI link, if there is one
  2. Try to resolve the URL by appending the module name and GUID, like so:
    http://myserver/MyModule_0b96dad8-f577-4a12-81d8-1db58ee0c87b_HelpInfo.xml
  3. Try to resolve the URI in the XML  HelpContentURI node in your help info file and expect a file listing
  4. Try to download the: http://myserver/MyModule_0b96dad8-f577-4a12-81d8-1db58ee0c87b_en-us.cab file

The following example will create a small lab environment with one web server and show you how to publish help content to support automatic updates for your own modules:

# In order to create a little lab environment we use AutomatedLab
if (-not (Get-Module AutomatedLab -List))
{
Install-Module AutomatedLab -Force -AllowClobber
}

# Create new lab definition
New-LabDefinition SimpleWebServer -DefaultVirtualizationEngine HyperV

# Add lab machines - make sure that Get-LabAvailableOperatingSystem returns something
Add-LabMachineDefinition -Name PACKTIIS -Roles WebServer -OperatingSystem 'Windows Server 2016 Datacenter'

Install-Lab

Invoke-LabCommand -ComputerName PACKTIIS -ActivityName ConfigureWebsite -ScriptBlock {
Set-WebConfigurationProperty -filter /system.webServer/directoryBrowse -name enabled -PSPath 'IIS:SitesDefault Web Site' -Value $true -Force
[void] (New-Item -ItemType Directory -Path C:inetpubwwwroothelpfiles)
New-SmbShare -Name helpshare -Path C:inetpubwwwroothelpfiles -FullAccess "Everyone","Guests","Anonymous Logon"
}

# After generating the external help, you need to host it
# We need to change our FWLink first.
$moduleGuid = (Get-Module VoiceCommands).Guid.Guid
[void] (New-PSDrive -Name help -PSProvider FileSystem -Root \PACKTIIShelpshare -Credential (new-object pscredential('Administrator',('Somepass1' | ConvertTo-SecureString -AsPlain -Force))))
$helpshare = 'help:'
$helpUri = 'http://PACKTIIS/helpfiles'

(Get-Content .VoiceCommandsVoiceCommands.psd1 -Raw) -replace 'HelpInfoURI = .*',"HelpInfoUri = '$helpUri'" | Out-File .VoiceCommandsVoiceCommands.psd1 -Force
(Get-Content .MarkdownHelpVoiceCommands.md -Raw) -replace '{{Please enter FwLink manually}}', $helpUri | Out-File .MarkdownHelpVoiceCommands.md -Force
(Get-Content .MarkdownHelpVoiceCommands.md -Raw) -replace '{{Please enter version .* format}}', '1.0.0.0' | Out-File .MarkdownHelpVoiceCommands.md -Force

$helpParam = @{
CabFilesFolder = '.VoiceCommandsen-us'
LandingPagePath = '.MarkdownHelpVoiceCommands.md'
OutputFolder = $helpshare
}
New-ExternalHelpCab @helpParam

# Update-Help will now download from your internal URL
# The verbose settings will show which URLs are being resolved and which files are used
Update-Help -Module VoiceCommands -Verbose

Apart from the hosted help files on a web server, you can add additional help links to your comment-based or file-based help by adding them as a related links. Now your user can use the -Online parameter with Get-Help and will be redirected to your internal support knowledge base or document management solution:

# Providing online help is also important
# Online links are provided on a cmdlet-basis
$link = 'http://lmgtfy.com/?s=d&q=Supporting+Online+Help'
(Get-Content .MarkdownHelpOut-Voice.md -Raw) -replace 'online version:.*',"online version: $link"
New-ExternalHelp -Path .MarkdownHelp -OutputPath .VoiceCommandsen-us -Force

Remove-Module VoiceCommands -ErrorAction SilentlyContinue
Import-Module .VoiceCommands

Get-Help Out-Voice -Online # :-)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.35.81