1+ function New-Documentation {
2+ [CmdletBinding ()]
3+ param
4+ (
5+ [Parameter (Mandatory = $true , Position = 0 )]
6+ [string ]$ModuleName ,
7+ [Parameter (Mandatory = $false , Position = 1 )]
8+ [string ]$OutputFolderPath = (Get-Location )
9+ )
10+
11+ $parametersProperties = @ (
12+ ' DefaultValue' ,
13+ ' ParameterValue' ,
14+ ' PipelineInput' ,
15+ ' Position' ,
16+ ' Required'
17+ )
18+
19+ $NL = [System.Environment ]::NewLine
20+ $scriptStart = " `````` PowerShell$NL "
21+ $scriptEnd = " $NL `````` "
22+
23+ $outputFile = (New-TemporaryFile ).FullName
24+ foreach ($cmd in (Get-Command - Module $ModuleName ).Name ) {
25+ Write-Verbose " Generating documentation for $ ( $cmd ) "
26+ $help = Get-Help $cmd - ErrorAction ' SilentlyContinue'
27+ " $NL ## " + $cmd | Out-File - FilePath $outputFile - Append
28+
29+ if ($help.Synopsis ) {
30+ " $NL ### .SYNOPSIS" | Out-File - FilePath $outputFile - Append
31+ $NL + $help.Synopsis | Out-File - FilePath $outputFile - Append
32+ }
33+
34+ if ($help.Syntax ) {
35+ " $NL ### .SYNTAX" | Out-File - FilePath $outputFile - Append
36+ $NL + $scriptStart + ($help.Syntax | Out-String ).trim() + $scriptEnd | Out-File - FilePath $outputFile - Append
37+ }
38+
39+ if ($help.Description ) {
40+ " $NL ### .DESCRIPTION" | Out-File - FilePath $outputFile - Append
41+ $NL + $help.Description.Text | Out-File - FilePath $outputFile - Append
42+ }
43+
44+ if ($help.Parameters ) {
45+ " $NL ### .PARAMETERS" | Out-File - FilePath $outputFile - Append
46+ forEach ($item in $help.Parameters.Parameter ) {
47+ " $NL ### `` -" + $item.name + " `` " | Out-File - FilePath $outputFile - Append
48+ if ($item.Description.Text ) {
49+ $NL + $item.Description.Text | Out-File - FilePath $outputFile - Append
50+ }
51+ " $NL |||" | Out-File - FilePath $outputFile - Append
52+ ' |-|:-|' | Out-File - FilePath $outputFile - Append
53+ " |**Type**: |$ ( $item.Type.Name ) |" | Out-File - FilePath $outputFile - Append
54+ forEach ($p in $parametersProperties ) {
55+ if ($item .$p ) {
56+ " |**$p **: |$ ( $item .$p ) |" | Out-File - FilePath $outputFile - Append
57+ }
58+ }
59+ }
60+ }
61+
62+ if ($help.Examples ) {
63+ " $NL ## Examples" | Out-File - FilePath $outputFile - Append
64+ forEach ($item in $help.Examples.Example ) {
65+ " $NL ### " + $item.title.Replace (' -' , ' ' ).Replace(' EXAMPLE' , ' Example' ) | Out-File - FilePath $outputFile - Append
66+ if ($item.Code ) {
67+ $NL + $scriptStart + $item.Code + $scriptEnd | Out-File - FilePath $outputFile - Append
68+ }
69+ }
70+ }
71+ }
72+
73+ try {
74+ $finalFileName = " $OutputFolderPath \$ModuleName .md"
75+ [System.Io.File ]::ReadAllText($outputFile ) | Out-File - FilePath $finalFileName - Encoding utf8
76+ }
77+ finally {
78+ Remove-Item $outputFile - Force
79+ }
80+ }
0 commit comments