Public/Connect-MDSOffice365.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
function Connect-MDSOffice365 {
    <#
    .NOTES
        ===========================================================================
        Created on: 2/4/2019 10:42 PM
        Created by: Bradley Wyatt
        E-Mail: Brad@TheLazyAdministrator.com
        GitHub: https://github.com/bwya77
        Website: https://www.thelazyadministrator.com
        Organization: Porcaro Stolarek Mete Partners; The Lazy Administrator
        Filename: Connect-Office365.ps1
        Version: 2.0.0

        Contributors: /u/Sheppard_Ra

        Changelog:
            2.0.0
                - Script repurposed for use in MDSTools module
            1.0.5
                - Updated comment based help
            1.0.4
                - Host title will add a service or services you are connected to. If unable to connect it will not display connection status until connection is valid
        ===========================================================================

    .SYNOPSIS
        Connect to Office 365 Services

    .DESCRIPTION
        Connect to different Office 365 Services using PowerShell function. Supports MFA.

    .PARAMETER MFA
        Description: Specifies MFA requirement to sign into Office 365 services. If set to $True it will use the Microsoft Exchange Online PowerShell Module to sign into Exchange & Compliance Center using MFA. Other modules support MFA without needing another external module.

        Warning - the Microsoft Exchange Online PowerShell Module has flaws. It'll close all open sessions.

    .PARAMETER Service
        Description: Specify service to connect to (Exchange, AzureAD, MSOnline, Teams, SecurityandCompliance, SharePoint, SkypeForBusiness)

    .EXAMPLE
        Description: Connect to SharePoint Online
        C:\PS> Connect-Office365 -SharePoint

    .EXAMPLE
        Description: Connect to Exchange Online and Azure AD V1 (MSOnline)
        C:\PS> Connect-Office365 -Service Exchange, MSOnline

    .EXAMPLE
        Description: Connect to Exchange Online and Azure AD V2 using Multi-Factor Authentication
        C:\PS> Connect-Office365 -Service Exchange, MSOnline -MFA

    .EXAMPLE
        Description: Connect to Teams and Skype for Business
        C:\PS> Connect-Office365 -Service Teams, SkypeForBusiness

    .EXAMPLE
        Description: Connect to SharePoint Online
         C:\PS> Connect-Office365 -Service SharePoint -SharePointOrganizationName bwya77 -MFA

    .LINK
        Online version: https://www.thelazyadministrator.com/2019/02/05/powershell-function-to-connect-to-all-office-365-services

    #>


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

    [OutputType()]
    [CmdletBinding(DefaultParameterSetName = 'Credential')]
    Param (
        [Parameter(Mandatory = $True, Position = 1)]
        [ValidateSet('AzureAD', 'Exchange', 'MSOnline', 'SecurityAndCompliance', 'SharePoint', 'SkypeForBusiness', 'Teams')]
        [string[]]$Service,

        [Parameter(Mandatory = $False, Position = 2)]
        [Alias('SPOrgName')]
        [string]$SharePointOrganizationName,

        [Parameter(Mandatory = $False, Position = 3, ParameterSetName = 'MDSCredential')]
        [String]$MDSCredential,

        [Parameter(Mandatory = $False, Position = 3, ParameterSetName = 'Credential')]
        [ValidateNotNullOrEmpty()]
        [PSCredential]$Credential,

        [Parameter(Mandatory = $False, Position = 4)]
        [Switch]$MFA,

        [Parameter(Mandatory = $False, Position = 5)]
        [switch]$Prefix
    )

    begin {
        If ($PSBoundParameters.ContainsKey('MDSCredential')) {
            Try {
                $Credential = Get-MDSCredential -Name $MDSCredential -ErrorAction Stop
            }
            Catch {
                Throw $PSItem
            }
        }
    }
    process {
        ForEach ($Item in $PSBoundParameters.Service) {
            Write-Verbose "Attempting connection to $Item"
            Switch ($Item) {
                AzureAD {
                    $ModuleName = 'AzureAD'
                    If (-not (Test-MDSModuleExist -Name $ModuleName -Item $Item)) {
                        continue
                    }
                    Else {
                        Try {
                            If ($True -eq $MFA) {
                                Connect-AzureAD -ErrorAction Stop
                            }
                            Else {
                                Connect-AzureAD -Credential $Credential -ErrorAction Stop
                            }
                        }
                        Catch {
                            Write-Error $PSItem
                            continue
                        }
                    }
                    continue
                }

                Exchange {
                    If ($True -eq $MFA) {
                        Try {
                            $connectEXOPSSessionSplat = @{
                                ErrorAction = 'Stop'
                                Verbose     = $False
                            }
                            If ($null -ne $Credential) {
                                $connectEXOPSSessionSplat.Add('UserPrincipalName', $Credential.UserName)
                            }
                            Connect-MDSEXOPSSession @connectEXOPSSessionSplat
                        }
                        Catch {
                            Write-Error $PSItem
                            continue
                        }
                    }
                    Else {
                        Try {
                            $importMDSExchOnlineSplat = @{
                                Credential  = $Credential
                                ErrorAction = 'Stop'
                            }
                            If ($PSBoundParameters.ContainsKey($Prefix)) {
                                $importMDSExchOnlineSplat.Add('Prefix', $True)
                            }
                            Import-MDSExchOnline @importMDSExchOnlineSplat
                        }
                        Catch {
                            Write-Error $PSItem
                            continue
                        }
                    }
                    continue
                }

                MSOnline {
                    $ModuleName = 'MSOnline'
                    If (-not (Test-MDSModuleExist -Name $ModuleName -Item $Item)) {
                        continue
                    }
                    Else {
                        Try {
                            Write-Verbose "Connecting to MSOnline"
                            If ($True -eq $MFA) {
                                Connect-MsolService -ErrorAction Stop
                            }
                            Else {
                                Connect-MsolService -Credential $Credential -ErrorAction Stop
                            }
                        }
                        Catch {
                            Write-Error $PSItem
                            continue
                        }
                    }
                    continue
                }

                SecurityAndCompliance {
                    If ($MFA -eq $True) {
                        Try {
                            $connectIPPSSessionSplat = @{
                                ErrorAction = 'Stop'
                                Verbose     = $False
                            }

                            If ($null -ne $Credential) {
                                $connectIPPSSessionSplat.Add('UserPrincipalName', $Credential.UserName)
                            }
                            Connect-MDSIPPSSession @connectIPPSSessionSplat
                        }
                        Catch {
                            Write-Error $PSItem
                            continue
                        }
                    }
                    Else {
                        Try {
                            $importMDSSecurityAndCompliance = @{
                                Credential  = $Credential
                                ErrorAction = 'Stop'
                            }
                            If ($PSBoundParameters.ContainsKey($Prefix)) {
                                $importMDSSecurityAndCompliance.Add('Prefix', $True)
                            }
                            Import-MDSSecurityAndCompliance @importMDSSecurityAndCompliance
                        }
                        Catch {
                            Write-Error $PSItem
                            continue
                        }
                    }
                    continue
                }

                SharePoint {
                    $ModuleName = 'Microsoft.Online.SharePoint.PowerShell'
                    If (-not (Test-MDSModuleExist -Name $ModuleName -Item $Item)) {
                        continue
                    }
                    Else {
                        If (-not ($PSBoundParameters.ContainsKey('SharePointOrganizationName'))) {
                            Write-Error 'Please provide a valid SharePoint organization name with the -SharePointOrganizationName parameter.'
                            continue
                        }

                        Try {
                            $SharePointURL = "https://{0}-admin.sharepoint.com" -f $SharePointOrganizationName
                            $connectSPOServiceSplat = @{
                                Url         = $SharePointURL
                                ErrorAction = 'Stop'
                            }
                            Write-Verbose "Connecting to SharePoint at $SharePointURL"
                            If ($True -eq $MFA) {
                                Connect-SPOService @connectSPOServiceSplat
                            }
                            Else {
                                Connect-SPOService @connectSPOServiceSplat -Credential $Credential
                            }
                        }
                        Catch {
                            Write-Error $PSItem
                            continue
                        }
                    }
                    continue
                }

                SkypeForBusiness {
                    Write-Verbose "Connecting to SkypeForBusiness"
                    $ModuleName = 'SkypeOnlineConnector'
                    If (-not (Test-MDSModuleExist -Name $ModuleName -Item $Item)) {
                        continue
                    }
                    Else {
                        Try {
                            If ($True -eq $MFA) {
                                $CSSession = New-CsOnlineSession -ErrorAction Stop
                            }
                            Else {
                                $CSSession = New-CsOnlineSession -Credential $Credential -ErrorAction Stop
                            }
                            Import-PSSession $CSSession -AllowClobber -ErrorAction Stop
                        }
                        Catch {
                            Write-Error $PSItem
                            continue
                        }
                    }
                    continue
                }

                Teams {
                    $ModuleName = 'MicrosoftTeams'
                    If (-not (Test-MDSModuleExist -Name $ModuleName -Item $Item)) {
                        continue
                    }
                    Else {
                        Try {
                            Write-Verbose "Connecting to Teams"
                            If ($MFA -eq $True) {
                                Connect-MicrosoftTeams -ErrorAction Stop
                            }
                            Else {
                                Connect-MicrosoftTeams -Credential $Credential -ErrorAction Stop
                            }
                        }
                        Catch {
                            Write-Error $PSItem
                            continue
                        }
                    }
                    continue
                }

                Default {
                    Write-Verbose "Default triggered for item $Item"
                }
            }
        }
    }
    end {}
}