-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectO365Services.ps1
352 lines (337 loc) · 10.5 KB
/
ConnectO365Services.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
<#
=============================================================================================
Name: Connect to all the Office 365 services using PowerShell
Description: This script automatically installs all the required modules(upon your confirmation) and connects to the services
Version: 3.5
Website: o365reports.com
Script by: O365Reports Team
For detailed script execution: https://o365reports.com/2019/10/05/connect-all-office-365-services-powershell/
============================================================================================
#>
Param
(
[Parameter(Mandatory = $false)]
[switch]$Disconnect,
[ValidateSet('AzureAD','MSOnline','ExchangeOnline','SharePoint','SharePointPnP','SecAndCompCenter','Teams')]
[string[]]$Services=("AzureAD","MSOnline","ExchangeOnline",'SharePoint','SharePointPnP','SecAndCompCenter','Teams'),
[string]$SharePointHostName,
[Switch]$MFA,
[string]$UserName,
[string]$Password
)
#Disconnecting Sessions
if($Disconnect.IsPresent)
{
#Disconnect Exchange Online,Skype and Security & Compliance center session
Get-PSSession | Remove-PSSession
#Disconnect Teams connection
Disconnect-MicrosoftTeams
#Disconnect SharePoint connection
Disconnect-SPOService
Write-Host All sessions in the current window has been removed. -ForegroundColor Yellow
}
else
{
if(($UserName -ne "") -and ($Password -ne ""))
{
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword
}
#Getting credential for non-MFA account
elseif(!($MFA.IsPresent))
{
$Credential=Get-Credential -Credential $null
}
$ConnectedServices=""
if($Services.Length -eq 7)
{
$RequiredServices=$Services
}
else
{
$RequiredServices=$PSBoundParameters.Services
}
#Loop through each required services
Foreach($Service in $RequiredServices)
{
Write-Host Checking connection to $Service...
Switch ($Service)
{
#Module and Connection settings for Exchange Online module
ExchangeOnline
{
$Module=Get-InstalledModule -Name ExchangeOnlineManagement -MinimumVersion 2.0.3
if($Module.count -eq 0)
{
Write-Host Required Exchange Online'(EXO V2)' module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Install-Module ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
}
else
{
Write-Host EXO V2 module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet.
}
Continue
}
if($mfa.IsPresent)
{
Connect-ExchangeOnline
}
else
{
Connect-ExchangeOnline -Credential $Credential
}
If((Get-EXOMailbox -ResultSize 1) -ne $null)
{
if($ConnectedServices -ne "")
{
$ConnectedServices=$ConnectedServices+","
}
$ConnectedServices=$ConnectedServices+" Exchange Online"
}
}
#Module and Connection settings for AzureAD V1 (MSOnline module)
MSOnline
{
$Module=Get-Module -Name MSOnline -ListAvailable
if($Module.count -eq 0)
{
Write-Host MSOnline module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Install-Module MSOnline
Import-Module MSOnline
}
else
{
Write-Host MSOnline module is required to connect AzureAD.Please install module using Install-Module MSOnline cmdlet.
}
Continue
}
if($mfa.IsPresent)
{
Connect-MsolService
}
else
{
Connect-MsolService -Credential $Credential
}
If((Get-MsolUser -MaxResults 1) -ne $null)
{
if($ConnectedServices -ne "")
{
$ConnectedServices=$ConnectedServices+","
}
$ConnectedServices=$ConnectedServices+" MSOnline"
}
if(($RequiredServices -contains "SharePoint") -eq "true")
{
$SharePointHostName=((Get-MsolDomain) | where {$_.IsInitial -eq "True"} ).name -split ".onmicrosoft.com"
$SharePointHostName=($SharePointHostName).trim()
}
}
#Module and Connection settings for AzureAD V2 (AzureAD module)
AzureAD
{
$Module=Get-Module -Name AzureAD -ListAvailable
if($Module.count -eq 0)
{
Write-Host AzureAD module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Install-Module AzureAD
Import-Module AzureAD
}
else
{
Write-Host AzureAD module is required to connect AzureAD.Please install module using Install-Module AzureAD cmdlet.
}
Continue
}
if($mfa.IsPresent)
{
Connect-AzureAD
}
else
{
Connect-AzureAD -Credential $Credential
}
If((Get-AzureADUser -Top 1) -ne $null)
{
if($ConnectedServices -ne "")
{
$ConnectedServices=$ConnectedServices+","
}
$ConnectedServices=$ConnectedServices+" AzureAD"
}
}
#Module and Connection settings for SharePoint Online module
SharePoint
{
$Module=Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable
if($Module.count -eq 0)
{
Write-Host SharePoint Online PowerShell module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Install-Module Microsoft.Online.SharePoint.PowerShell
}
else
{
Write-Host SharePoint Online PowerShell module is required.Please install module using Install-Module Microsoft.Online.SharePoint.PowerShell cmdlet.
Continue
}
}
if(!($PSBoundParameters['SharePointHostName']) -and ([string]$SharePointHostName -eq "") )
{
Write-Host SharePoint organization name is required.`nEg: Contoso for [email protected] -ForegroundColor Yellow
$SharePointHostName= Read-Host "Please enter SharePoint organization name"
}
if($MFA.IsPresent)
{
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://$SharePointHostName-admin.sharepoint.com
}
else
{
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://$SharePointHostName-admin.sharepoint.com -credential $credential
}
if((Get-SPOTenant) -ne $null)
{
if($ConnectedServices -ne "")
{
$ConnectedServices=$ConnectedServices+","
}
$ConnectedServices=$ConnectedServices+"SharePoint Online"
}
}
#Module and Connection settings for Sharepoint PnP module
SharePointPnP
{
$Module=Get-InstalledModule -Name SharePointPnPPowerShellOnline
if($Module.count -eq 0)
{
Write-Host SharePoint PnP module module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Install-Module -Name SharePointPnPPowerShellOnline -AllowClobber
}
else
{
Write-Host SharePoint Pnp module is required.Please install module using Install-Module SharePointPnPPowerShellOnline cmdlet.
}
Continue
}
if(!($PSBoundParameters['SharePointHostName']) -and ([string]$SharePointHostName -eq "") )
{
Write-Host SharePoint organization name is required.`nEg: Contoso for [email protected] -ForegroundColor Yellow
$SharePointHostName= Read-Host "Please enter SharePoint organization name"
}
if($MFA.IsPresent)
{
Import-Module SharepointpnpPowerShellOnline -DisableNameChecking
Connect-PnPOnline -Url https://$SharePointHostName.sharepoint.com -UseWebLogin -WarningAction Ignore
}
else
{
Import-Module SharepointpnpPowerShellOnline -DisableNameChecking
Connect-PnPOnline -Url https://$SharePointHostName.sharepoint.com -credential $credential -WarningAction Ignore
}
If ($? -eq $true)
{
if($ConnectedServices -ne "")
{
$ConnectedServices=$ConnectedServices+","
}
$ConnectedServices=$ConnectedServices+"SharePoint PnP"
}
}
#Module and Connection settings for Security & Compliance center
SecAndCompCenter
{
$Module=Get-InstalledModule -Name ExchangeOnlineManagement -MinimumVersion 2.0.3
if($Module.count -eq 0)
{
Write-Host Exchange Online'(EXO V2)' module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Install-Module ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
}
else
{
Write-Host EXO V2 module is required to connect Security and Compliance PowerShell.Please install module using Install-Module ExchangeOnlineManagement cmdlet.
}
Continue
}
if($mfa.IsPresent)
{
Connect-IPPSSession -WarningAction SilentlyContinue
}
else
{
Connect-IPPSSession -Credential $Credential -WarningAction SilentlyContinue
}
$Result=Get-RetentionCompliancePolicy
If(($?) -eq $true)
{
if($ConnectedServices -ne "")
{
$ConnectedServices=$ConnectedServices+","
}
$ConnectedServices=$ConnectedServices+" Security & Compliance Center"
}
}
#Module and Connection settings for Teams Online module
Teams
{
$Module=Get-InstalledModule -Name MicrosoftTeams -MinimumVersion 4.0.0
if($Module.count -eq 0)
{
Write-Host Required MicrosoftTeams module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Install-Module MicrosoftTeams -AllowClobber -Force
}
else
{
Write-Host MicrosoftTeams module is required.Please install module using Install-Module MicrosoftTeams cmdlet.
}
Continue
}
if($mfa.IsPresent)
{
$Team=Connect-MicrosoftTeams
}
else
{
$Team=Connect-MicrosoftTeams -Credential $Credential
}
#Check for Teams connectivity
If($Team -ne $null)
{
if($ConnectedServices -ne "")
{
$ConnectedServices=$ConnectedServices+","
}
$ConnectedServices=$ConnectedServices+"Teams"
}
}
}
}
if($ConnectedServices -eq "")
{
$ConnectedServices="-"
}
Write-Host `n`nConnected Services $ConnectedServices -ForegroundColor DarkYellow
}