-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00-Update-M365Modules.ps1
204 lines (189 loc) · 9.37 KB
/
00-Update-M365Modules.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
# Bitpusher
# \`._,'/
# (_- -_)
# \o/
# The Digital
# Fox
# https://theTechRelay.com
# https://github.com/bitpusher2k
#
# UpdateM365Modules.ps1
# Created by https://github.com/directorcia @directorcia
# Modified by Bitpusher/The Digital Fox
# v2.8 last updated 2024-05-12
# Script to install/update MSOL PowerShell modules.
#
# Usage:
# powershell -executionpolicy bypass -f .\UpdateM365Modules.ps1
#
# Run as admin.
#
#comp #m365 #security #bec #script #update #powershell #modules #install #graph
#Requires -Version 5.1
param(## if no parameters used then don't prompt to install missing modules, just install them
[switch]$prompt = $false ## if -prompt used then prompt to install missing modules
)
<# CIAOPS
Script provided as is. Use at own risk. No guarantees or warranty provided.
Description - Update all the relevant Microsoft Online PowerShell modules
Source - https://github.com/directorcia/Office365/blob/master/o365-update.ps1
## Prerequisites = 1
## 1. Run PowerShell environment as an administrator
More scripts available by joining http://www.ciaopspatron.com
#>
Write-Output "Powershell version is:"
$PSVersionTable
Write-Output "Installed modules:"
Get-Module -ListAvailable
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-Output "`n`nIf you get an error you may need to run: Install-Module -Name Microsoft.Identity.Client -RequiredVersion 4.44.0.0"
Write-Output "`n`nWill now attempt to install/update M365 modules..."
function test-package ($packagename) {
try {
$found = Get-PackageProvider -Name $packagename -ErrorAction SilentlyContinue
} catch {
$found = $false
}
if ($found) {
## If module exists then update
#get version of the module (selects the first if there are more versions installed)
$version = (Get-PackageProvider -Name $packagename) | Sort-Object Version -Descending | Select-Object Version -First 1
#get version of the module in psgallery
$psgalleryversion = Find-PackageProvider -Name $packagename | Sort-Object Version -Descending | Select-Object Version -First 1
#convert to string for comparison
$stringver = $version | Select-Object @{ n = 'Version'; e = { $_.Version -as [string] } }
$a = $stringver | Select-Object version -ExpandProperty version
#convert to string for comparison
$onlinever = $psgalleryversion | Select-Object @{ n = 'Version'; e = { $_.Version -as [string] } }
$b = $onlinever | Select-Object Version -ExpandProperty Version
#version compare
if ([version]"$a" -ge [version]"$b") {
Write-Output " Local package $a greater or equal to Gallery package $b"
Write-Output " No update required`n"
} else {
Write-Output " Local package $a lower version than Gallery package $b"
Write-Output " Will be updated"
update-packageprovider -Name $packagename -Force -confirm:$false
Write-Output ""
}
} else {
## If module doesn't exist then prompt to update
Write-Output -NoNewline " [Warning]" $pacakgename" package not found.`n"
if ($prompt) {
do {
$result = Read-Host -Prompt "Install this package (Y/N)?"
} until (-not [string]::IsNullOrEmpty($result))
if ($result -eq 'Y' -or $result -eq 'y') {
Write-Output "Installing package", $packagename"`n"
Install-PackageProvider -Name $packagename -Force -Confirm:$false
}
} else {
Write-Output "Installing package", $packagename"`n"
Install-PackageProvider -Name $packagename -Force -Confirm:$false
}
}
}
function test-install ($modulename) {
try {
$found = Get-InstalledModule -Name $modulename -ErrorAction SilentlyContinue
} catch {
$found = $false
}
if ($found) {
## If module exists then update
#get version of the module (selects the first if there are more versions installed)
$version = (Get-InstalledModule -Name $modulename) | Sort-Object Version -Descending | Select-Object Version -First 1
#get version of the module in psgallery
$psgalleryversion = Find-Module -Name $modulename | 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-Output " Local module $a greater or equal to Gallery module $b"
Write-Output " No update required`n"
} else {
Write-Output " Local module $a lower version than Gallery module $b"
Write-Output " Will be updated"
Update-Module -Name $modulename -Force -Confirm:$false
Write-Output ""
}
} else {
## If module doesn't exist then prompt to update
Write-Output -NoNewline " [Warning]" $modulename" module not found.`n"
if ($prompt) {
do {
$result = Read-Host -Prompt " Install this module (Y/N)?"
} until (-not [string]::IsNullOrEmpty($result))
if ($result -eq 'Y' -or $result -eq 'y') {
Write-Output " Installing module", $modulename"`n"
Install-Module -Name $modulename -Force -Confirm:$false -AllowClobber
}
} else {
Write-Output " Installing module", $modulename"`n"
Install-Module -Name $modulename -Force -Confirm:$false -AllowClobber
}
}
}
## 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
Write-Output "Prompt to install missing modules =", $prompt"`n"
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())
if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Output "(1 of 16) Update NuGet provider"
test-package -packagename NuGet
Write-Output "(2 of 16) Update Azure AD module"
test-install -ModuleName AzureAD
Write-Output "(3 of 16) Update Azure Information Protection module"
$aadrmcheck = Get-Module -ListAvailable -Name aadrm
if ($aadrmcheck) {
Write-Output " [Warning] Older module Azure AD Rights management module (AADRM) is still installed"
Write-Output " Uninstalling AADRM module as support ended July 15, 2020 "
Uninstall-Module aadrm -AllVersions -Force -Confirm:$false
Write-Output " Now Azure Information Protection module will now be installed"
}
test-install -ModuleName AIPService
Write-Output "(4 of 16) Update Teams Module"
test-install -ModuleName MicrosoftTeams
Write-Output "(5 of 16) Update SharePoint Online module"
test-install -ModuleName Microsoft.Online.SharePoint.PowerShell
Write-Output "(6 of 16) Update Microsoft Online module"
test-install -ModuleName MSOnline
Write-Output "(7 of 16) Update PowerShellGet module"
test-install -ModuleName PowershellGet
Write-Output "(8 of 16) Update Exchange Online module"
test-install -ModuleName ExchangeOnlineManagement
Write-Output "(9 of 16) Update Azure module"
test-install -ModuleName Az
Write-Output "(10 of 16) Update SharePoint PnP module"
$pnpcheck = Get-Module -ListAvailable -Name SharePointPnPPowerShellOnline
if ($pnpcheck) {
Write-Output " [Warning] Older SharePoint PnP module is still installed"
Write-Output " Uninstalling older SharePoint PnP module"
Uninstall-Module SharePointPnPPowerShellOnline -AllVersions -Force -Confirm:$false
Write-Output " New SharePoint PnP module will now be installed"
}
test-install -ModuleName PnP.PowerShell
Write-Output "(11 of 16) Update Microsoft Graph module"
test-install -ModuleName Microsoft.Graph # Update-Module Microsoft.Graph
Write-Output "(11.5 of 16) Update Microsoft Graph Beta module"
test-install -ModuleName Microsoft.Graph.Beta # Update-Module Microsoft.Graph.Beta
Write-Output "(12 of 16) Update Windows Autopilot Module"
## will also update dependent AzureAD and Microsoft.Graph.Intune modules
test-install -ModuleName WindowsAutoPilotIntune
Write-Output "(13 of 16) Centralised Add-in Deployment"
test-install -ModuleName O365CentralizedAddInDeployment
Write-Output "(14 of 16) PowerApps"
test-install -ModuleName Microsoft.PowerApps.PowerShell
Write-Output "(15 of 16) PowerApps Administration module"
test-install -ModuleName Microsoft.PowerApps.Administration.PowerShell
Write-Output "(16 of 16) Microsoft 365 Commerce module"
test-install -ModuleName MSCommerce
} else {
Write-Output "*** ERROR *** - Please re-run PowerShell environment as Administrator`n"
Write-Output "Start-Process PowerShell -Verb RunAs"
}
Write-Output "`nScript completed"