Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active December 7, 2015 09:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/1806e2417e94a43116ab to your computer and use it in GitHub Desktop.
Save guitarrapc/1806e2417e94a43116ab to your computer and use it in GitHub Desktop.
measure-command {ps | Format-Markdown}
function Format-Markdown
{
<#
.Author
Trevor Sullivan <trevor@trevorsullivan.net>
http://trevorsullivan.net
.Synopsis
Formats an array of objects as a Markdown table.
.Parameter Properties
The -Properties parameter accepts an array of System.String objects, which
represents the properties that will be retrieved from the input objects.
.Parameter InputObject
The -InputObject accepts an array of arbitrary objects. The objects should
all be of the same type. If the input objects are of different types, then
the output from this command will be unpredictable.
.Example
Get-Process -Name *b* | Format-Markdown -Properties Id,Name,WorkingSet;
.Example
Format-Markdown -Properties Name,Status,DisplayName -InputObject (Get-Service);
#>
[CmdletBinding()]
param
(
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
[Object[]] $InputObject,
[string[]] $Properties = '*'
)
begin
{
$mdString = New-Object System.Collections.Generic.List[string];
}
process
{
# Get Properies for only once
if ($null -eq $PropList)
{
$PropList = $InputObject | Get-Member -MemberType Property,NoteProperty,AliasProperty,ScriptProperty -Name $Properties;
}
# Create Markdown table internal string
foreach ($Item in $InputObject)
{
$internalString = New-Object System.Collections.Generic.List[string];
foreach ($Property in $PropList)
{
Write-Verbose -Message ('Item is: {0}; Property name is: {1}' -f $Item.ToString(), $Property.Name);
try
{
$PropValue = $null;
$PropValue = $Item.$($Property.Name).ToString();
$internalString.Add("|$PropValue");
}
catch
{
Write-Verbose -Message ('Failed to obtain value for property: {0}' -f $Property.Name);
}
}
$internalString.Add('|');
$mdString.Add("$internalString");
}
}
end
{
# Adding Markdown Table Header
$headerList = New-Object System.Collections.Generic.List[string];
foreach ($Property in $PropList)
{
$headerName = $Property.Name;
$headerList.Add("|$headerName");
}
$headerList.Add('|');
$headerString = ($PropList | ForEach-Object -Begin { "`r`n|" } -Process { '---|' } -End { '|'; });
$headerList.Add($headerString);
$mdString.Insert(0, "$headerList");
return $mdString;
}
}
function Format-Markdown {
<#
.Author
Trevor Sullivan <trevor@trevorsullivan.net>
http://trevorsullivan.net
.Synopsis
Formats an array of objects as a Markdown table.
.Parameter Properties
The -Properties parameter accepts an array of System.String objects, which
represents the properties that will be retrieved from the input objects.
.Parameter InputObject
The -InputObject accepts an array of arbitrary objects. The objects should
all be of the same type. If the input objects are of different types, then
the output from this command will be unpredictable.
.Example
Get-Process -Name *b* | Format-Markdown -Properties Id,Name,WorkingSet;
.Example
Format-Markdown -Properties Name,Status,DisplayName -InputObject (Get-Service);
#>
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
[System.Object[]] $InputObject
, [string[]] $Properties = '*'
)
begin {
}
process {
$Params = $PSBoundParameters;
foreach ($Item in $InputObject) {
$MDString += "`n";
foreach ($Property in ($Item | Get-Member -MemberType Property,NoteProperty,AliasProperty,ScriptProperty -Name $Properties)) {
Write-Verbose -Message ('Item is: {0}; Property name is: {1}' -f $Item.ToString(), $Property.Name);
try {
$PropValue = $null;
$PropValue = $Item.$($Property.Name).ToString();
$MDString += '|{0}' -f $PropValue;
} catch {
Write-Verbose -Message ('Failed to obtain value for property: {0}' -f $Property.Name);
}
}
$MDString += '|';
}
}
end {
$PropList = $InputObject | Get-Member -MemberType Property,NoteProperty,AliasProperty,ScriptProperty -Name $Properties;
$Header = '';
foreach ($Property in $PropList) {
$Header += '|{0}' -f $Property.Name;
}
$Header += '|';
$Header += $PropList | ForEach-Object -Begin { "`r`n|" } -Process { '---|' } -End { '|'; };
$MDString = $MDString.Insert(0, $Header);
Write-Output $MDString;
}
}
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 4
Milliseconds      : 213
Ticks             : 42137417
TotalDays         : 4.87701585648148E-05
TotalHours        : 0.00117048380555556
TotalMinutes      : 0.0702290283333333
TotalSeconds      : 4.2137417
TotalMilliseconds : 4213.7417
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 5
Milliseconds      : 875
Ticks             : 58757014
TotalDays         : 6.80058032407407E-05
TotalHours        : 0.00163213927777778
TotalMinutes      : 0.0979283566666667
TotalSeconds      : 5.8757014
TotalMilliseconds : 5875.7014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment