forked from directorcia/Office365
-
Notifications
You must be signed in to change notification settings - Fork 1
/
o365-connect-spo.ps1
254 lines (236 loc) · 13.8 KB
/
o365-connect-spo.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
param(
[switch]$noprompt = $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 Office 365 admin portal and the SharePoint Online portal
Source - https://github.com/directorcia/Office365/blob/master/o365-connect-spo.ps1
Prerequisites = 2
1. Ensure msonline module installed or updated
2. Ensure SharePoint online PowerShell module installed or updated
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-spo.txt"
start-transcript "..\o365-connect-spo.txt" | Out-Null ## Log file created in parent directory that is overwritten on each run
}
write-host -foregroundcolor $systemmessagecolor "SharePoint Online Connection script started`n"
write-host -ForegroundColor $processmessagecolor "Prompt =",(-not $noprompt)
# Microsoft Online Module
if (get-module -listavailable -name MSOnline) { ## Has the Microsoft Online PowerShell module been installed?
write-host -ForegroundColor $processmessagecolor "Microsoft Online PowerShell module installed"
}
else {
write-host -ForegroundColor $warningmessagecolor -backgroundcolor $errormessagecolor "[001] - Microsoft Online PowerShell module not installed`n"
if (-not $noprompt) {
do {
$response = read-host -Prompt "`nDo you wish to install the Microsoft Online PowerShell module (Y/N)?"
} until (-not [string]::isnullorempty($response))
if ($result -eq 'Y' -or $result -eq 'y') {
write-host -foregroundcolor $processmessagecolor "Installing Microsoft Online PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "install-Module -Name msonline -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "Microsoft Online 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 Microsoft Online module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "install-Module -Name msonline -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "Microsoft Online module installed"
}
}
if (-not $noupdate) {
write-host -foregroundcolor $processmessagecolor "Check whether newer version of Microsoft Online PowerShell module is available"
#get version of the module (selects the first if there are more versions installed)
$version = (Get-InstalledModule -name msonline) | Sort-Object Version -Descending | Select-Object Version -First 1
#get version of the module in psgallery
$psgalleryversion = Find-Module -Name msonline | 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 (-not $noprompt) {
do {
$response = read-host -Prompt "`nDo you wish to update the Microsoft Online PowerShell module (Y/N)?"
} until (-not [string]::isnullorempty($response))
if ($result -eq 'Y' -or $result -eq 'y') {
write-host -foregroundcolor $processmessagecolor "Updating Microsoft Online PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "update-Module -Name msonline -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "Microsoft Online PowerShell module - updated"
}
else {
write-host -foregroundcolor $processmessagecolor "Microsoft Online PowerShell module - not updated"
}
}
else {
write-host -foregroundcolor $processmessagecolor "Microsoft Online PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "update-Module -Name msonline -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "Microsoft Online PowerShell module - updated"
}
}
}
write-host -foregroundcolor $processmessagecolor "Microsoft Online PowerShell module loading"
Try {
Import-Module msonline | Out-Null
}
catch {
Write-Host -ForegroundColor $errormessagecolor "[002] - Unable to load Microsoft 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 Online PowerShell module loaded"
## Connect to Office 365 admin service
write-host -foregroundcolor $processmessagecolor "Connecting to Microsoft 365 Admin service"
try {
connect-msolservice
}
catch {
Write-Host -ForegroundColor $errormessagecolor "[003] - Unable to connect to Microsoft Online`n"
Write-Host -ForegroundColor $errormessagecolor $_.Exception.Message
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 3
}
write-host -foregroundcolor $processmessagecolor "Connected to Microsoft 365 Admin service"
## Auto detect SharePoint Online admin domain
write-host -foregroundcolor $processmessagecolor "Determining SharePoint Administration URL"
$domains = get-msoldomain ## get a list of all domains in tenant
foreach ($domain in $domains) { ## loop through all these domains
if ($domain.name.contains('onmicrosoft')) { ## find the onmicrosoft.com domain
$onname = $domain.name.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 Online module
if (get-module -listavailable -name microsoft.online.sharepoint.powershell) { ## Has the SharePOint Online PowerShell module been installed?
write-host -ForegroundColor $processmessagecolor "SharePoint Online PowerShell module installed"
}
else {
write-host -ForegroundColor $warningmessagecolor -backgroundcolor $errormessagecolor "[004] - SharePoint Online PowerShell module not installed`n"
if (-not $noprompt) {
do {
$response = read-host -Prompt "`nDo you wish to install the SharePoint Online PowerShell module (Y/N)?"
} until (-not [string]::isnullorempty($response))
if ($result -eq 'Y' -or $result -eq 'y') {
write-host -foregroundcolor $processmessagecolor "Installing SharePoint Online PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "install-Module -Name microsoft.online.sharepoint.powershell -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online Online 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 module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "install-Module -Name microsoft.online.sharepoint.powershell -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online module installed"
}
}
if (-not $noupdate) {
write-host -foregroundcolor $processmessagecolor "Check whether newer version of SharePoint Online PowerShell module is available"
#get version of the module (selects the first if there are more versions installed)
$version = (Get-InstalledModule -name microsoft.online.sharepoint.powershell) | Sort-Object Version -Descending | Select-Object Version -First 1
#get version of the module in psgallery
$psgalleryversion = Find-Module -Name microsoft.online.sharepoint.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 (-not $noprompt) {
do {
$response = read-host -Prompt "`nDo you wish to update the SharePoint Online PowerShell module (Y/N)?"
} until (-not [string]::isnullorempty($response))
if ($result -eq 'Y' -or $result -eq 'y') {
write-host -foregroundcolor $processmessagecolor "Updating SharePoint Online PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "update-Module -Name microsoft.online.sharepoint.powershell -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online PowerShell module - updated"
}
else {
write-host -foregroundcolor $processmessagecolor "SharePoint Online PowerShell module - not updated"
}
}
else {
write-host -foregroundcolor $processmessagecolor "Updating SharePoint Online PowerShell module - Administration escalation required"
Start-Process powershell -Verb runAs -ArgumentList "update-Module -Name microsoft.online.sharepoint.powershell -Force -confirm:$false" -wait -WindowStyle Hidden
write-host -foregroundcolor $processmessagecolor "SharePoint Online PowerShell module - updated"
}
}
}
# Import SharePoint Online module
write-host -foregroundcolor $processmessagecolor "SharePoint Online PowerShell module loading"
Try {
Import-Module microsoft.online.sharepoint.powershell -disablenamechecking | Out-Null
}
catch {
Write-Host -ForegroundColor $errormessagecolor "[005] - Unable to load SharePoint Online PowerShell module`n"
Write-Host -ForegroundColor $errormessagecolor $_.Exception.Message
if ($debug) {
Stop-Transcript | Out-Null ## Terminate transcription
}
exit 5
}
write-host -foregroundcolor $processmessagecolor "SharePoint Online PowerShell module loaded"
# Connect to SharePoint Online Service
write-host -foregroundcolor $processmessagecolor "Connecting to SharePoint Online"
Try {
connect-sposervice -url $tenanturl | Out-Null
}
catch {
Write-Host -ForegroundColor $errormessagecolor "[006] - Unable to connect to SharePoint Online`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`n"
write-host -foregroundcolor $systemmessagecolor "SharePoint Online Connection script finished`n"
if ($debug) {
Stop-Transcript | Out-Null
}