-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFormat-ReverseToken.ps1
93 lines (79 loc) · 2.56 KB
/
Format-ReverseToken.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
Function Format-ReverseToken {
<#
.SYNOPSIS
To reverse a string that is broken into tokens by a delimiter
.DESCRIPTION
To reverse a string that is broken into tokens by a delimiter
.PARAMETER String
The string you wish to be reversed. Can be a string or an array of strings.
Can be passed from the pipeline
.PARAMETER Delimiter
The delimiter character that separates the tokens. Defaults to '.'
.PARAMETER IncludeInput
Switch to display the input parameters with the output
.PARAMETER Trim
Switch to trim each token
.EXAMPLE
Format-ReverseToken -String 'google.com'
Would return
com.google
.EXAMPLE
Format-ReverseToken -String 'monster.us .google.com'
Would return
com.google.us .monster
.EXAMPLE
Format-ReverseToken -String 'monster.us .google.com' -Trim -IncludeInput
Would return
Original Delimiter Trim ReverseToken
-------- --------- ---- ------------
monster.us .google.com . True com.google.us.monster
.EXAMPLE
'yahoo.com','google.com' | Format-ReverseToken
Would return
com.yahoo
com.google
.EXAMPLE
Format-ReverseToken -String 'monster;google;com' -Trim -Delimiter ';'
Would return
com;google;monster
#>
[CmdletBinding(ConfirmImpact='None')]
[OutputType('string')]
param(
[Parameter(Mandatory, HelpMessage='Enter a string composed of tokens',Position=0,ValueFromPipeline)]
[string[]] $String,
[string] $Delimiter = '.',
[switch] $IncludeInput,
[switch] $Trim
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
foreach ($currentString in $String) {
if ($Trim) {
$currentString = $currentString.Trim()
}
$tmpArray = $currentString.split($Delimiter)
if ($Trim) {
for ($i=0; $i -lt $tmpArray.Count; $i++) {
$tmpArray[$i] = $tmpArray[$i].Trim()
}
}
$ReturnVal = $tmpArray[($tmpArray.Count-1)..0] -join $Delimiter
if ($IncludeInput) {
New-Object -TypeName 'psobject' -Property ([ordered] @{
Original = $currentString
Delimiter = $Delimiter
Trim = $Trim
ReverseToken = $ReturnVal
})
} else {
write-output -InputObject $ReturnVal
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}