-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGet-UniqueUPN.ps1
169 lines (143 loc) · 5.25 KB
/
Get-UniqueUPN.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
function Get-UniqueUPN
{
<#
.SYNOPSIS
Cmdlet will generate a forest wide unique UPN.
.DESCRIPTION
Cmdlet will generate a forest wide unique UPN according to generation rules
defined by the user.
Cmdlet accept different types of objects to generate the UPN to allow greater flexibility
ADObject - For example and object from Get-AdUser cmdlet
Strings - Representing First Name, Last Name etc.
DirectoryService Objects - For example when using native .Net methods to retrieve the identity
.PARAMETER ADObject
An ADObject for example output of the Get-ADUser cmdlet
.PARAMETER FirstName
A string representing the First Name of the user
.PARAMETER LastName
A string representing the Last Name of the user
.PARAMETER MiddleName
A string representing the Middle Name of the user, parameter is optional.
.PARAMETER UPNSuffix
A string representing the UPN suffix to be used.
.PARAMETER FirstNameFormat
A string representing the format to be for the First Name part of the UPN.
.PARAMETER LastNameFormat
A string representing the format to be for the Last Name part of the UPN.
.PARAMETER IncludeMiddleName
When paramenter is specified user Middle Name, if present, will be included in the UPN generation process.
.PARAMETER ADServer
A string representing the name of the AD Domain Controller that will be used to query Active Directory.
If no server is specified the closest Global Catalog will be automatically selected.
.PARAMETER Separator
A string representing the separator to be used between UPN parts, defaults to a '.'.
#>
[CmdletBinding(DefaultParameterSetName = 'Strings')]
param
(
[Parameter(ParameterSetName = 'ADObject',
Mandatory = $true)]
[object]$ADObject,
[Parameter(ParameterSetName = 'Strings',
Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$FirstName,
[Parameter(ParameterSetName = 'Strings',
Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$LastName,
[Parameter(ParameterSetName = 'Strings')]
[ValidateNotNullOrEmpty()]
[string]$MiddleName,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$UPNSuffix,
[ValidateSet('FullName', 'FirstLetter', IgnoreCase = $true)]
[ValidateNotNullOrEmpty()]
[string]$FirstNameFormat = 'Full',
[ValidateSet('FullName', 'FirstLetter', IgnoreCase = $true)]
[ValidateNotNullOrEmpty()]
[string]$LastNameFormat = 'FullName',
[switch]$IncludeMiddleName,
[ValidateNotNullOrEmpty()]
[string]$ADServer,
[ValidateNotNullOrEmpty()]
[string]$Separator = '.'
)
if ($PSCmdlet.ParameterSetName -eq 'ADObject')
{
switch ($ADObject.GetType().FullName)
{
'Microsoft.ActiveDirectory.Management.ADUser'
{
[string]$firstName = $ADObject.GivenName
[string]$lastName = $ADObject.Surname
[string]$middleName = $ADObject.MiddleName
break
}
'System.DirectoryServices.DirectoryEntry'
{
[string]$firstName = $ADObject.Properties['givenName'][0]
[string]$lastName = $ADObject.Properties['sn'][0]
[string]$middleName = $ADObject.Properties['middleName'][0]
break
}
'System.DirectoryServices.SearchResult'
{
[string]$firstName = $ADObject.Properties['givenName'][0]
[string]$lastName = $ADObject.Properties['sn'][0]
[string]$middleName = $ADObject.Properties['middleName'][0]
break
}
default
{
throw "Unsupported AD object type: $($ADObject.GetType().FullName)"
}
}
}
else
{
[string]$firstName = $FirstName
[string]$lastName = $LastName
[string]$middleName = $MiddleName
}
# Format first name
$firstName = switch ($FirstNameFormat)
{
'FullName'
{
$firstName
}
'FirstLetter'
{
$firstName.Substring(0, 1)
}
}
# Format last name
$LastName = switch ($FirstNameFormat)
{
'FullName'
{
$LastName
}
'FirstLetter'
{
$LastName.Substring(0, 1)
}
}
# Use middle name
[string]$middleNamePart = if ($IncludeMiddleName -and $MiddleName)
{
'{0}{1}' -f $Separator, $MiddleName
}
# Setup required attributes
[string]$baseUPN = ('{0}{1}{2}{3}@{4}' -f $FirstName, $middleNamePart, $Separator, $LastName, $UPNSuffix).ToLower()
[string]$uniqueUPN = $baseUPN
[int]$counter = 1
while (Test-UPNExist -UPN $uniqueUPN -Server $ADServer)
{
$uniqueUPN = '{0}{1}@{2}' -f ($baseUPN.Split('@')[0]), $counter, $UPNSuffix
$counter++
}
return $uniqueUPN
}