-
Notifications
You must be signed in to change notification settings - Fork 10
/
start-parameterValidation.ps1
403 lines (353 loc) · 19.7 KB
/
start-parameterValidation.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<#
.SYNOPSIS
This function validates the parameters within the script. Paramter validation is shared across functions.
.DESCRIPTION
This function validates the parameters within the script. Paramter validation is shared across functions.
#>
Function start-parameterValidation
{
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true,ParameterSetName = 'AADConnect')]
[Parameter(Mandatory = $true,ParameterSetName = 'AADConnectMulti')]
[AllowNull()]
$aadConnectServer,
[Parameter(Mandatory = $true,ParameterSetName = 'AADConnect')]
[Parameter(Mandatory = $true,ParameterSetName = 'AADConnectMulti')]
[AllowNull()]
$aadConnectCredential,
[Parameter(Mandatory = $true,ParameterSetName = 'AADConnectMulti')]
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeMulti')]
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeOnlineMulti')]
[Parameter(Mandatory = $true,ParameterSetName = 'AzureADMulti')]
[Parameter(Mandatory = $true,ParameterSetName = 'MaxThreadCount')]
[Parameter(Mandatory = $true,ParameterSetName = 'ActiveDirectory')]
[AllowNull()]
$serverNames,
[Parameter(Mandatory = $true,ParameterSetName = 'Exchange')]
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeMulti')]
[AllowNull()]
$exchangeServer,
[Parameter(Mandatory = $true,ParameterSetName = 'Exchange')]
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeMulti')]
[AllowNull()]
$exchangeCredential,
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeOnline')]
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeOnlineMulti')]
[AllowNull()]
$exchangeOnlineCredential,
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeOnline')]
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeOnlineMulti')]
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeOnlineCertAuth')]
[AllowNull()]
$exchangeOnlineCertificateThumbprint,
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeOnlineCertAuth')]
[AllowNull()]
$exchangeOnlineOrganizationName,
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeOnlineCertAuth')]
[AllowNull()]
$exchangeOnlineAppID,
[Parameter(Mandatory = $true,ParameterSetName = 'AzureAD')]
[Parameter(Mandatory = $true,ParameterSetName = 'AzureADMulti')]
[AllowNull()]
$azureADCredential,
[Parameter(Mandatory = $true,ParameterSetName = 'AzureAD')]
[Parameter(Mandatory = $true,ParameterSetName = 'AzureADMulti')]
[Parameter(Mandatory = $true,ParameterSetName = 'AzureADCertAuth')]
[AllowNull()]
$azureCertificateThumbprint,
[Parameter(Mandatory = $true,ParameterSetName = 'AzureADCertAuth')]
[AllowNull()]
$azureTenantID,
[Parameter(Mandatory = $true,ParameterSetName = 'AzureADCertAuth')]
[AllowNull()]
$azureApplicationID,
[Parameter(Mandatory = $true,ParameterSetName = 'NoSyncOU')]
[AllowNull()]
$retainOriginalGroup,
[Parameter(Mandatory = $true,ParameterSetName = 'NoSyncOU')]
[AllowNull()]
$doNoSyncOU,
[Parameter(Mandatory = $true,ParameterSetName = 'HybridMailFlow')]
[AllowNull()]
$useOnPremisesExchange,
[Parameter(Mandatory = $true,ParameterSetName = 'HybridMailFlow')]
[AllowNull()]
$enableHybridMailFlow,
[Parameter(Mandatory = $true,ParameterSetName = 'ActiveDirectory')]
[AllowNull()]
$activeDirectoryCredential,
[Parameter(Mandatory = $true,ParameterSetName = 'MaxThreadCount')]
[AllowNull()]
$maxThreadCount,
[Parameter(Mandatory = $true,ParameterSetName = 'RemoteDriveLetter')]
[AllowNull()]
$remoteDriveLetter,
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeOnline')]
[Parameter(Mandatory = $true,ParameterSetName = 'ExchangeOnlineMulti')]
[Parameter(Mandatory = $true,ParameterSetName = 'AzureAD')]
[AllowNull()]
$threadCount=0,
[Parameter(Mandatory = $true,ParameterSetName = 'msGraphCertAuth')]
[AllowNull()]
$msGraphCertificateThumbprint,
[Parameter(Mandatory = $true,ParameterSetName = 'msGraphCertAuth')]
[AllowNull()]
$msGraphTenantID,
[Parameter(Mandatory = $true,ParameterSetName = 'msGraphCertAuth')]
[AllowNull()]
$msGraphApplicationID
)
#Output all parameters bound or unbound and their associated values.
write-functionParameters -keyArray $MyInvocation.MyCommand.Parameters.Keys -parameterArray $PSBoundParameters -variableArray (Get-Variable -Scope Local -ErrorAction Ignore)
$functionParameterSetName = $PsCmdlet.ParameterSetName
$aadConnectParameterSetName = 'AADConnect'
$aadConnectParameterSetNameMulti = 'AADConnectMulti'
$exchangeParameterSetName = "Exchange"
$exchangeParameterSetNameMulti = "ExchangeMulti"
$exchangeOnlineParameterSetName = "ExchangeOnline"
$exchangeOnlineParameterSetNameMulti = "ExchangeOnlineMulti"
$exchangeOnlineParameterSetNameCertAuth = "ExchangeOnlineCertAuth"
$azureADParameterSetName = "AzureAD"
$azureADParameterSetNameMulti = "AzureADMulti"
$azureADParameterSetNameCertAuth = "AzureCertAuth"
$msGraphParameterSetNameCertAuth = "MSGraphCertAuth"
$doNoSyncOUParameterSetName = "NoSyncOU"
$hybridMailFlowParameterSetName = "HybridMailFlow"
$activeDirectoryParameterSetName = "ActiveDirectory"
$maxThreadCountParameterSetName = "MaxThreadCount"
$remoteDriveLetterParameterSetName = "RemoteDriveLetter"
$functionTrueFalse = $false
#Start function processing.
Out-LogFile -string "********************************************************************************"
Out-LogFile -string "BEGIN start-parameterValidation"
Out-LogFile -string "********************************************************************************"
out-logfile -string ("The parameter set name for validation: "+$functionParameterSetName)
if ($functionParameterSetName -eq $remoteDriveLetterParameterSetName)
{
if ($remoteDriveLetter.length -ne 1)
{
out-logfile -string "Please specify a single drive letter - for example S" -isError:$TRUE
}
else
{
out-logfile -string "Drive letter specified is a single character."
if ([regex]::Match($remoteDriveLetter,"[a-zA-Z]"))
{
out-logfile -string "Drive letter specified is single and is a valid drive character."
}
else
{
out-logfile -string "Please specify a valid character A-Z or a-z for the remote drive letter." -iserror:$TRUE
}
}
}
if ($functionParameterSetName -eq $maxThreadCountParameterSetName)
{
if ($serverNames.count -gt $maxThreadCount)
{
out-logfile -string ("More servers were specified than the hard coded thread count which is = "+$maxThreadCount.tostring()) -isError:$TRUE
}
else
{
out-logfile -string "The number of servers provided is less than the max thread count."
}
}
if ($functionParameterSetName -eq $activeDirectoryParameterSetName)
{
test-credentials -credentialsToTest $activeDirectoryCredential
test-itemCount -itemsToCount $activeDirectoryCredential -itemsToCompareCount $serverNames
}
if ($functionParameterSetName -eq $hybridMailFLowParameterSetName)
{
if (($useOnPremisesExchange -eq $False) -and ($enableHybridMailflow -eq $true))
{
out-logfile -string "Exchange on premises information must be provided in order to enable hybrid mail flow." -isError:$TRUE
}
}
if ($functionParameterSetName -eq $doNoSyncOUParameterSetName)
{
if (($retainOriginalGroup -eq $FALSE) -and ($dnNoSyncOU -eq "NotSet"))
{
out-LogFile -string "A no SYNC OU is required if retain original group is false." -isError:$TRUE
}
}
if ($functionParamterSetName -eq $azureADParameterSetNameCertAuth)
{
if (($azureCertificateThumbprint -ne "") -and ($azureTenantID -eq "") -and ($azureApplicationID -eq ""))
{
out-logfile -string "The azure tenant ID and Azure App ID are required when using certificate authentication to Azure." -isError:$TRUE
}
elseif (($azureCertificateThumbprint -ne "") -and ($AzureTenantID -ne "") -and ($azureApplicationID -eq ""))
{
out-logfile -string "The azure app id is required to use certificate authentication to Azure." -isError:$TRUE
}
elseif (($azureCertificateThumbprint -ne "") -and ($azureTenantID -eq "") -and ($azureApplicationID -ne ""))
{
out-logfile -string "The azure tenant ID is required to use certificate authentication to Azure." -isError:$TRUE
}
elseif (($azureCertificateThumbprint -eq "") -and ($azureTenantID -eq "") -and ($azureApplicationID -ne ""))
{
out-logfile -string "No components of Azure AD Cert Authentication were provided - this is not necessarily an issue."
}
else
{
out-logfile -string "All components necessary for Exchange certificate thumbprint authentication were specified."
}
}
if ($functionParameterSetName -eq $msGraphParameterSetNameCertAuth)
{
if (($msGraphCertificateThumbprint -ne "") -and ($msGraphTenantID -eq "") -and ($msGraphApplicationID -eq ""))
{
out-logfile -string "The msGraph tenant ID and msGraph App ID are required when using certificate authentication to msGraph." -isError:$TRUE
}
elseif (($msGraphCertificateThumbprint -ne "") -and ($msGraphTenantID -ne "") -and ($msGraphApplicationID -eq ""))
{
out-logfile -string "The msGraph app id is required to use certificate authentication to msGraph." -isError:$TRUE
}
elseif (($msGraphCertificateThumbprint -ne "") -and ($msGraphTenantID -eq "") -and ($msGraphApplicationID -ne ""))
{
out-logfile -string "The msGraph tenant ID is required to use certificate authentication to msGraph." -isError:$TRUE
}
elseif (($msGraphCertificateThumbprint -eq "") -and ($msGraphTenantID -eq "") -and ($msGraphApplicationID -ne ""))
{
out-logfile -string "No components of msGraph Cert Authentication were provided - this is not necessarily an issue."
}
else
{
out-logfile -string "All components necessary for Exchange certificate thumbprint authentication were specified."
}
}
if ($functionParameterSetName -eq $azureADParameterSetName)
{
if (($azureADCredential -ne $NULL) -and ($azureCertificateThumbprint -ne ""))
{
Out-LogFile -string "ERROR: Only one method of azure cloud authentication can be specified. Use either azure cloud credentials or azure cloud certificate thumbprint." -isError:$TRUE
}
elseif (($azureADCredential -eq $NULL) -and ($azureCertificateThumbprint -eq "") -and ($threadCount -ne 0))
{
out-logfile -string "ERROR: One permissions method to connect to Azure AD must be specified."
out-logfile -string "https://timmcmic.wordpress.com/2022/09/18/office-365-distribution-list-migration-version-2-0-part-20/" -isError:$TRUE
}
else
{
Out-LogFile -string "Only one method of Azure AD specified."
if ($functionParamterSetName -eq $azureADParameterSetNameMulti)
{
out-logfile -string "Validating the exchange online credential array"
test-credentials -credentialsToTest $azureADCredential
test-itemCount -itemsToCount $azureADCredential -itemsToCompareCount $serverNames
}
}
}
if ($functionParameterSetName -eq $exchangeOnlineParameterSetNameCertAuth)
{
if (($exchangeOnlineCertificateThumbPrint -ne "") -and ($exchangeOnlineOrganizationName -eq "") -and ($exchangeOnlineAppID -eq ""))
{
out-logfile -string "The exchange organization name and application ID are required when using certificate thumbprint authentication to Exchange Online." -isError:$TRUE
}
elseif (($exchangeOnlineCertificateThumbPrint -ne "") -and ($exchangeOnlineOrganizationName -ne "") -and ($exchangeOnlineAppID -eq ""))
{
out-logfile -string "The exchange application ID is required when using certificate thumbprint authentication." -isError:$TRUE
}
elseif (($exchangeOnlineCertificateThumbPrint -ne "") -and ($exchangeOnlineOrganizationName -eq "") -and ($exchangeOnlineAppID -ne ""))
{
out-logfile -string "The exchange organization name is required when using certificate thumbprint authentication." -isError:$TRUE
}
elseif (($exchangeOnlineCertificateThumbPrint -eq "") -and ($exchangeOnlineOrganizationName -eq "") -and ($exchangeOnlineAppID -eq ""))
{
out-logfile -string "No components of certificate authentication were specified. This is not necessary an error."
}
else
{
out-logfile -string "All components necessary for Exchange certificate thumbprint authentication were specified."
$functionTrueFalse = $TRUE
}
}
if (($functionParameterSetName -eq $exchangeOnlineParameterSetName) -or ($functionParameterSetName -eq $exchangeOnlineParameterSetNameMulti))
{
if (($exchangeOnlineCredential -ne $NULL) -and ($exchangeOnlineCertificateThumbPrint -ne ""))
{
Out-LogFile -string "ERROR: Only one method of cloud authentication can be specified. Use either cloud credentials or cloud certificate thumbprint." -isError:$TRUE
}
elseif (($exchangeOnlineCredential -eq $NULL) -and ($exchangeOnlineCertificateThumbPrint -eq "") -and ($threadCount -ne 0))
{
out-logfile -string "ERROR: One permissions method to connect to Exchange Online must be specified." -isError:$TRUE
}
else
{
Out-LogFile -string "Only one method of Exchange Online authentication specified."
if ($functionParamterSetName -eq $exchangeOnlineParameterSetNameMulti)
{
out-logfile -string "Validating the exchange online credential array"
test-credentials -credentialsToTest $exchangeOnlineCredential
test-itemCount -itemsToCount $exchangeOnlineCredential -itemsToCompareCount $serverNames
}
}
}
if (($functionParameterSetName -eq $exchangeParameterSetName) -or ($functionParameterSetName -eq $exchangeParameterSetNameMulti))
{
if (($exchangeServer -eq "") -and ($exchangeCredential -ne $null))
{
#The exchange credential was specified but the exchange server was not specified.
Out-LogFile -string "ERROR: Exchange Server is required when specifying Exchange Credential." -isError:$TRUE
}
elseif (($exchangeCredential -eq $NULL) -and ($exchangeServer -ne ""))
{
#The exchange server was specified but the exchange credential was not.
Out-LogFile -string "ERROR: Exchange Credential is required when specifying Exchange Server." -isError:$TRUE
}
elseif (($exchangeCredential -ne $NULL) -and ($exchangeServer -ne ""))
{
#The server name and credential were specified for Exchange.
Out-LogFile -string "The server name and credential were specified for Exchange."
#Set useOnPremisesExchange to TRUE since the parameters necessary for use were passed.
$functionTrueFalse=$TRUE
if ($functionParamterSetName -eq $exchangeParameterSetNameMulti)
{
test-credentials -credentialsToTest $exchangeCredential
test-itemCount -itemsToCount $exchangeCredential -itemsToCompareCount $serverNames
}
Out-LogFile -string "Set useOnPremsiesExchanget to TRUE since the parameters necessary for use were passed - "
}
else
{
Out-LogFile -string "Neither Exchange Server or Exchange Credentials specified - retain useOnPremisesExchange FALSE - "
}
}
if (($functionParameterSetName -eq $aadConnectParameterSetName) -or ($functionParameterSetName -eq $aadConnectParameterSetNameMulti))
{
if (($aadConnectServer -eq "") -and ($aadConnectCredential -ne $null))
{
#The credential was specified but the server name was not.
Out-LogFile -string "ERROR: AAD Connect Server is required when specifying AAD Connect Credential" -isError:$TRUE
}
elseif (($aadConnectCredential -eq $NULL) -and ($aadConnectServer -ne ""))
{
#The server name was specified but the credential was not.
Out-LogFile -string "ERROR: AAD Connect Credential is required when specifying AAD Connect Server" -isError:$TRUE
}
elseif (($aadConnectCredential -ne $NULL) -and ($aadConnectServer -ne ""))
{
#The server name and credential were specified for AADConnect.
Out-LogFile -string "AADConnectServer and AADConnectCredential were both specified."
#Set useAADConnect to TRUE since the parameters necessary for use were passed.
$functionTrueFalse=$TRUE
if ($functionParameterSetName -eq $aadConnectParameterSetNameMulti)
{
Out-LogFile -string "AADConnectServer and AADConnectCredential were both specified."
test-credentials -credentialsToTest $aadConnectCredential
test-itemCount -itemsToCount $aadConnectCredential -itemsToCompareCount $serverNames
}
}
else
{
Out-LogFile -string "Neither AADConnect Server or AADConnect Credentials specified."
}
}
Out-LogFile -string "********************************************************************************"
Out-LogFile -string "END start-parameterValidation"
Out-LogFile -string "********************************************************************************"
return $functionTrueFalse
}