|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Creates Markdown files from PowerShell Get-Help output |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | +Creates Markdown files from PowerShell Get-Help output |
| 7 | +
|
| 8 | +Helper Tool to create GitHub Markdown files from PS1 scripts. |
| 9 | +Will check for all PS1 scripts in a given folder and try to create a markdown file. |
| 10 | +If flowcharts are available integrates those into the markdown. |
| 11 | +Since this is a helper script for the repos documentation it has limitations on usage |
| 12 | +
|
| 13 | +.EXAMPLE |
| 14 | +.\CreateMarkdown-FromPS1.ps1 -dir ..\Deploy -savepath .\Deploy |
| 15 | +
|
| 16 | +.EXAMPLE |
| 17 | +.\CreateMarkdown-FromPS1.ps1 -dir . -savepath . |
| 18 | +
|
| 19 | +.INPUTS |
| 20 | +None. You cannot pipe objects. |
| 21 | +
|
| 22 | +.OUTPUTS |
| 23 | +None. |
| 24 | +
|
| 25 | +#> |
| 26 | +param( |
| 27 | + # Directory with PS1 scripts |
| 28 | + [Parameter(Mandatory=$True,HelpMessage="Directory with PS1 scripts")] |
| 29 | + [string] $dir, |
| 30 | + |
| 31 | + # Markdown file directory |
| 32 | + [Parameter(HelpMessage="Markdown file directory")] |
| 33 | + [string] $savepath = "." |
| 34 | +) |
| 35 | + |
| 36 | +function Get-ParameterMarkdownTable { |
| 37 | + param ( |
| 38 | + # Get-Help Object |
| 39 | + [Parameter(Mandatory=$True,HelpMessage="Get-Help Object")] |
| 40 | + [object] $help |
| 41 | + ) |
| 42 | + |
| 43 | + $result = "| Name | Type | Required | Default | Description |`n| - | - | - | - | - |" |
| 44 | + |
| 45 | + #Write-Host $help |
| 46 | + |
| 47 | + $help.parameters[0].parameter.foreach({ |
| 48 | + #Write-Host "`nParameter: $_" |
| 49 | + # try parse default from description |
| 50 | + $description = $_.description.text |
| 51 | + $default = "" |
| 52 | + if(($_.description.text -match "- Default: (.+)$")) |
| 53 | + { |
| 54 | + $default = $Matches[1] |
| 55 | + $description = $_.description.text.replace("- Default: $default", "") |
| 56 | + } |
| 57 | + if(($_.defaultValue -ne "")) |
| 58 | + { |
| 59 | + $default = $_.defaultValue |
| 60 | + } |
| 61 | + |
| 62 | + $result += "`n| $($_.name) | $($_.type.name) | $($_.required) | $($default) | $($description) |" |
| 63 | + }) |
| 64 | + |
| 65 | + return $result |
| 66 | +} |
| 67 | + |
| 68 | +function Get-Examples { |
| 69 | + param ( |
| 70 | + # Get-Help Object |
| 71 | + [Parameter(Mandatory=$True,HelpMessage="Get-Help Object")] |
| 72 | + [object] $help |
| 73 | + ) |
| 74 | + |
| 75 | + $result = "``````powershell`n" |
| 76 | + $help.Examples[0].example.foreach({ |
| 77 | + #Write-Host "`Examples: $($_.code)" |
| 78 | + |
| 79 | + $result += "$($_.code)`n`n" |
| 80 | + }) |
| 81 | + $result += "```````n"; |
| 82 | + return $result |
| 83 | +} |
| 84 | + |
| 85 | +function Get-FlowChart { |
| 86 | + param ( |
| 87 | + # Filename |
| 88 | + [Parameter(Mandatory=$True,HelpMessage="Filename")] |
| 89 | + [string] $file |
| 90 | + ) |
| 91 | + |
| 92 | + $fileparts = $file.Split('.') |
| 93 | + $flowchartfile = "flowchart/$($fileparts[0]).flowchart" |
| 94 | + if (Test-Path -Path $flowchartfile) |
| 95 | + { |
| 96 | + # render flowchart |
| 97 | + diagrams flowchart $flowchartfile > $null |
| 98 | + |
| 99 | + return "`n`n## Flowchart`n`n<div align='center'>`n`n`n</div>" |
| 100 | + |
| 101 | + } else { |
| 102 | + return "" |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +function ConvertHelpToMarkdown { |
| 108 | + |
| 109 | + param ( |
| 110 | + # PS1 file name |
| 111 | + [Parameter(Mandatory=$True,HelpMessage="PS1 file name")] |
| 112 | + [object] $file, |
| 113 | + |
| 114 | + # Markdown file directory |
| 115 | + [Parameter(HelpMessage="Markdown file directory")] |
| 116 | + [string] $savepath = "." |
| 117 | + ) |
| 118 | + |
| 119 | + Write-Host "Processing $($file.name)..." |
| 120 | + $help = Get-Help -Name "$($file.FullName)" -full |
| 121 | + #$help |
| 122 | + |
| 123 | + if ($help.description -ne $null) |
| 124 | + { |
| 125 | + # Headline + SYNOPSIS |
| 126 | + $markdown = "# $($file.name)`n`n$($help.synopsis)" |
| 127 | + |
| 128 | + # Description |
| 129 | + $markdown += "`n`n## Description`n`n$($help.description.text)" |
| 130 | + |
| 131 | + # Parameters |
| 132 | + $parameters = Get-ParameterMarkdownTable -help $help |
| 133 | + $markdown += "`n`n## Parameters`n`n$($parameters)" |
| 134 | + |
| 135 | + # Examples |
| 136 | + $examples = Get-Examples -help $help |
| 137 | + $markdown += "`n`n## Examples`n`n$($examples)" |
| 138 | + |
| 139 | + # flowchart |
| 140 | + $markdown += Get-FlowChart -file $file.name |
| 141 | + |
| 142 | + $markdownfile = $file.name.replace("ps1","md") |
| 143 | + Set-Content -Path "$savepath/$markdownfile" -Value $markdown |
| 144 | + Write-Host "Created $savepath/$markdownfile..." |
| 145 | + } else { |
| 146 | + Write-Host "Skipped $($file.name)..." |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +# Load ps1 files in folder |
| 151 | +$FILES = @(Get-ChildItem -Path $dir -File -Recurse) | Where-Object -FilterScript {$_.name.contains(".ps1")} |
| 152 | +# Create Markdown files |
| 153 | +$FILES.ForEach({ConvertHelpToMarkdown -file $_ -savepath $savepath}) |
| 154 | + |
0 commit comments