-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTest-IsValidDn.ps1
46 lines (38 loc) · 2.02 KB
/
Test-IsValidDn.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
function Test-IsValidDN
{
<#
.SYNOPSIS
Cmdlet will check if the input string is a valid distinguishedname.
.DESCRIPTION
Cmdlet will check if the input string is a valid distinguishedname.
Cmdlet is intended as a dignostic tool for input validation
.PARAMETER ObjectDN
A string representing the object distinguishedname.
.EXAMPLE
PS C:\> Test-IsValidDN -ObjectDN 'Value1'
#>
[OutputType([bool])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias('DN', 'DistinguishedName')]
[string]
$ObjectDN
)
# Create new string builder
[System.Text.StringBuilder]$regexStringBuilder = [System.Text.StringBuilder]::New()
[void]($regexStringBuilder.Append('^(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|'))
[void]($regexStringBuilder.Append('(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:'))
[void]($regexStringBuilder.Append('[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*")(?:\+(?:[A-Za-z]'))
[void]($regexStringBuilder.Append('[\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]'))
[void]($regexStringBuilder.Append('|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|'))
[void]($regexStringBuilder.Append('\\[\dA-Fa-f]{2})*"))*(?:,(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#'))
[void]($regexStringBuilder.Append('(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]'))
[void]($regexStringBuilder.Append('{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*")(?:\+(?:[A-Za-z]'))
[void]($regexStringBuilder.Append('[\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\'))
[void]($regexStringBuilder.Append('+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*"))*)*$'))
# Define DN Regex
[string]$distinguishedNameRegex = $regexStringBuilder.ToString()
return $ObjectDN -match $distinguishedNameRegex
}