-
-
Save Matticusau/49943eb19efd54783966449bde53e9db to your computer and use it in GitHub Desktop.
ConvertTo-Markdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Converts a PowerShell object to a Markdown table. | |
.EXAMPLE | |
$data | ConvertTo-Markdown | |
.EXAMPLE | |
ConvertTo-Markdown($data) | |
.EXAMPLE | |
Invoke-ScriptAnalyzer -Path C:\MyScript.ps1 | Select-Object -Property RuleName,Line,Severity,Message | ` | |
ConvertTo-Markdown | Out-File C:\MyScript.ps1.md | |
Converts the output of PSScriptAnalyzer for a given script to a Markdown report using selected properties | |
#> | |
Function ConvertTo-Markdown { | |
[CmdletBinding()] | |
[OutputType([string])] | |
Param ( | |
[Parameter( | |
Mandatory = $true, | |
Position = 0, | |
ValueFromPipeline = $true | |
)] | |
[PSObject[]]$collection | |
) | |
Begin { | |
$items = @() | |
$columns = @{} | |
} | |
Process { | |
ForEach($item in $collection) { | |
$items += $item | |
$item.PSObject.Properties | ForEach-Object { | |
# get the value to avoid (null value object errors) | |
$value = $null; | |
if ($value -ne $_.Value) | |
{ | |
$value = $_.Value.ToString().Length; | |
} | |
# verify if we need to add the value to the hash table | |
if(-not $columns.ContainsKey($_.Name) -or $columns[$_.Name] -lt $value.Length) { | |
$columns[$_.Name] = $value; | |
} | |
} | |
} | |
} | |
End { | |
ForEach($key in $($columns.Keys)) { | |
$columns[$key] = [Math]::Max($columns[$key], $key.Length) | |
} | |
$header = @() | |
ForEach($key in $columns.Keys) { | |
$header += ('{0,-' + $columns[$key] + '}') -f $key | |
} | |
$header -join ' | ' | |
$separator = @() | |
ForEach($key in $columns.Keys) { | |
$separator += '-' * $columns[$key] | |
} | |
$separator -join ' | ' | |
ForEach($item in $items) { | |
$values = @() | |
ForEach($key in $columns.Keys) { | |
$values += ('{0,-' + $columns[$key] + '}') -f $item.($key) | |
} | |
$values -join ' | ' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is forked from GuruAnt/ConvertTo-Markdown.ps1 and includes the following improvements: