-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathValidateComputerName.ps1
70 lines (58 loc) · 2.42 KB
/
ValidateComputerName.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
function Test-Computername {
[OutputType('bool')]
[CmdletBinding()]
param (
[Parameter(Mandatory,ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[String]$ComputerName
)
Write-Debug "Testing $ComputerName"
if ($ComputerName.Length -lt 1 -or $ComputerName.Length -gt 15)
{
Write-Verbose "Not allowed: $ComputerName"
Write-Verbose "Length must be between 1 and 15 characters"
return $false
}
$disallowedChars = @('\', '~', ':', '!', '@', '#', '$', '%', '^', '&', "'", '(', ')', '{', '}',' ')
foreach ($c in $disallowedChars)
{
Write-Debug "Testing: $c"
if ($ComputerName.Contains($c))
{
Write-Verbose "Not allowed: $c in $ComputerName"
Write-Verbose "Disallowed characters are: $($disallowedChars -join '') see https://support.microsoft.com/en-us/help/909264/naming-conventions-in-active-directory-for-computers-domains-sites-and"
return $false
}
}
if ($ComputerName.EndsWith('-') -or $ComputerName.EndsWith('.'))
{
Write-Verbose "Not allowed: $ComputerName"
Write-Verbose 'Computer names cannot end with - or .'
return $false
}
if ($ComputerName.StartsWith('.'))
{
Write-Verbose "Not allowed: $ComputerName"
Write-Verbose 'Computer names cannot start with .'
return $false
}
if ($ComputerName.Length -eq 2)
{
$disallowedNames = @("AN", "AO", "AU", "BA", "BG", "BO", "BU", "CA", "CD", "CG", "CO", "DA", "DG", "DU", "EA", "ED", "HI", `
"IU", "LA", "LG", "LS", "LW", "ME", "MU", "NO", "NS", "NU", "PA", "PO", "PS", "PU", "RC", "RD", "RE", "RO", "RS", "RU", `
"SA", "SI", "SO", "SU", "SY", "WD")
foreach ($name in $disallowedNames)
{
Write-Debug "Testing: $name"
if ($ComputerName -eq $name)
{
Write-Verbose "Not allowed: $ComputerName"
Write-Verbose '2-character SDDL user strings that are listed in well-known SIDs list cannot be used. Otherwise, "import", "export" and "take control" operations fail. See https://msdn.microsoft.com/en-us/library/windows/desktop/aa379602%28v=vs.85%29.aspx'
Write-Verbose ($disallowedNames -join ',')
return $false
}
}
}
Write-Verbose "$ComputerName is valid"
return $true
}