-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertfrom-customstring.ps1
37 lines (31 loc) · 1.45 KB
/
convertfrom-customstring.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
function ConvertFrom-CustomString {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true)]
[string]$InputString
)
process {
if ($InputString -ne $null) {
$pattern = '(?:"(?:\\.|[^"\\])*"|\[(?:\\.|[^\]\\])*]|\((?:\\.|[^\\)])*\)|\{(?:\\.|[^\\}])*\}|\S+)'
$matches = [regex]::Matches($InputString, $pattern)
$tokens = @()
foreach ($match in $matches) {
$token = $match.Value
# Remove surrounding quotes or delimiters and unescape any escaped characters
switch -Regex ($token) {
'^".*"$' { $token = $token.Substring(1, $token.Length - 2) -replace '\\"', '"' }
'^\[.*\]$' { $token = $token.Substring(1, $token.Length - 2) -replace '\\\]', ']' }
'^\(.*\)$' { $token = $token.Substring(1, $token.Length - 2) -replace '\\\)', ')' }
'^\{.*\}$' { $token = $token.Substring(1, $token.Length - 2) -replace '\\\}', '}' }
}
$tokens += $token
}
# Create a custom object with properties P1, P2, P3, ...
$obj = New-Object -TypeName PSObject
for ($i = 0; $i -lt $tokens.Count; $i++) {
Add-Member -InputObject $obj -MemberType NoteProperty -Name ("P" + ($i + 1)) -Value $tokens[$i]
}
return $obj
}
}
}