Public/Import-MDSExchOnline.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
Function Import-MDSExchOnline {
    <#
    .SYNOPSIS
    Import the Exchange Online PowerShell cmdlets by passing an MDSCredential, credential, or via a credential prompt when used interactively.

    .DESCRIPTION
    Import the Exchange Online PowerShell cmdlets by passing an MDSCredential, credential, or via a credential prompt when used interactively.

    .EXAMPLE
    Import-MDSExchOnline -MDSCredential MyCred1

    Import the EXO cmdlets with the stored 'MyCred1' credentials. The stored credential username should be a UPN.

    .EXAMPLE
    Import-MDSExchOnline -MDSCredential MyCred1 -Prefix O365

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

    .EXAMPLE
    Import-MDSExchOnline -Credential $CredentialObject

    Import the EXO cmdlets with a credential object.

    .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,
            ParameterSetName = 'MDSCredential'
        )]
        [parameter(ParameterSetName = 'Credential')]
        [string]$Prefix
    )

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

            # New-PSSession
            $SessionParameters = @{
                Name              = $SessionName
                ConfigurationName = 'Microsoft.Exchange'
                ConnectionUri     = 'https://outlook.office365.com/powershell-liveid/'
                Credential        = $Credential
                Authentication    = 'Basic'
            }
            $Session = New-PSSession @SessionParameters

            # Import-PSSession
            $PSSessionParameters = @{
                Session             = $Session
                DisableNameChecking = $true
                Global              = $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
        }
        Catch {
            Write-Error $PSItem
        }
    }
    End {}
}