Public/Import-MDSExchOnprem.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
Function Import-MDSExchOnprem {
    <#
    .SYNOPSIS
    Import the Exchange On-Premises PowerShell cmdlets using a MDSCredential

    .DESCRIPTION
    Import the Exchange On-Premises PowerShell cmdlets using a MDSCredential

    .EXAMPLE
    Import-MDSExchOnprem -MDSCredential MyCred1 -ExchangeServer MyServerName

    Import the Exchange On-Premises cmdlets with the stored 'MyCred1' credentials

    .EXAMPLE
    Import-MDSExchOnprem -MDSCredential MyCred1 -ExchangeServer MyServerName -ViewEntireForest

    Import the Exchange On-Premises cmdlets with the stored 'MyCred1' credentials and set the session's ADServerSettings to allow viewing the entire forest

    .EXAMPLE
    Import-MDSExchOnprem -MDSCredential MyCred1 -ExchangeServer MyServerName -Prefix OnPrem

    Import the EXO cmdlets with the stored 'MyCred1' credentials and prefix the cmdlets. For example Get-Mailbox becomes Get-OnPremMailbox. This allows you to load both the EXO cmdlets and Exchange cmdlets in the same session.

    .NOTES

    #>

    [System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingPlainTextForPassword','')]
    [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUsePSCredentialType','')]

    [CmdletBinding(DefaultParameterSetName = 'Credential')]
    Param(
        [parameter(
            Position = 0,
            Mandatory = $true,
            ParameterSetName = 'MDSCredential'
        )]
        [String]$MDSCredential,

        [parameter(
            Position = 0,
            Mandatory = $true,
            ParameterSetName = 'Credential'
        )]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.CredentialAttribute()]
        $Credential,

        [Parameter(
            Position = 1,
            Mandatory = $false,
            ParameterSetName = 'MDSCredential'
        )]
        [parameter(ParameterSetName = 'Credential')]
        [string]$ExchangeServer,

        [parameter(
            Position = 2,
            ParameterSetName = 'MDSCredential'
        )]
        [parameter(ParameterSetName = 'Credential')]
        [string]$Prefix,

        [parameter(
            Position = 3,
            ParameterSetName = 'MDSCredential'
        )]
        [parameter(ParameterSetName = 'Credential')]
        [switch]$ViewEntireForest
    )

    Begin {
        $SessionName = 'Microsoft.Exchange'
        If (Get-PSSession -Name $SessionName -ErrorAction SilentlyContinue) {
            Try {
                Remove-PSSession -Name $SessionName -ErrorAction Stop
                Write-Verbose "Session $($SessionName) removed"
            }
            Catch {}
        }
    }
    Process {
        Try {
            # MDSCredentials
            If ($PSBoundParameters.MDSCredential) {
                $Credential = Get-MDSCredential -Name $MDSCredential -ErrorAction Stop
            }

            # Exchange Server Query
            If (-not $PSBoundParameters.ExchangeServer) {
                $ExchangeServer = Get-MDSExchServerFromLDAP -Random -ErrorAction Stop | Select-Object -Expand FQDN
            }

            # New-PSSession
            $SessionParameters = @{
                Name              = $SessionName
                ConfigurationName = 'Microsoft.Exchange'
                ConnectionUri     = "http://$($ExchangeServer)/Powershell/?SerializationLevel=Full"
                Credential        = $Credential
                Authentication    = 'Kerberos'
            }
            $Session = New-PSSession @SessionParameters -ErrorAction Stop

            # Import-PSSession
            $PSSessionParameters = @{
                Session             = $Session
                AllowClobber        = $true
                DisableNameChecking = $true
                ErrorAction         = 'Stop'
            }
            If ($PSBoundParameters.Prefix) {$PSSessionParameters.Add("Prefix",$Prefix)}
            $ModuleInfo = Import-PSSession @PSSessionParameters

            # Import-Module
            $ModuleParameters = @{
                ModuleInfo          = $ModuleInfo
                DisableNameChecking = $true
                Global              = $true
                ErrorAction         = 'Stop'
            }
            If ($PSBoundParameters.Prefix) {$ModuleParameters.Add("Prefix",$Prefix)}
            Import-Module @ModuleParameters

            # Set-ADServerSettings to view the entire forest
            If ($PSBoundParameters.ViewEntireForest) {
                $ADServerSettings = Get-Command "Set-$($Prefix)ADServerSettings"
                If ($ADServerSettings) {
                    & $ADServerSettings -ViewEntireForest $True
                }
            }
        }
        Catch {
            Write-Error $PSItem
        }
    }
    End {}
}