-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathxExchangeHelper.psm1
2391 lines (2021 loc) · 64.1 KB
/
xExchangeHelper.psm1
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
Gets the existing Remote PowerShell session to Exchange, if it exists
#>
function Get-ExistingRemoteExchangeSession
{
[CmdletBinding()]
param ()
return (Get-PSSession -Name 'DSCExchangeSession' -ErrorAction SilentlyContinue)
}
<#
.SYNOPSIS
Establishes an Exchange remote PowerShell session to the local server.
Reuses the session if it already exists.
.PARAMETER Credential
The Credentials to use when creating a remote PowerShell session to
Exchange.
.PARAMETER CommandsToLoad
A list of the cmdlets that should be imported in the remote PowerShell
session.
.PARAMETER SetupProcessName
The name of the primary Exchange Setup process. If this process is
detected by this function, an exception will be thrown.
#>
function Get-RemoteExchangeSession
{
[CmdletBinding()]
param
(
[Parameter()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,
[Parameter()]
[System.String[]]
$CommandsToLoad,
[Parameter()]
[System.String]
$SetupProcessName = 'ExSetup*'
)
# Check if Exchange Setup is running. If so, we need to throw an exception, as a running Exchange DSC resource will block Exchange Setup from working properly.
if (Test-ExchangeSetupRunning -SetupProcessName $SetupProcessName)
{
throw 'Exchange Setup is currently running. Preventing creation of new Remote PowerShell session to Exchange.'
}
# See if the session already exists
$Session = Get-ExistingRemoteExchangeSession
# Attempt to reuse the session if we found one
if ($null -ne $Session)
{
if ($Session.State -eq 'Opened')
{
Write-Verbose -Message 'Reusing existing Remote Powershell Session to Exchange'
}
else # Session is in an unexpected state. Remove it so we can rebuild it
{
Remove-RemoteExchangeSession
$Session = $null
}
}
# Either the session didn't exist, or it was broken and we nulled it out. Create a new one
if ($null -eq $Session)
{
# First make sure we are on a valid server version, and that Exchange is fully installed
if (!(Test-ExchangeSetupComplete -Verbose:$VerbosePreference))
{
throw 'A supported version of Exchange is either not present, or not fully installed on this machine.'
}
Write-Verbose -Message 'Creating new Remote Powershell session to Exchange'
# Get local server FQDN
$machineDomain = (Get-CimInstance -ClassName Win32_ComputerSystem).Domain.ToLower()
$serverName = $env:computername.ToLower()
$serverFQDN = $serverName + "." + $machineDomain
# Override chatty banner, because chatty
New-Alias Get-ExBanner Out-Null
New-Alias Get-Tip Out-Null
# Load built in Exchange functions, and create session
$exbin = Join-Path -Path ((Get-ItemProperty HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\Setup).MsiInstallPath) -ChildPath "bin"
$remoteExchange = Join-Path -Path "$exbin" -ChildPath 'RemoteExchange.ps1'
. $remoteExchange
$Session = _NewExchangeRunspace -fqdn $serverFQDN -credential $Credential -UseWIA $false -AllowRedirection $false
# Remove the aliases we created earlier
Remove-Item Alias:Get-ExBanner
Remove-Item Alias:Get-Tip
if ($null -ne $Session)
{
$Session.Name = 'DSCExchangeSession'
}
}
# If the session is still null here, things went wrong. Throw exception
if ($null -eq $Session)
{
throw "Failed to establish remote Powershell session to FQDN: $serverFQDN"
}
else # Import the session globally
{
# Temporarily set Verbose to SilentlyContinue so the Session and Module import isn't noisy
$oldVerbose = $VerbosePreference
$VerbosePreference = 'SilentlyContinue'
if ($CommandsToLoad.Count -gt 0)
{
$moduleInfo = Import-PSSession $Session -WarningAction SilentlyContinue -DisableNameChecking -AllowClobber -CommandName $CommandsToLoad -Verbose:0
}
else
{
$moduleInfo = Import-PSSession $Session -WarningAction SilentlyContinue -DisableNameChecking -AllowClobber -Verbose:0
}
Import-Module $moduleInfo -Global -DisableNameChecking
# Set Verbose back
$VerbosePreference = $oldVerbose
}
}
<#
.SYNOPSIS
Removes any Remote Exchange PowerShell Sessions that have been setup by
xExchange.
#>
function Remove-RemoteExchangeSession
{
[CmdletBinding()]
param ()
$sessions = Get-ExistingRemoteExchangeSession
if ($null -ne $sessions)
{
Write-Verbose -Message 'Removing existing remote Powershell sessions'
Get-ExistingRemoteExchangeSession | Remove-PSSession
}
}
<#
.SYNOPSIS
Checks whether a supported version of Exchange is at least partially
installed.
#>
function Test-ExchangePresent
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
$version = Get-ExchangeVersionYear
if ($version -in '2013','2016','2019')
{
return $true
}
else
{
return $false
}
}
<#
.SYNOPSIS
Gets the installed Exchange Version, and returns the number as a
string. Returns N/A if the version cannot be found, and will
optionally throw an exception if ThrowIfUnknownVersion was set to
$true.
.PARAMETER ThrowIfUnknownVersion
Whether the function should throw an exception if the version cannot
be found. Defauls to $false.
#>
function Get-ExchangeVersionYear
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter()]
[System.Boolean]
$ThrowIfUnknownVersion = $false
)
$version = 'N/A'
$installedVersionDetails = Get-DetailedInstalledVersion
if ($null -ne $installedVersionDetails)
{ # If Exchange is installed
switch ($installedVersionDetails.VersionMajor)
{
15
{
switch ($installedVersionDetails.VersionMinor)
{
0 {$version = '2013'}
1 {$version = '2016'}
2 {$version = '2019'}
}
}
}
}
if ($version -eq 'N/A' -and $ThrowIfUnknownVersion)
{
throw 'Failed to discover a known Exchange Version'
}
return $version
}
<#
.SYNOPSIS
Function to read installed Exchange's Uninstall information from registry.
The function returns with the Uninstall registry key object.
#>
function Get-ExchangeUninstallKey
{
[CmdletBinding()]
[OutputType([Microsoft.Win32.RegistryKey])]
param()
# First try to get the Exchange 2016 / 2019 uninstall key.
$uninstallKey = Get-Item -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{CD981244-E9B8-405A-9026-6AEB9DCEF1F1}' -ErrorAction SilentlyContinue
# If the first key attempt is NULL, this may be a 2013 server. Try the 2013 key.
if ($null -eq $uninstallKey)
{
$uninstallKey = Get-Item -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{4934D1EA-BE46-48B1-8847-F1AF20E892C1}' -ErrorAction SilentlyContinue
}
return $uninstallKey
}
<#
.SYNOPSIS
Gets installed Exchange's buildnumber, which refers to the installed updates,
and returns a hashtable with Major, Minor, Update versions.
Returns NULL if the version cannot be found, and will optionally throw
an exception if ThrowIfUnknownVersion was set to $true.
#>
function Get-DetailedInstalledVersion
{
[CmdletBinding()]
[OutputType([System.Management.Automation.PSCustomObject])]
param()
$installedVersionDetails = $null
$uninstallKey = Get-ExchangeUninstallKey
if ($null -ne $uninstallKey)
{
$uninstallKeyPath = $uninstallKey.Name.ToLower().Replace('hkey_local_machine','hklm:')
$DisplayVersion = Get-ItemProperty -Path $uninstallKeyPath -Name 'DisplayVersion' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty DisplayVersion
$VersionBuild = $null
$DisplayVersion -match '(?<VersionMajor>\d+).(?<VersionMinor>\d+).(?<VersionBuild>\d+)'
if($Matches)
{
$VersionBuild = $Matches['VersionBuild']
}
$versionDetails = @{
VersionMajor = Get-ItemProperty -Path $uninstallKeyPath -Name 'VersionMajor' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty VersionMajor
VersionMinor = Get-ItemProperty -Path $uninstallKeyPath -Name 'VersionMinor' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty VersionMinor
VersionBuild = $VersionBuild
DisplayVersion = $DisplayVersion
}
$installedVersionDetails = New-Object -TypeName PSCustomObject -Property $versionDetails
}
return $installedVersionDetails
}
<#
.SYNOPSIS
Returns whether Exchange Setup has fully and successfully completed.
#>
function Test-ExchangeSetupComplete
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
$exchangePresent = Test-ExchangePresent
$setupPartiallyCompleted = Test-ExchangeSetupPartiallyCompleted -Verbose:$VerbosePreference
if ($exchangePresent -eq $true -and $setupPartiallyCompleted -eq $false)
{
Write-Verbose -Message 'Exchange is present and setup is detected as being fully complete.'
$isSetupComplete = $true
}
else
{
Write-Verbose -Message "Exchange setup detected as not being fully complete. Exchange Present: $exchangePresent. Setup Partially Complete: $setupPartiallyCompleted."
$isSetupComplete = $false
}
return $isSetupComplete
}
<#
.SYNOPSIS
Checks whether any Setup watermark keys exist which means that a
previous installation of setup had already started but not completed.
#>
function Test-ExchangeSetupPartiallyCompleted
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
Write-Verbose -Message 'Checking if setup is partially complete'
$isPartiallyCompleted = $false
# Now check if setup actually completed successfully
[System.String[]] $roleKeys = @( 'CafeRole', 'ClientAccessRole', 'FrontendTransportRole', 'HubTransportRole', 'MailboxRole', 'UnifiedMessagingRole' )
foreach ($key in $roleKeys)
{
$values = $null
$values = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\$key" -ErrorAction SilentlyContinue
if ($null -ne $values)
{
Write-Verbose -Message "Checking values at key 'HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\$key'"
if ($null -ne $values.UnpackedVersion)
{
# If ConfiguredVersion is missing, or Action or Watermark or present, setup needs to be resumed
if ($null -eq $values.ConfiguredVersion)
{
Write-Warning -Message "Registry value missing. Setup considered partially complete. Location: 'HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\$key\ConfiguredVersion'."
$isPartiallyCompleted = $true
}
if ($null -ne $values.Action)
{
Write-Warning -Message "Registry value present. Setup considered partially complete. Location: 'HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\$key\Action'. Value: '$($values.Action)'."
$isPartiallyCompleted = $true
}
if ($null -ne $values.Watermark)
{
Write-Warning -Message "Registry value present. Setup considered partially complete. Location: 'HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\$key\Watermark'. Value: '$($values.Watermark)'."
$isPartiallyCompleted = $true
}
}
}
}
return $isPartiallyCompleted
}
<#
.SYNOPSIS
Gets Exchange's setup.exe file's version info.
It will return VersionMajor, VersionMinor, VersionBuild values as PSCustomObject
or NULL if not readable.
.PARAMETER Path
The path of the setup.exe which is used within the xExchInstall DSC resource.
#>
function Get-SetupExeVersion
{
[CmdletBinding()]
[OutputType([System.Management.Automation.PSCustomObject])]
param
(
[Parameter(Mandatory=$true)]
[System.String]
$Path
)
$version = $null
# Get Exchange setup.exe version
if(Test-Path -Path $Path -ErrorAction SilentlyContinue)
{
$setupexeVersionInfo = (Get-ChildItem -Path $Path).VersionInfo
$setupexeVersionInfo = @{
VersionMajor = [System.Int32] $setupexeVersionInfo.ProductMajorPart
VersionMinor = [System.Int32] $setupexeVersionInfo.ProductMinorPart
VersionBuild = [System.Int32] $setupexeVersionInfo.ProductBuildPart
}
$version = New-Object -TypeName PSCustomObject -Property $setupexeVersionInfo
}
return $version
}
<#
.SYNOPSIS
Checks if installed Exchange version is older than the version of the setup.exe,
which is used within the xExchInstall DSC Resource call.
Will return Boolean.
.PARAMETER Path
The path of the setup.exe which is used within the xExchInstall DSC resource.
.PARAMETER Arguments
The commandline arguments of setup.exe.
#>
function Test-ShouldUpgradeExchange
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory=$true)]
[System.String]
$Path,
[Parameter(Mandatory=$true)]
[System.String]
$Arguments
)
$shouldUpgrade = $false
if(($Arguments -notmatch '/mode:upgrade') -and ($Arguments -notmatch '/m:upgrade'))
{
return $shouldUpgrade
}
$setupExeVersion = Get-SetupExeVersion -Path $Path
if($null -ne $setupExeVersion`
-and $null -ne $setupExeVersion.VersionMajor`
-and $null -ne $setupExeVersion.VersionMinor`
-and $null -ne $setupExeVersion.VersionBuild)
{
Write-Verbose -Message "Setup.exe version is: '$('Major: {0}, Minor: {1}, Build: {2}' -f $setupExeVersion.VersionMajor,$setupexeVersion.VersionMinor, $setupexeVersion.VersionBuild)'"
$exchangeDisplayVersion = Get-DetailedInstalledVersion
if($null -ne $exchangeDisplayVersion`
-and $null -ne $exchangeDisplayVersion.VersionMajor`
-and $null -ne $exchangeDisplayVersion.VersionMinor`
-and $null -ne $exchangeDisplayVersion.VersionBuild)
{ # If we have an exchange installed
Write-Verbose -Message "Comparing setup.exe version and installed Exchange's version."
Write-Verbose -Message "Exchange version is: '$('Major: {0}, Minor: {1}, Build: {2}' -f $exchangeDisplayVersion.Major,$exchangeDisplayVersion.Minor, $exchangeDisplayVersion.Build)'"
if(($exchangeDisplayVersion.VersionMajor -eq $setupExeVersion.VersionMajor)`
-and ($exchangeDisplayVersion.VersionMinor -eq $setupExeVersion.VersionMinor)`
-and ($exchangeDisplayVersion.VersionBuild -lt $setupExeVersion.VersionBuild) )
{ # If server has lower version of CU installed
Write-Verbose -Message 'Version upgrade is requested.'
# Executing with the upgrade.
$shouldUpgrade = $true
}
else
{
Write-Verbose -Message 'Exchange update is not possible. Version of installed Exchange cannot be updated with the version of setup.exe.'
}
}
else
{
Write-Error -Message "Get-ExchangeInstallStatus: Script cannot determine installed Exchange's version. Please check if Exchange is installed."
}
}
else
{
Write-Error -Message "Get-ExchangeInstallStatus: Script cannot determine setup.exe version. Please check the file '$Path'."
}
return $shouldUpgrade
}
<#
.SYNOPSIS
Checks for the exact status of Exchange setup and returns the results
in a Hashtable.
.PARAMETER Path
The path of the setup.exe which is used within the xExchInstall DSC resource.
.PARAMETER Arguments
The command line arguments to be passed to Exchange Setup.
#>
function Get-ExchangeInstallStatus
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter()]
[System.String]
$Path,
[Parameter()]
[System.String]
$Arguments
)
Write-Verbose -Message 'Checking Exchange Install Status'
$shouldStartInstall = $false
$shouldInstallLanguagePack = Test-ShouldInstallUMLanguagePack -Arguments $Arguments
$setupRunning = Test-ExchangeSetupRunning
$setupComplete = Test-ExchangeSetupComplete -Verbose:$VerbosePreference
$exchangePresent = Test-ExchangePresent
# Exchange CU install / update support
$shouldUpgrade = Test-ShouldUpgradeExchange -Path $Path -Arguments $Arguments -Verbose:$VerbosePreference
if ($setupRunning -or $setupComplete)
{
if(($shouldInstallLanguagePack -or $shouldUpgrade) -and $setupComplete)
{
$shouldStartInstall = $true
}
else
{
# Do nothing. Either Install is already running, or it's already finished successfully
}
}
elseif (!$setupComplete)
{
$shouldStartInstall = $true
}
Write-Verbose -Message "Finished Checking Exchange Install Status. ShouldInstallLanguagePack: $shouldInstallLanguagePack. SetupRunning: $setupRunning. SetupComplete: $setupComplete. ExchangePresent: $exchangePresent. ShouldStartInstall: $shouldStartInstall."
$returnValue = @{
ShouldInstallLanguagePack = $shouldInstallLanguagePack
SetupRunning = $setupRunning
SetupComplete = $setupComplete
ExchangePresent = $exchangePresent
ShouldUpgrade = $shouldUpgrade
ShouldStartInstall = $shouldStartInstall
}
$returnValue
}
<#
.SYNOPSIS
Check for missing registry keys that may cause Exchange setup to try to
restart WinRM mid setup , which will in turn cause the DSC resource to
fail. If any required keys are missing, configure WinRM, then force a
reboot.
#>
function Set-WSManConfigStatus
{
# Suppressing this rule because $global:DSCMachineStatus is used to trigger a reboot.
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]
<#
Suppressing this rule because $global:DSCMachineStatus is only set,
never used (by design of Desired State Configuration).
#>
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Scope='Function', Target='DSCMachineStatus')]
[CmdletBinding()]
[OutputType([System.Boolean])]
param ()
$needReboot = $false
$wsmanKey = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN' -ErrorAction SilentlyContinue
if ($null -ne $wsmanKey)
{
if ($null -eq $wsmanKey.UpdatedConfig)
{
$needReboot = $true
Write-Verbose -Message "Value 'UpdatedConfig' missing from registry key HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN. Running: winrm i restore winrm/config"
Set-Location "$($env:windir)\System32\inetsrv"
winrm i restore winrm/config | Out-Null
Write-Verbose -Message 'Machine needs to be rebooted before Exchange setup can proceed'
$global:DSCMachineStatus = 1
}
}
else
{
throw 'Unable to find registry key: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN'
}
return $needReboot
}
<#
.SYNOPSIS
Given the specified Exchange Setup arguments, determines whether an
Exchange UM Language Pack should be installed or not.
.PARAMETER Arguments
The command line arguments to be passed to Exchange Setup.
#>
function Test-ShouldInstallUMLanguagePack
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[System.String]
$Arguments
)
if($Arguments -match '(?<=/AddUMLanguagePack:)(([a-z]{2}-[A-Z]{2},?)+)(?=\s)')
{
$Cultures = $Matches[0]
Write-Verbose -Message "AddUMLanguagePack parameters detected: $Cultures"
$Cultures = $Cultures -split ','
foreach($Culture in $Cultures)
{
if((Test-UMLanguagePackInstalled -Culture $Culture) -eq $false)
{
Write-Verbose -Message "UM Language Pack: $Culture is not installed"
return $true
}
}
}
return $false
}
<#
.SYNOPSIS
Checks whether Exchange Setup is running by checking if the
ExSetup.exe process currently exists as a running process.
.PARAMETER SetupProcessName
The name of the process to check if running. Defaults to ExSetup*.
#>
function Test-ExchangeSetupRunning
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[System.String]
$SetupProcessName = 'ExSetup*'
)
return ($null -ne (Get-Process -Name $SetupProcessName -ErrorAction SilentlyContinue))
}
<#
.SYNOPSIS
Checks if two strings are equal, or are both either null or empty.
If IgnoreCase is specified, returns true if both strings are like
each other, regardless of case. Without IgnoreCase, only returns
true if both strings are identical. Also returns true if both strings
are either null or empty. Returns false for all other cases.
.PARAMETER String1
The first System.String object to compare.
.PARAMETER String2
The second System.String object to compare
.PARAMETER IgnoreCase
Whether case should be ignored when comparing the two strings.
#>
function Compare-StringToString
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[System.String]
$String1,
[Parameter()]
[System.String]
$String2,
[Parameter()]
[System.Management.Automation.SwitchParameter]
$IgnoreCase
)
if (([System.String]::IsNullOrEmpty($String1) -and [System.String]::IsNullOrEmpty($String2)))
{
return $true
}
else
{
if ($IgnoreCase -eq $true)
{
return ($String1 -like $String2)
}
else
{
return ($String1 -clike $String2)
}
}
}
<#
.SYNOPSIS
Compares two Nullable Boolean objects and returns true if they are both
set to true, both set to false, or both set to either null or false.
.PARAMETER Bool1
The first System.Boolean object to compare.
.PARAMETER Bool2
The second System.Boolean object to compare.
#>
function Compare-BoolToBool
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[Nullable[System.Boolean]]
$Bool1,
[Parameter()]
[Nullable[System.Boolean]]
$Bool2
)
if ($Bool1 -ne $Bool2)
{
if (!(($null -eq $Bool1 -and $Bool2 -eq $false) -or ($null -eq $Bool2 -and $Bool1 -eq $false)))
{
return $false
}
}
return $true
}
<#
.SYNOPSIS
Takes a string which should be in timespan format, and compares it to
an actual EnhancedTimeSpan object. Returns true if they are equal.
.PARAMETER TimeSpan
The Microsoft.Exchange.Data.EnhancedTimeSpan object to compare.
.PARAMETER String
The System.String object to compare.
#>
function Compare-TimespanToString
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[Microsoft.Exchange.Data.EnhancedTimeSpan]
$TimeSpan,
[Parameter()]
[System.String]
$String
)
try
{
$converted = [Microsoft.Exchange.Data.EnhancedTimeSpan]::Parse($String)
return ($TimeSpan.Equals($converted))
}
catch
{
throw "String '$String' is not in a valid format for an EnhancedTimeSpan"
}
return $false
}
<#
.SYNOPSIS
Takes a string which should be in ByteQuantifiedSize format, and
compares it to an actual ByteQuantifiedSize object. Returns true if
they are equal.
.PARAMETER ByteQuantifiedSize
The Microsoft.Exchange.Data.ByteQuantifiedSize object to compare.
.PARAMETER String
The System.String object to compare.
#>
function Compare-ByteQuantifiedSizeToString
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[Microsoft.Exchange.Data.ByteQuantifiedSize]
$ByteQuantifiedSize,
[Parameter()]
[System.String]
$String
)
try
{
$converted = [Microsoft.Exchange.Data.ByteQuantifiedSize]::Parse($String)
return ($ByteQuantifiedSize.Equals($converted))
}
catch
{
throw "String '$String' is not in a valid format for a ByteQuantifiedSize"
}
}
<#
.SYNOPSIS
Takes a string which should be in Microsoft.Exchange.Data.Unlimited
format, and compares with an actual Unlimited object. Returns true if
they are equal.
.PARAMETER Unlimited
The Microsoft.Exchange.Data.Unlimited object to compare.
.PARAMETER String
The System.String object to compare.
#>
function Compare-UnlimitedToString
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[System.Object]
$Unlimited,
[Parameter()]
[System.String]
$String
)
if ($Unlimited.IsUnlimited)
{
return (Compare-StringToString -String1 'Unlimited' -String2 $String -IgnoreCase)
}
elseif ((Compare-StringToString -String1 'Unlimited' -String2 $String -IgnoreCase) -and !$Unlimited.IsUnlimited)
{
return $false
}
elseif (($Unlimited.Value -is [System.Int32]) -and !$Unlimited.IsUnlimited)
{
return (Compare-StringToString -String1 $Unlimited -String2 $String -IgnoreCase)
}
else
{
return (Compare-ByteQuantifiedSizeToString -ByteQuantifiedSize $Unlimited -String $String)
}
}
<#
.SYNOPSIS
Takes an ADObjectId, gets a recipient from it using Get-Recipient, and
checks if the EmailAddresses property contains the given AddressString.
The Get-Recipient cmdlet must be loaded for this function to succeed.
.PARAMETER ADObjectId
The ADObjectID to run Get-Recipient against and compare against the
given AddressString.
.PARAMETER AddressString
The AddressString to compare against the EmailAddresses property of the
Get-Recipient results.
#>
function Compare-ADObjectIdToSmtpAddressString
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter()]
[System.Object]
$ADObjectId,
[Parameter()]
[System.String]
$AddressString
)
if ($null -ne (Get-Command -Name 'Get-Recipient' -ErrorAction SilentlyContinue))
{
if ($null -eq $ADObjectId -and ![System.String]::IsNullOrEmpty($AddressString))
{
return $false
}
elseif ($null -ne $ADObjectId -and [System.String]::IsNullOrEmpty($AddressString))
{
return $false
}
elseif ($null -eq $ADObjectId -and [System.String]::IsNullOrEmpty($AddressString))
{
return $true
}
$recipient = Get-Recipient -Identity $ADObjectId.DistinguishedName -ErrorAction SilentlyContinue
if ($null -eq $recipient)
{
throw "Failed to Get-Recipient for ADObjectID with distinguishedName: $($ADObjectId.DistinguishedName)"
}
return ($null -ne ($recipient.EmailAddresses | Where-Object {$_.AddressString -like $AddressString}))
}
else
{
throw 'Compare-ADObjectIdToSmtpAddressString requires the Get-Recipient cmdlet. Make sure this is in the RBAC scope of the executing user account.'
}
}
<#
.SYNOPSIS
Takes a string containing a given separator, and breaks it into a
string array.
.PARAMETER StringIn
The System.String object to split into an array.
.PARAMETER Separator
The System.Char object to use as a separater when splitting the
given System.String object.
#>
function Convert-StringToArray
{
[CmdletBinding()]
[OutputType([System.String[]])]
param
(
[Parameter()]
[System.String]
$StringIn,
[Parameter()]
[System.Char]
$Separator
)
[System.String[]] $array = $StringIn.Split($Separator)
for ($i = 0; $i -lt $array.Length; $i++)
{
$array[$i] = $array[$i].Trim()
}
return $array
}
<#
.SYNOPSIS
Takes an array of strings and converts each element in the array to
all lowercase characters.
.PARAMETER Array
The array of System.String objects to convert into lowercase strings.
#>
function Convert-StringArrayToLowerCase
{
[CmdletBinding()]
[OutputType([System.String[]])]
param
(
[Parameter()]
[System.String[]]
$Array