-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvert-SplitNameValue.psm1
More file actions
177 lines (142 loc) · 6.29 KB
/
Convert-SplitNameValue.psm1
File metadata and controls
177 lines (142 loc) · 6.29 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function ConvertTo-SplitNameValue {
param(
[Parameter(Mandatory = $false,ValueFromPipeLine = $true)]
[psobject]$InputData,
[Parameter(Mandatory = $false)]
[switch]$IncludeNull,
[Parameter(Mandatory = $false)]
[switch]$Preview,
[Parameter(Mandatory = $false)]
[string]$ObjectMarker,
[Parameter(Mandatory = $false)]
[switch]$DisableObjectMarkers,
[Parameter(Mandatory = $false)]
[switch]$Help
)
begin {
$Index = 0
$SplitNameValue = ""
}
process {
if ($Help -eq $true) {
Write-Output ""
Write-Output "Info: You can use the '-InputData' parameter or the pipeline to Input Data containing 1 or more objects or arrays."
Write-Output "Info: You can use the '-IncludeNull' parameter to show all object data even if it is null"
Write-Output "Info: You can use the '-Preview' parameter to display the SplitNameValue Output as a list (For Debugging/Viewing)"
Write-Output "Info: You can use the '-ObjectMarker' parameter to choose a custom postfix marker when InputData contains multiple objects or arrays."
Write-Output "Info: The Default ObjectMarker is '_#' Example with 2 Objects: ( ObjectSize=10 | ObjectStatus=True | ObjectSize_#2=20 | ObjectStatus_#2=False )"
Write-Output ""
write-Output "The -DisableObjectMarkers Parameter can be used to Stop the separation of multiple objects,"
write-Output "But be WARNED this will cause weird overwritten/mangled objects to be generated if any objects data has the same name as another objects data,"
write-Output "On the other hand if you know all the objects data has different names then you could combine them into one object by doing this."
break
}
if ($InputData -eq $null) {
Write-Output ""
Write-Output "Error: No data suppied in pipeline or in the '-InputData' parameter";
break
}
$Index++
if ($ObjectMarker -eq "") {
$ObjectMarker = "_#"
}
if ($DisableObjectMarkers -eq $true) {
$Index = 1
}
if ($Index -eq 1) { $Count = "" } else { $Count = $ObjectMarker + $index }
foreach ($prop in $InputData.psobject.properties) {
if (($InputData | Format-List | Out-String) -match $prop.Name) {
if ($IncludeNull -eq $false) {
if ($InputData.$($prop.Name) -ne $null) {
if ($SplitNameValue -eq "" -and $index -eq 1) {
$SplitNameValue = ($prop.Name + $Count + "=" + $prop.Value)
} else {
$SplitNameValue = ($SplitNameValue += "|" + $prop.Name + $Count + "=" + $prop.Value)
}
}
} else {
if ($SplitNameValue -eq "") {
$SplitNameValue = ($prop.Name + $Count + "=" + $prop.Value)
} else {
$SplitNameValue = ($SplitNameValue += "|" + $prop.Name + $Count + "=" + $prop.Value)
}
}
}
}
}
end {
if ($Preview -eq $false) {
Write-Output $SplitNameValue | Out-String
} else {
Write-Output ""
Write-Output $SplitNameValue.Replace("|",[Environment]::NewLine)
}
}
}
function ConvertFrom-SplitNameValue {
param(
[Parameter(Mandatory = $false,ValueFromPipeLine = $true)]
[string[]]$InputString,
[Parameter(Mandatory = $false)]
[string]$ObjectMarker,
[Parameter(Mandatory = $false)]
[switch]$Help
)
process {
function Split-Text {
param(
[Parameter(Mandatory = $True)] [string]$Text,
[Parameter(Mandatory = $True)] [string]$Separator,
[string]$EscapeChar = '\'
)
$Text -split
('(?<=(?<!{0})(?:{0}{0})*){1}' -f [regex]::Escape($EscapeChar),[regex]::Escape($Separator)) `
-replace ('{0}(.)' -f [regex]::Escape($EscapeChar)),'$1'
}
if ($Help -eq $true) {
Write-Output ""
Write-Output "Info: You can use the '-InputString' parameter or the pipeline to Input a String in SplitNameValue Format"
Write-Output "Info: SplitNameValue Format Exampe: ( Date=5/19/1999 | Time=12:03 AM | DOW=Wednesday )"
Write-Output "Info: You can use the '-ObjectMarker' parameter to choose a custom postfix marker when InputString contains multiple objects or arrays"
Write-Output "Info: You must supply the same custom ObjectMarker found in the InputString (If not using Default) in order to correctly separate objects"
Write-Output "Info: The Default ObjectMarker is '_#' Example with 2 Objects: ( ObjectSize=10 | ObjectStatus=True | ObjectSize_#2=20 | ObjectStatus_#2=False )"
break
}
if ($InputString -eq $null) {
Write-Output ""
Write-Output "Error: No String Suppied in Pipeline or in the '-InputString' parameter";
break
}
if ($ObjectMarker -eq "") {
$ObjectMarker = "_#"
}
$array = $InputString.Split("|");
if ($array[-1] -like "*$ObjectMarker*") {
$NumberOfObjects = (Split-Text -Text $array[-1] -Separator $ObjectMarker)[1].Split("=")[0]
} else {
$NumberOfObjects = 1;
}
$Count = 1
while ($Count -le $NumberOfObjects) {
$obj = [pscustomobject]::new()
foreach ($split in $array) {
if ((((Split-Text -Text ($split.Split("=")[0]) -Separator $ObjectMarker)[1]) -eq $Count) -or (($split.Split("=")[0] -notlike "*$ObjectMarker*") -and $Count -eq 1)) {
if ($Count -eq 1) {
Add-Member -InputObject $obj -NotePropertyName ($split.Split("=")[0]) -NotePropertyValue $split.Split("=",2)[1] -Force
} else {
Add-Member -InputObject $obj -NotePropertyName ((Split-Text -Text $split -Separator ($ObjectMarker + $Count))[0]) -NotePropertyValue $split.Split("=",2)[1] -Force
}
}
}
$Count = $Count + 1
Write-Output $obj
}
}
}
Set-Alias CT-SNV ConvertTo-SplitNameValue
Set-Alias ConvertTo-SNV ConvertTo-SplitNameValue
Set-Alias CT-SplitNameValue ConvertTo-SplitNameValue
Set-Alias CF-SNV ConvertFrom-SplitNameValue
Set-Alias ConvertFrom-SNV ConvertFrom-SplitNameValue
Set-Alias CF-SplitNameValue ConvertFrom-SplitNameValue
Export-ModuleMember -Function ConvertTo-SplitNameValue, ConvertFrom-SplitNameValue -Alias CT-SNV, ConvertTo-SNV, CT-SplitNameValue, CF-SNV, ConvertFrom-SNV, CF-SplitNameValue