-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathConvertTo-StringList.ps1
More file actions
109 lines (98 loc) · 3.67 KB
/
ConvertTo-StringList.ps1
File metadata and controls
109 lines (98 loc) · 3.67 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
function ConvertTo-StringList {
<#
.SYNOPSIS
Turns encoded list data into something you can foreach over.
.DESCRIPTION
String input: if it is JSON (object or array), it is converted first; otherwise comma/semicolon/newline
splitting applies. Other shapes: wrapper objects, or an existing array/list. After conversion, the return
value is always foreach-able (empty collection is @()). Arrays and IList instances are returned
as-is — they are already foreach-able without conversion.
This exists because front end multi-value input is annoying to deal with.
.PARAMETER InputObject
Encoded list (string/JSON), wrapper object, or an existing array/list.
.PARAMETER PropertyNames
On hashtables/PSCustomObjects, property names to read in order.
.OUTPUTS
Always an enumerable suitable for: foreach ($item in (ConvertTo-StringList ...)) { }
#>
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[Alias('Input', 'Value')]
[AllowNull()]
$InputObject,
[string[]]$PropertyNames = @('Items', 'Value')
)
# Output must be foreach-able; $null input yields empty collection.
if ($null -eq $InputObject) {
return @()
}
if ($InputObject -is [string]) {
$s = $InputObject.Trim()
if (-not $s) {
return @()
}
if ($s.StartsWith('[') -or $s.StartsWith('{')) {
try {
$parsed = $s | ConvertFrom-Json -ErrorAction Stop
return ConvertTo-StringList -InputObject $parsed -PropertyNames $PropertyNames
} catch {
}
}
return @(
$s -split '[,;\r\n]+' | ForEach-Object { $_.Trim() } | Where-Object { $_ }
)
}
if ($InputObject -is [Array]) {
return $InputObject
}
if ($InputObject -is [System.Collections.IList] -and $InputObject -isnot [string]) {
return $InputObject
}
if ($InputObject -is [hashtable]) {
if ($InputObject.Count -eq 0) {
return @()
}
foreach ($name in $PropertyNames) {
if ($InputObject.ContainsKey($name)) {
return ConvertTo-StringList -InputObject $InputObject[$name] -PropertyNames $PropertyNames
}
}
foreach ($p in $InputObject.GetEnumerator() | Sort-Object { $_.Key }) {
$v = $p.Value
if ($null -eq $v) { continue }
if ($v -is [string] -or ($v -is [System.Collections.IEnumerable] -and $v -isnot [hashtable] -and $v -isnot [pscustomobject])) {
return ConvertTo-StringList -InputObject $v -PropertyNames @()
}
}
$single = "$InputObject".Trim()
if ($single) {
return , @($single)
}
return @()
}
if ($InputObject -is [pscustomobject]) {
foreach ($name in $PropertyNames) {
if ($InputObject.PSObject.Properties.Name -contains $name) {
return ConvertTo-StringList -InputObject $InputObject.$name -PropertyNames $PropertyNames
}
}
foreach ($p in $InputObject.PSObject.Properties) {
$v = $p.Value
if ($null -eq $v) { continue }
if ($v -is [string] -or ($v -is [System.Collections.IEnumerable] -and $v -isnot [hashtable] -and $v -isnot [pscustomobject])) {
return ConvertTo-StringList -InputObject $v -PropertyNames @()
}
}
$single = "$InputObject".Trim()
if ($single) {
return , @($single)
}
return @()
}
$t = "$InputObject".Trim()
if ($t) {
return , @($t)
}
return @()
}