-
Notifications
You must be signed in to change notification settings - Fork 28
/
Update-AllPowerShellModules.ps1
211 lines (181 loc) · 9.58 KB
/
Update-AllPowerShellModules.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
# https://itpro-tips.com/2020/update-all-powershell-modules-at-once/
# https://itpro-tips.com/2020/mettre-a-jour-tous-les-modules-powershell-en-une-fois/
[CmdletBinding()]
param (
[Parameter()]
# To exclude modules from the update process
[String[]]$ExcludedModules,
# To include only these modules for the update process
[String[]]$IncludedModules,
[switch]$SkipPublisherCheck,
[switch]$SimulationMode
)
<#
/!\/!\/!\ PLEASE READ /!\/!\/!\
/!\ If you look for a quick way to update, please keep in mind Microsoft has a built-in CMDlet to update ALL the PowerShell modules installed:
/!\ Update-Module [-Verbose]
/!\ This script is intended as a replacement of the Update-Module:
/!\ - to provide more human readable output than the -Verbose option of Update-Module
/!\ - to force install with -SkipPublisherCheck (Authenticode change) because Update-Module has not this option
/!\ - to exclude some modules from the update process
/!\ - to remove older versions because Update-Module does not remove older versions (it only installs a new version in the $env:PSModulePath\<moduleName> and keep the old module)
This script provides informations about the module version (current and the latest available on PowerShell Gallery) and update to the latest version
If you have a module with two or more versions, the script delete them and reinstall only the latest.
#>
#Requires -Version 5.0
#Requires -RunAsAdministrator
Write-Host -ForegroundColor cyan 'Define PowerShell to add TLS1.2 in this session, needed since 1st April 2020 (https://devblogs.microsoft.com/powershell/powershell-gallery-tls-support/)'
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
# if needed, register PSGallery
# Register PSGallery PSprovider and set as Trusted source
# Register-PSRepository -Default -ErrorAction SilentlyContinue
# Set-PSRepository -Name PSGallery -InstallationPolicy trusted -ErrorAction SilentlyContinue
if ($SimulationMode) {
Write-Host -ForegroundColor yellow 'Simulation mode is ON, nothing will be installed / removed / updated'
}
function Remove-OldPowerShellModules {
param (
[string]$ModuleName,
[string]$GalleryVersion
)
try {
$oldVersions = Get-InstalledModule -Name $ModuleName -AllVersions -ErrorAction Stop | Where-Object { $_.Version -ne $GalleryVersion }
foreach ($oldVersion in $oldVersions) {
Write-Host -ForegroundColor Cyan "$ModuleName - Uninstall previous version" -NoNewline
Write-Host -ForegroundColor White " ($($oldVersion.Version))"
if (-not($SimulationMode)) {
Remove-Module $ModuleName -ErrorAction SilentlyContinue
Uninstall-Module $oldVersion -Force -ErrorAction Stop
}
}
}
catch {
Write-Warning "$module - $($_.Exception.Message)"
}
}
if ($IncludedModules) {
Write-Host -ForegroundColor Cyan "Get PowerShell modules like $IncludedModules"
$modules = Get-InstalledModule | Where-Object { $_.Name -like $IncludedModules }
}
else {
Write-Host -ForegroundColor Cyan 'Get all PowerShell modules'
$modules = Get-InstalledModule
}
foreach ($module in $modules.Name) {
if ($ExcludedModules -contains $module) {
Write-Host -ForegroundColor Yellow "Module $module is excluded from the update process"
continue
}
elseif ($module -like "$excludedModules") {
Write-Host -ForegroundColor Yellow "Module $module is excluded from the update process (match $excludeModules)"
continue
}
$currentVersion = $null
try {
$currentVersion = (Get-InstalledModule -Name $module -AllVersions -ErrorAction Stop).Version
}
catch {
Write-Warning "$module - $($_.Exception.Message)"
continue
}
try {
$moduleGalleryInfo = Find-Module -Name $module -ErrorAction Stop
}
catch {
Write-Warning "$module not found in the PowerShell Gallery. $($_.Exception.Message)"
continue
}
# $current version can also be a version follow by -preview
if ($currentVersion -like '*-preview') {
Write-Warning "The module installed is a preview version, it will not tested by this script"
}
if ($moduleGalleryInfo.Version -like '*-preview') {
Write-Warning "The module in PowerShell Gallery is a preview version, it will not tested bt this script"
continue
}
else {
$moduleGalleryVersion = $moduleGalleryInfo.Version
}
# Convert published date to YYYY/MM/DD HH:MM:SS format
$publishedDate = [datetime]$moduleGalleryInfo.PublishedDate
$publishedDate = $publishedDate.ToString("yyyy/MM/dd HH:mm:ss")
if ($null -eq $currentVersion) {
Write-Host -ForegroundColor Cyan "$module - Install from PowerShellGallery version $($moduleGalleryInfo.Version) - Release date: $publishedDate"
if (-not($SimulationMode)) {
try {
Install-Module -Name $module -Force -SkipPublisherCheck -ErrorAction Stop
}
catch {
Write-Warning "$module - $($_.Exception.Message)"
}
}
}
elseif ($moduleGalleryInfo.Version -eq $currentVersion) {
Write-Host -ForegroundColor Green "$module - already in latest version: " -NoNewline
Write-Host -ForegroundColor White "$currentVersion" -NoNewline
Write-Host -ForegroundColor Green " - Release date:" -NoNewline
Write-Host -ForegroundColor White " $publishedDate"
}
elseif ($currentVersion.count -gt 1) {
Write-Host -ForegroundColor Yellow "$module is installed in $($currentVersion.count) versions:" -NoNewline
Write-Host -ForegroundColor White " $($currentVersion -join ' | ')"
Write-Host -ForegroundColor Cyan "$module - Uninstall previous $module version(s) below the latest version" -NoNewline
Write-Host -ForegroundColor White " ($($moduleGalleryInfo.Version))"
Remove-OldPowerShellModules -ModuleName $module -GalleryVersion $moduleGalleryInfo.Version
# Check again the current Version as we uninstalled some old versions
$currentVersion = (Get-InstalledModule -Name $module).Version
if ($moduleGalleryVersion -ne $currentVersion) {
Write-Host -ForegroundColor Cyan "$module - Install from PowerShellGallery version" -NoNewline
Write-Host -ForegroundColor White " $($moduleGalleryInfo.Version)" -NoNewline
Write-Host -ForegroundColor Cyan " - Release date:" -NoNewline
Write-Host -ForegroundColor White " $publishedDate"
if (-not($SimulationMode)) {
try {
Install-Module -Name $module -Force -ErrorAction Stop
Remove-OldPowerShellModules -ModuleName $module -GalleryVersion $moduleGalleryInfo.Version
}
catch {
Write-Warning "$module - $($_.Exception.Message)"
}
}
}
}
# https://invoke-thebrain.com/2018/12/comparing-version-numbers-powershell/
elseif ([version]$currentVersion -gt [version]$moduleGalleryVersion) {
Write-Host -ForegroundColor Yellow "$module - the current version $currentVersion is newer than the version available on PowerShell Gallery $($moduleGalleryInfo.Version) (Release date: $publishedDate). Sometimes happens when you install a module from another repository or via .exe/.msi or if you change the version number manually."
}
elseif ([version]$currentVersion -lt [version]$moduleGalleryVersion) {
Write-Host -ForegroundColor Cyan "$module - Update from PowerShellGallery version" -NoNewline
Write-Host -ForegroundColor White " $currentVersion -> $($moduleGalleryInfo.Version)" -NoNewline
Write-Host -ForegroundColor Cyan " - Release date:" -NoNewline
Write-Host -ForegroundColor White " $publishedDate"
if (-not($SimulationMode)) {
try {
Update-Module -Name $module -Force -ErrorAction Stop
Remove-OldPowerShellModules -ModuleName $module -GalleryVersion $moduleGalleryInfo.Version
}
catch {
if ($_.Exception.Message -match 'Authenticode') {
Write-Host -ForegroundColor Yellow "$module - The module certificate used by the creator is either changed since the last module install or the module sign status has changed."
if ($SkipPublisherCheck.IsPresent) {
Write-Host -ForegroundColor Cyan "$module - SkipPublisherCheck Parameter is present, so install will run without Authenticode check"
Write-Host -ForegroundColor Cyan "$module - Install from PowerShellGallery version $($moduleGalleryInfo.Version) - Release date: $publishedDate"
try {
Install-Module -Name $module -Force -SkipPublisherCheck
}
catch {
Write-Warning "$module - $($_.Exception.Message)"
}
Remove-OldPowerShellModules -ModuleName $module -GalleryVersion $moduleGalleryInfo.Version
}
else {
Write-Warning "$module - If you want to update this module, run again with -SkipPublisherCheck switch, but please keep in mind the security risk"
}
}
else {
Write-Warning "$module - $($_.Exception.Message)"
}
}
}
}
}