forked from directorcia/Office365
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho365-connect-pnp.ps1
376 lines (353 loc) · 21.1 KB
/
o365-connect-pnp.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
param(
[switch]$prompt = $false, ## if -noprompt used then user will not be asked for any input
[switch]$noupdate = $false, ## if -noupdate used then module will not be checked for more recent version
[switch]$debug = $false ## if -debug create a log file
)
<# CIAOPS
Script provided as is. Use at own risk. No guarantees or warranty provided.
Description - Log into the SharePoint Online with PnP
Source - https://github.com/directorcia/Office365/blob/master/o365-connect-pnp.ps1
Prerequisites = 3
1. Ensure pnp.powershell module is installed and updated
2. Ensure Microsoft.Graph module is installed and updated
3. Newerversions of the pnp.powershell module require PowerShell V7 or above
More scripts available by joining http://www.ciaopspatron.com
#>
## Variables
$systemmessagecolor = "cyan"
$processmessagecolor = "green"
$errormessagecolor = "red"
$warningmessagecolor = "yellow"
## If you have running scripts that don't have a certificate, run this command once to disable that level of security
## set-executionpolicy -executionpolicy bypass -scope currentuser -force
Clear-Host
if ($debug) {
write-host "Script activity logged at ..\o365-connect-pnp.txt"
start-transcript "..\o365-connect-pnp.txt" | Out-Null ## Log file created in parent directory that is overwritten on each run
}
write-host -foregroundcolor $systemmessagecolor "SharePoint Online PNP Connection script started`n"
write-host -ForegroundColor $processmessagecolor "Prompt =", ($prompt)
write-host -ForegroundColor $processmessagecolor "Debug =", ($debug)
write-host -ForegroundColor $processmessagecolor "Update =", (-not $noupdate)
$ps = $PSVersionTable.PSVersion
if ($ps.Major -lt 7) {
write-host -foregroundcolor $errormessagecolor "`nThis script requires PowerShell version 7 or above`n"
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 1
}
Write-host -foregroundcolor $processmessagecolor "`nDetected supported PowerShell version: $($ps.Major).$($ps.Minor)"
# Microsoft Online Module
if (get-module -listavailable -name Microsoft.Graph.Identity.DirectoryManagement) {
## Has the Microsoft Graph module been installed?
write-host -ForegroundColor $processmessagecolor "Microsoft Graph Identity Directory Management module installed"
}
else {
write-host -ForegroundColor $warningmessagecolor -backgroundcolor $errormessagecolor "[001] - Microsoft Graph Identity Directory Management module not installed`n"
if (prompt) {
do {
$response = read-host -Prompt "`nDo you wish to install the Microsoft Graph Identity Directory Management module (Y/N)?"
} until (-not [string]::isnullorempty($response))
if ($result -eq 'Y' -or $result -eq 'y') {
write-host -foregroundcolor $processmessagecolor "Installing Microsoft Graph Identity Directory Management module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "install-Module -Name Microsoft.Graph.Identity.DirectoryManagement -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "Microsoft Graph Identity Directory Management module installed"
}
else {
write-host -foregroundcolor $processmessagecolor "Terminating script"
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 1 ## Terminate script
}
}
else {
write-host -foregroundcolor $processmessagecolor "Installing Microsoft Graph Identity Directory Management module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "install-Module -Name Microsoft.Graph.Identity.DirectoryManagement -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "Microsoft Graph Identity Directory Management module installed"
}
}
if (-not $noupdate) {
write-host -foregroundcolor $processmessagecolor "Check whether newer version of Microsoft Graph Directory Management module is available"
#get version of the module (selects the first if there are more versions installed)
$version = (Get-InstalledModule -name Microsoft.Graph.Identity.DirectoryManagement) | Sort-Object Version -Descending | Select-Object Version -First 1
#get version of the module in psgallery
$psgalleryversion = Find-Module -Name Microsoft.Graph.Identity.DirectoryManagement | Sort-Object Version -Descending | Select-Object Version -First 1
#convert to string for comparison
$stringver = $version | Select-Object @{n = 'ModuleVersion'; e = { $_.Version -as [string] } }
$a = $stringver | Select-Object Moduleversion -ExpandProperty Moduleversion
#convert to string for comparison
$onlinever = $psgalleryversion | Select-Object @{n = 'OnlineVersion'; e = { $_.Version -as [string] } }
$b = $onlinever | Select-Object OnlineVersion -ExpandProperty OnlineVersion
#version compare
if ([version]"$a" -ge [version]"$b") {
Write-Host -foregroundcolor $processmessagecolor "Local module $a greater or equal to Gallery module $b"
write-host -foregroundcolor $processmessagecolor "No update required"
}
else {
Write-Host -foregroundcolor $warningmessagecolor "Local module $a lower version than Gallery module $b"
write-host -foregroundcolor $warningmessagecolor "Update recommended"
if ($prompt) {
do {
$response = read-host -Prompt "`nDo you wish to update the Microsoft Graph Identity Directory Management PowerShell module (Y/N)?"
} until (-not [string]::isnullorempty($response))
if ($result -eq 'Y' -or $result -eq 'y') {
write-host -foregroundcolor $processmessagecolor "Updating Microsoft Graph Identity Directory Management PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "update-Module -Name Microsoft.Graph.Identity.DirectoryManagement -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "Microsoft Graph Identity Directory Management PowerShell module - updated"
}
else {
write-host -foregroundcolor $processmessagecolor "Microsoft Graph Identity Directory Management PowerShell module - not updated"
}
}
else {
write-host -foregroundcolor $processmessagecolor "Microsoft Graph Identity Directory Management PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "update-Module -Name Microsoft.Graph.Identity.DirectoryManagement -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "Microsoft Graph Identity Directory Management PowerShell module - updated"
}
}
}
write-host -foregroundcolor $processmessagecolor "Microsoft Graph Identity Directory Management PowerShell module loading"
Try {
Import-Module Microsoft.Graph.Identity.DirectoryManagement | Out-Null
}
catch {
Write-Host -ForegroundColor $errormessagecolor "[002] - Unable to load Microsoft Graph Identity Directory Management PowerShell module`n"
Write-Host -ForegroundColor $errormessagecolor $_.Exception.Message
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 2
}
write-host -foregroundcolor $processmessagecolor "Microsoft Graph Identity Directory Management PowerShell module loaded"
# SharePoint Online module
if (get-module -listavailable -name Microsoft.Graph.Sites) {
## Has the SharePOint Online PowerShell module been installed?
write-host -ForegroundColor $processmessagecolor "SharePoint Online Graph PowerShell module installed"
}
else {
write-host -ForegroundColor $warningmessagecolor -backgroundcolor $errormessagecolor "[004] - SharePoint Online Graph PowerShell module not installed`n"
if ($prompt) {
do {
$response = read-host -Prompt "`nDo you wish to install the SharePoint Online Graph PowerShell module (Y/N)?"
} until (-not [string]::isnullorempty($response))
if ($result -eq 'Y' -or $result -eq 'y') {
write-host -foregroundcolor $processmessagecolor "Installing SharePoint Online Graph PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "install-Module -Name Microsoft.Graph.Sites -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online Online Graph PowerShell module installed"
}
else {
write-host -foregroundcolor $processmessagecolor "Terminating script"
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 1 ## Terminate script
}
}
else {
write-host -foregroundcolor $processmessagecolor "Installing SharePoint Online Graph module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "install-Module -Name Microsoft.Graph.Sites -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online Graph module installed"
}
}
if (-not $noupdate) {
write-host -foregroundcolor $processmessagecolor "Check whether newer version of SharePoint Online Graph PowerShell module is available"
#get version of the module (selects the first if there are more versions installed)
$version = (Get-InstalledModule -name Microsoft.Graph.Sites) | Sort-Object Version -Descending | Select-Object Version -First 1
#get version of the module in psgallery
$psgalleryversion = Find-Module -Name Microsoft.Graph.Sites | Sort-Object Version -Descending | Select-Object Version -First 1
#convert to string for comparison
$stringver = $version | Select-Object @{n = 'ModuleVersion'; e = { $_.Version -as [string] } }
$a = $stringver | Select-Object Moduleversion -ExpandProperty Moduleversion
#convert to string for comparison
$onlinever = $psgalleryversion | Select-Object @{n = 'OnlineVersion'; e = { $_.Version -as [string] } }
$b = $onlinever | Select-Object OnlineVersion -ExpandProperty OnlineVersion
#version compare
if ([version]"$a" -ge [version]"$b") {
Write-Host -foregroundcolor $processmessagecolor "Local module $a greater or equal to Gallery module $b"
write-host -foregroundcolor $processmessagecolor "No update required"
}
else {
Write-Host -foregroundcolor $warningmessagecolor "Local module $a lower version than Gallery module $b"
write-host -foregroundcolor $warningmessagecolor "Update recommended"
if ($prompt) {
do {
$response = read-host -Prompt "`nDo you wish to update the SharePoint Online Graph PowerShell module (Y/N)?"
} until (-not [string]::isnullorempty($response))
if ($result -eq 'Y' -or $result -eq 'y') {
write-host -foregroundcolor $processmessagecolor "Updating SharePoint Online Graph PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "update-Module -Name Microsoft.Graph.Sites -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online PowerShell Graph module - updated"
}
else {
write-host -foregroundcolor $processmessagecolor "SharePoint Online PowerShell Graph module - not updated"
}
}
else {
write-host -foregroundcolor $processmessagecolor "Updating SharePoint Online Graph PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "update-Module -Name Microsoft.Graph.Sites -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online Graph PowerShell module - updated"
}
}
}
write-host -foregroundcolor $processmessagecolor "Microsoft Graph SharePoint Online PowerShell module loading"
Try {
Import-Module Microsoft.Graph.Sites | Out-Null
}
catch {
Write-Host -ForegroundColor $errormessagecolor "[002] - Unable to load Microsoft Graph SharePoint Online PowerShell module`n"
Write-Host -ForegroundColor $errormessagecolor $_.Exception.Message
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 2
}
write-host -foregroundcolor $processmessagecolor "Microsoft Graph SharePoint Online PowerShell module loaded"
## Connect to Office 365 admin service
write-host -foregroundcolor $processmessagecolor "Connecting to Microsoft Graph"
try {
Connect-MgGraph -nowelcome -Scopes "Sites.Read.All", "sites.ReadWrite.All,Domain.Read.All"
}
catch {
Write-Host -ForegroundColor $errormessagecolor "[003] - Unable to connect to Microsoft Graph`n"
Write-Host -ForegroundColor $errormessagecolor $_.Exception.Message
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 3
}
write-host -foregroundcolor $processmessagecolor "Connected to Microsoft Graph"
## Auto detect SharePoint Online admin domain
write-host -foregroundcolor $processmessagecolor "Determining SharePoint URL"
$domains = get-mgdomain ## get a list of all domains in tenant
foreach ($domain in $domains) {
## loop through all these domains
if ($domain.id.contains('onmicrosoft')) {
## find the onmicrosoft.com domain
$onname = $domain.id.split(".") ## split the onmicrosoft.com domain when found at the period. Will produce an array that contains each string as an element
$tenantname = $onname[0] ## the first string in this array is the name of the tenant
} ## end of find the on.microsoft.com domain
} ## end of the domain checking look
$tenanturl = "https://" + $tenantname + "-admin.sharepoint.com"
Write-host -ForegroundColor $processmessagecolor "SharePoint admin URL =", $tenanturl
# SharePoint PNP Online module
if (get-module -listavailable -name pnp.powershell) {
## Has the SharePoint Online PNP PowerShell module been installed?
write-host -ForegroundColor $processmessagecolor "SharePoint Online PNP PowerShell module installed"
}
else {
write-host -ForegroundColor $warningmessagecolor -backgroundcolor $errormessagecolor "[004] - SharePoint Online PNP PowerShell module not installed`n"
if ($prompt) {
do {
$response = read-host -Prompt "`nDo you wish to install the SharePoint Online PNP PowerShell module (Y/N)?"
} until (-not [string]::isnullorempty($response))
if ($result -eq 'Y' -or $result -eq 'y') {
write-host -foregroundcolor $processmessagecolor "Installing SharePoint Online PNP PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "install-Module -Name pnp.powershell -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online Online PNP PowerShell module installed"
}
else {
write-host -foregroundcolor $processmessagecolor "Terminating script"
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 1 ## Terminate script
}
}
else {
write-host -foregroundcolor $processmessagecolor "Installing SharePoint Online PNP module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "install-Module -Name pnp.powershell -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online PNP module installed"
}
}
if (-not $noupdate) {
write-host -foregroundcolor $processmessagecolor "Check whether newer version of SharePoint Online PNP PowerShell module is available"
#get version of the module (selects the first if there are more versions installed)
$version = (Get-InstalledModule -name pnp.powershell) | Sort-Object Version -Descending | Select-Object Version -First 1
#get version of the module in psgallery
$psgalleryversion = Find-Module -Name pnp.powershell | Sort-Object Version -Descending | Select-Object Version -First 1
#convert to string for comparison
$stringver = $version | Select-Object @{n = 'ModuleVersion'; e = { $_.Version -as [string] } }
$a = $stringver | Select-Object Moduleversion -ExpandProperty Moduleversion
#convert to string for comparison
$onlinever = $psgalleryversion | Select-Object @{n = 'OnlineVersion'; e = { $_.Version -as [string] } }
$b = $onlinever | Select-Object OnlineVersion -ExpandProperty OnlineVersion
#version compare
if ([version]"$a" -ge [version]"$b") {
Write-Host -foregroundcolor $processmessagecolor "Local module $a greater or equal to Gallery module $b"
write-host -foregroundcolor $processmessagecolor "No update required"
}
else {
Write-Host -foregroundcolor $warningmessagecolor "Local module $a lower version than Gallery module $b"
write-host -foregroundcolor $warningmessagecolor "Update recommended"
if ($prompt) {
do {
$response = read-host -Prompt "`nDo you wish to update the SharePoint Online PNP PowerShell module (Y/N)?"
} until (-not [string]::isnullorempty($response))
if ($result -eq 'Y' -or $result -eq 'y') {
write-host -foregroundcolor $processmessagecolor "Updating SharePoint Online PNP PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "update-Module -Name pnp.powershell -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online PNP PowerShell module - updated"
}
else {
write-host -foregroundcolor $processmessagecolor "SharePoint Online PNP PowerShell module - not updated"
}
}
else {
write-host -foregroundcolor $processmessagecolor "Updating SharePoint Online PNP PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "update-Module -Name pnp.powershell -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online PNP PowerShell module - updated"
}
}
}
write-host -foregroundcolor $processmessagecolor "Get all SharePoint Online sites"
$sites = (Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/beta/sites?search=$($tenantname)" -Method GET).value
$root = (Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/beta/sites/root" -Method GET)
##$sites = get-mgsite -search $tenantname
$siteSummary = @()
$siteSummary += [pscustomobject]@{
Name = $root.displayname
weburl = $root.weburl
}
Foreach ($site in $sites) {
$siteSummary += [pscustomobject]@{
Name = $site.name
weburl = $site.weburl
}
}
write-host -foregroundcolor $processmessagecolor "Sites found =",$sitesummary.count
$result = $sitesummary | select-object Name, weburl | Sort-Object Name,weburl | Out-GridView -OutputMode Single -title "Select SharePoint site to connect to with PNP"
write-host -foregroundcolor $processmessagecolor "Selected SharePoint Online site =", $result.weburl
# Import SharePoint Online PNP module
write-host -foregroundcolor $processmessagecolor "SharePoint Online PNP PowerShell module loading"
Try {
Import-Module pnp.powershell | Out-Null
}
catch {
Write-Host -ForegroundColor $errormessagecolor "[005] - Unable to load SharePoint Online PNP PowerShell module. Try using PowerShell V7 or above`n"
Write-Host -ForegroundColor $errormessagecolor $_.Exception.Message
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 5
}
write-host -foregroundcolor $processmessagecolor "SharePoint Online PNP PowerShell module loaded"
# Connect to SharePoint Online PNP Service
write-host -foregroundcolor $processmessagecolor "Connecting to SharePoint PNP Online"
Try {
connect-pnponline -url $result.weburl -launchbrowser -devicelogin | Out-Null
}
catch {
Write-Host -ForegroundColor $errormessagecolor "[006] - Unable to connect to SharePoint Online PNP`n"
Write-Host -ForegroundColor $errormessagecolor $_.Exception.Message
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 6
}
write-host -foregroundcolor $processmessagecolor "Connected to SharePoint Online PNP`n"
write-host -foregroundcolor $systemmessagecolor "SharePoint Online PNP Connection script finished`n"
if ($debug) {
Stop-Transcript | Out-Null
}