forked from tonypags/PsWinAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormat-ObjectToString.ps1
More file actions
107 lines (84 loc) · 3.93 KB
/
Copy pathFormat-ObjectToString.ps1
File metadata and controls
107 lines (84 loc) · 3.93 KB
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
function Format-ObjectToString
{
<#
.SYNOPSIS
Formats a PS Object into a string using custom attributes.
.DESCRIPTION
Ingests any PS Object and requires an ordered hashtable to map its key-value pairs to
control the content of a resultant string from the values in the object.
The result will be in the form:
PropertyName1: ExpressionResult1; PropertyName2: ExpressionResult2, etc...
.PARAMETER Format
An ORDERED hashtable giving the output formatting of the object properties as a series of strings.
For the KEY, use the NAME of the property as you would like it displayed in the resultant string.
Use a single tilde (~) character for the key to hide the property name in the resultant string and
force that item to be first in the list, regardless of place in the (ordered) hashtable.
For the VALUE, use an EXPRESSION that will give you the value to be displayed after a given
property's equater, as when using the form of `$Object | Select @{'Name'='Name';Exp=$Format.Key}`
.PARAMETER Delimiter
One or more characters that will separate each property along the new string. The default is "; ".
The tilde (~) is not allowed.
.PARAMETER Equater
One or more characters that will separate the property and the value in each pair in the resultant
string. The default is ": ".
.PARAMETER InputObject
The object with multiple properties you would like to turn into a delimited string.
.EXAMPLE
Get-ChildItem | Format-ObjectToString [ordered]@{ '~'={$_.FullName}; 'Modified'={
$_.LastWriteTime.ToString('yyyyMMdd')} } -Equater ' = '
"C:\Users\myprofile\Downloads\vote.txt"; Modified = 20161108
#>
[CmdletBinding()]
param (
# An ORDERED hashtable giving the output formatting of the object properties.
[Parameter(Mandatory=$true,
Position=0)]
#[System.Collections.Specialized.OrderedDictionary]
$Format,
# One or more characters that will separate each property along the new string.
[Parameter(Position=1)]
[ValidateScript({ $_ -ne '~' })]
[string]
$Delimiter = '; ',
# One or more characters will separate the Property Name from the Expression Result.
[Parameter(Position=2)]
[string]
$Equater = ': ',
# The object with multiple properties you would like to turn into a delimited string.
[Parameter(ValueFromPipeline=$true)]
$InputObject
)
begin {}
process {
Foreach ($object in $InputObject) {
# Build a string
[string[]]$thisResultantString = @()
# The user may have elected to choose a name-less value to be first.
if ($Format.Keys -contains '~') {
$thisResultantString += $object | Select-Object @{
Name = 'ShortLivedPropertyName'
Expression = $Format['~']
} | Select-Object -ExpandProperty ShortLivedPropertyName -ea Stop
}#if ($Format.Keys -contains '~')
# Process the remaining Keys
[string[]]$Keys = $Format.Keys | Where-Object {$_ -ne '~'}
Foreach ($Key in $Keys) {
$thisResultantString += $object | Select-Object @{
Name = $Key
Expression = $Format[$Key]
} | Select-Object @{
Name = 'ShortLivedPropertyName'
Expression = {
[string](
$Key +
$Equater +
$_.$Key
)
}
} | Select-Object -ExpandProperty ShortLivedPropertyName -ea Stop
}#Foreach ($object in $InputObject)
Write-Output ($thisResultantString -join $Delimiter)
}#Foreach ($object in $InputObject)
}#Process
end {}
}