-
Notifications
You must be signed in to change notification settings - Fork 5
/
Format-StringToReverse.ps1
99 lines (90 loc) · 3.41 KB
/
Format-StringToReverse.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
93
94
95
96
97
98
99
Function Format-StringToReverse {
<#
.SYNOPSIS
Function that can reverses a string that has a delimiter.
.DESCRIPTION
Function that can reverses a string that has a delimiter.
It splits the string with the supplied delimiter then uses [Array]::Reverse() to reverse the string.
After reversing the string it joins it with the same delimiter used to split.
This for example can be used to reverse the order of a users DN.
.NOTES
Author: Justin Perdok (@JustinPerdok), https://justin-p.me.
License: MIT
.LINK
https://github.com/justin-p/PowerShell/blob/master/Format-StringToReverse.ps1
.PARAMETER InputString
A string you would like to reverse
.PARAMETER Delimiter
The charachter between words in the string
.EXAMPLE
PS C:\> Format-StringToReverse -InputString 'This Is A String' -Delimiter ' '
String A Is This
.EXAMPLE
PS C:\> $User = Get-ADUsers -Identity SomeUser
PS C:\> $User.DistinguishedName
CN=SomeUser,OU=Users,OU=SomeOU,OU=CustomerName,DC=ad,DC=SomeDomain,DC=tld
PS C:\> $UserOUDN = $(Format-StringToReverse -InputString $($User.DistinguishedName) -SplitChar ',')
PS C:\> $UserOUDN
DC=tld,DC=SomeDomain,DC=ad,OU=CustomerName,OU=SomeOU,OU=Users,CN=SomeUser
PS C:\> $($UserOUDN.SubString($UserOUDN.IndexOf('OU=')).split(',')[0]).replace('OU=','')
CustomerName
#>
[CmdletBinding(SupportsShouldProcess=$true)]
Param (
[String]$InputString,
[String]$Delimiter
)
Begin {
$FunctionName = $MyInvocation.MyCommand.Name
Write-Debug "$($FunctionName) - Begin."
Try {
If ($script:ThisModuleLoaded -eq $true) {
#Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
}
If ($PSCmdlet.ShouldProcess("$($FunctionName) - Creating '`$OutputObject'")) {
Try {
[Array]$OutputObject = $($InputString.Split($Delimiter))
}
Catch {
Write-Error -Message "$($FunctionName) - $($PSItem)" -ErrorAction Stop
}
}
}
Catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
Process {
Write-Debug "$($FunctionName) - Process."
Try {
If ($PSCmdlet.ShouldProcess("$($FunctionName) - Filling '`$OutputObject'")) {
Try {
[Array]::Reverse($OutputObject)
$OutputObject = $OutputObject -Join $($Delimiter)
}
Catch {
Write-Error -Message "$($FunctionName) - $($PSItem)" -ErrorAction Stop
}
}
}
Catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
End {
Try {
If ($PSCmdlet.ShouldProcess("$($FunctionName) - Returning object")) {
Try {
Return $OutputObject
}
Catch {
Write-Error -Message "$($FunctionName) - $($PSItem)" -ErrorAction Stop
}
}
}
Catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
Write-Debug "$($FunctionName) - End."
}
}