Private/Format-HashTable.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<#
    .SYNOPSIS
    This function allows for the manipulation of hashtable headings.
 
    .DESCRIPTION
    This function allows for the manipulation of Hashtable headings. The output
    is an Array -- not a Hashtable. All object data will be lost on output.
 
    .PARAMETER HashtableIn
    This paramater is the input object. It must be of the type [Hashtable].
 
    .PARAMETER KeyHeading
    Type: [String]
    The new left side heading value.
 
    .PARAMETER ValueHeading
    Type: [String]
    The new right side heading value.
 
    .EXAMPLE
    Format-Hashtable -HashtableIn @{'one'=1;'two'=2} -KeyHeading LeftHeading -ValueHeading RightHeading
 
    The output of the Hashtable in HashTableIn before formatting is:
 
    PS C:\> @{'one'=1;'two'=2}
 
    Name Value
    ---- -----
    one 1
    two 2
 
    The output of the command above is:
 
    PS C:\> Format-Hashtable -Hashtable @{'one'=1;'two'=2} -KeyHeading LeftHeading -ValueHeading RightHeading
 
    LeftHeading RightHeading
    ---------- -----------
    one 1
    two 2
 
 
    .NOTES
    This function is meant to make the output of a hashtable more readable by changing the Heading values to
    be more descriptive.
 
 
    .INPUTS
    Hashtable, String
 
    .OUTPUTS
    Array
#>

function Format-Hashtable
{
  param(
    [Parameter(Mandatory,ValueFromPipeline)]
    [hashtable]
    $HashtableIn,
    [ValidateNotNullOrEmpty()]
    [string]
    $KeyHeading = 'Name',
    [ValidateNotNullOrEmpty()]
    [string]
    $ValueHeading = 'Value',
    [switch]
    $OutString,
    [switch]
    $OutCsv,
    [switch]
    $OutJson
  )

  $Output = $HashtableIn.GetEnumerator() | Select-Object -Property @{
    Label      = $KeyHeading
    Expression = {
      $_.Key
    }
  }, @{
    Label      = $ValueHeading
    Expression = {
      $_.Value
    }
  }

  if ($OutString) {
    $OutputStringBuilder = [System.Text.StringBuilder]::new()
    foreach ($KeyValuePair in $Output) {
      $ThisValue = '{0} : {1}{2}' -f $KeyValuePair.Name.Trim(),$KeyValuePair.Value.Trim(),"`n"
      $null = $OutputStringBuilder.Append($ThisValue)
    }
    $Output = $OutputStringBuilder.ToString()
  } elseif ($OutCsv) {
    $null = $OutputStringBuilder = [System.Text.StringBuilder]::new()
    $null = $OutputNameLineStringBuilder = [System.Text.StringBuilder]::new()
    $null = $OutputValueLineStringBuilder = [System.Text.StringBuilder]::new()
    foreach ($KeyValuePair in $Output) {
      if($KeyValuePair.Name -like "*,*"){
        $KeyValuePair.Name = "`"$($KeyValuePair.Name)`""
      }
      if($KeyValuePair.Value -like "*,*"){
        $KeyValuePair.Value = "`"$($KeyValuePair.Value)`""
      }
      $ThisNameLineString = '{0},' -f $KeyValuePair.Name.Trim()
      $OutputNameLineStringBuilder.Append($ThisNameLineString) | Out-Null
      $ThisValueLineString = '{0},' -f $KeyValuePair.Value.Trim()
      $OutputValueLineStringBuilder.Append($ThisValueLineString) | Out-Null
    }
    $OutputStringBuilder.Append($OutputNameLineStringBuilder.ToString()) | Out-Null
    $OutputStringBuilder.AppendLine() | Out-Null
    $OutputStringBuilder.Append($OutputValueLineStringBuilder.ToString()) | Out-Null
    $null = $Output = $OutputStringBuilder.ToString()
  } elseif ($OutJson) {
    $Output = $Output | ConvertTo-Json
  }

  $Output
}