PowerShell/Scripts/convert-ps2md.ps1

136 lines
3.2 KiB
PowerShell
Raw Normal View History

2021-08-28 11:44:08 +02:00
<#
2021-08-28 12:37:13 +02:00
.SYNOPSIS
2021-08-30 15:48:23 +02:00
convert-ps2md.ps1 [<filename>]
2021-08-28 12:37:13 +02:00
.DESCRIPTION
2021-08-29 17:50:03 +02:00
Converts the comment-based help of a PowerShell script to Markdown.
2021-08-28 12:37:13 +02:00
.EXAMPLE
PS> .\convert-ps2md.ps1 myscript.ps1
.NOTES
2021-08-29 15:42:40 +02:00
Author: Markus Fleschutz · License: CC0
2021-08-28 12:37:13 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2021-08-28 11:44:08 +02:00
#>
2021-08-30 15:48:23 +02:00
param([string]$filename = "")
2021-08-28 11:44:08 +02:00
2021-08-28 17:48:14 +02:00
function EncodePartOfHtml { param([string]$Value)
2021-08-28 11:44:08 +02:00
($Value -replace '<', '&lt;') -replace '>', '&gt;'
}
2021-08-28 17:48:14 +02:00
function GetCode { param($Example)
2021-08-28 11:44:08 +02:00
$codeAndRemarks = (($Example | Out-String) -replace ($Example.title), '').Trim() -split "`r`n"
$code = New-Object "System.Collections.Generic.List[string]"
for ($i = 0; $i -lt $codeAndRemarks.Length; $i++) {
if ($codeAndRemarks[$i] -eq 'DESCRIPTION' -and $codeAndRemarks[$i + 1] -eq '-----------') {
break
}
if (1 -le $i -and $i -le 2) {
continue
}
$code.Add($codeAndRemarks[$i])
}
$code -join "`r`n"
}
2021-08-28 17:48:14 +02:00
function GetRemark { param($Example)
2021-08-28 11:44:08 +02:00
$codeAndRemarks = (($Example | Out-String) -replace ($Example.title), '').Trim() -split "`r`n"
$isSkipped = $false
$remark = New-Object "System.Collections.Generic.List[string]"
for ($i = 0; $i -lt $codeAndRemarks.Length; $i++) {
if (!$isSkipped -and $codeAndRemarks[$i - 2] -ne 'DESCRIPTION' -and $codeAndRemarks[$i - 1] -ne '-----------') {
continue
}
$isSkipped = $true
$remark.Add($codeAndRemarks[$i])
}
$remark -join "`r`n"
}
try {
2021-08-30 15:48:23 +02:00
if ($filename -eq "") { $filename = read-host "Enter path to PowerShell script" }
$ScriptName = (get-item "$filename").Name
2021-08-28 14:20:05 +02:00
2021-08-30 15:48:23 +02:00
$full = Get-Help $filename -Full
2021-08-28 17:48:14 +02:00
2021-09-06 14:46:52 +02:00
"# The $ScriptName PowerShell Script"
2021-08-28 12:58:48 +02:00
2021-08-28 17:48:14 +02:00
""
2021-08-29 18:27:03 +02:00
"## Synopsis & Description"
2021-08-29 18:05:09 +02:00
"``````powershell"
2021-08-28 17:48:14 +02:00
"$($full.Synopsis)"
2021-08-29 18:05:09 +02:00
"``````"
2021-08-28 12:58:48 +02:00
$Description = ($full.description | Out-String).Trim()
if ($Description -ne "") {
2021-08-28 17:48:14 +02:00
""
2021-08-28 12:58:48 +02:00
"$Description"
}
$Syntax = (($full.syntax | Out-String) -replace "`r`n", "`r`n`r`n").Trim()
if ($Syntax -ne "") {
""
2021-08-29 18:21:53 +02:00
"## Syntax & Parameters"
2021-08-28 12:58:48 +02:00
"``````powershell"
"$Syntax"
"``````"
}
2021-08-28 11:44:08 +02:00
2021-08-28 14:20:05 +02:00
foreach($parameter in $full.parameters.parameter) {
""
"``````"
"$(((($parameter | Out-String).Trim() -split "`r`n")[-5..-1] | % { $_.Trim() }) -join "`r`n")"
"``````"
}
2021-08-29 18:12:58 +02:00
""
"``````"
2021-08-29 18:21:53 +02:00
"[<CommonParameters>]"
2021-08-29 18:12:58 +02:00
" This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, "
" WarningVariable, OutBuffer, PipelineVariable, and OutVariable."
"``````"
2021-08-28 11:44:08 +02:00
2021-08-28 17:48:14 +02:00
foreach($input in $full.inputTypes.inputType) {
2021-08-28 14:20:05 +02:00
""
"## Inputs"
2021-08-28 17:48:14 +02:00
"$($input.type.name)"
2021-08-28 14:20:05 +02:00
}
2021-08-28 11:44:08 +02:00
2021-08-28 17:48:14 +02:00
foreach($output in $full.outputTypes.outputType) {
2021-08-28 14:20:05 +02:00
""
"## Outputs"
2021-08-28 17:48:14 +02:00
"$($output.type.name)"
2021-08-28 14:20:05 +02:00
}
2021-08-28 11:44:08 +02:00
2021-08-28 17:48:14 +02:00
foreach($example in $full.examples.example) {
""
"## Example"
"``````powershell"
"$(GetCode $example)"
"``````"
"$(GetRemark $example)"
}
2021-08-28 11:44:08 +02:00
2021-08-28 12:58:48 +02:00
$Notes = ($full.alertSet.alert | Out-String).Trim()
if ($Notes -ne "") {
""
"## Notes"
"$Notes"
}
$Links = ($full.relatedlinks | Out-String).Trim()
if ($Links -ne "") {
""
"## Related Links"
"$Links"
}
2021-08-29 12:44:05 +02:00
""
2021-09-06 14:46:52 +02:00
"*Generated by convert-ps2md.ps1 using the comment-based help of $ScriptName*"
2021-08-28 14:20:05 +02:00
} catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
2021-08-29 12:44:05 +02:00
}