-
-
Save ScriptAutomate/f4a838f6acbb503bd28cf15ef70fcfa6 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
Function ConvertTo-Markdown { | |
<# | |
.Synopsis | |
Converts a PowerShell object to a Markdown table. | |
.Description | |
Converts a PowerShell object to a Markdown table. | |
.Parameter InputObject | |
PowerShell object to be converted | |
.Example | |
ConvertTo-Markdown -InputObject (Get-Service) | |
Converts a list of running services on the local machine to a Markdown table | |
.Example | |
ConvertTo-Markdown -InputObject (Import-CSV "C:\Scratch\lwsmachines.csv") | Out-File "C:\Scratch\file.markdown" -Encoding "ASCII" | |
Converts a CSV file to a Markdown table | |
.Example | |
Import-CSV "C:\Scratch\lwsmachines.csv" | ConvertTo-Markdown | Out-File "C:\Scratch\file2.markdown" -Encoding "ASCII" | |
Converts a CSV file to a markdown table via the pipeline. | |
.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 | |
#> | |
[CmdletBinding()] | |
[OutputType([string])] | |
Param ( | |
[Parameter( | |
Mandatory = $true, | |
Position = 0, | |
ValueFromPipeline = $true | |
)] | |
[PSObject[]]$collection | |
) | |
Begin { | |
$items = @() | |
$columns = [ordered]@{} | |
} | |
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.Contains($_.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
For a much better experience, just use: https://github.com/Sarafian/MarkdownPS