-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathUpdate-PowerShellResource.ps1
More file actions
293 lines (259 loc) · 14.4 KB
/
Update-PowerShellResource.ps1
File metadata and controls
293 lines (259 loc) · 14.4 KB
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
[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-PSResource [-Verbose]
/!\ This script is intended as a replacement of the Update-PSResource:
/!\ - to provide more human readable output than the -Verbose option of Update-PSResource
/!\ - to force install with -SkipPublisherCheck (Authenticode change) because Update-PSResource has not this option
/!\ - to exclude some modules from the update process
/!\ - to remove older versions because Update-PSResource does not remove older versions (it only installs a new version in the $env:PSModulePath\<moduleName> and keep the old module)
/!\ - to provide a simulation mode (no install / uninstall / update, only display what would be done)
/!\ - update module AND scrip
/!\ - works ONLY with Powershell 7.0+
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
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-LegacyPSResource {
param (
[Parameter(Mandatory = $true)]
[System.Object]$Resource,
[Parameter(Mandatory = $true)]
[string]$GalleryVersion
)
try {
$oldVersions = $Resource | Where-Object { $_.Version -ne $GalleryVersion }
foreach ($oldVersion in $oldVersions) {
Write-Host -ForegroundColor Cyan "$($Resource.Name) - Uninstall previous version" -NoNewline
Write-Host -ForegroundColor White " ($($oldVersion.Version))"
# https://github.com/PowerShell/PSResourceGet/issues/1793
# Uinstall-PSResource on OneDrive Error: Access to the path is denied
if (-not($SimulationMode)) {
if ($Resource.InstalledLocation -like "$env:programfiles\*") {
Uninstall-PSResource -Name $($Resource.Name) -Version $oldVersion.Version -ErrorAction Stop
}
elseif ($Resource.InstalledLocation -like "$env:OneDrive\*") {
# module installed in OneDrive location
$installedLocation = "$($oldVersion.InstalledLocation)\$($Resource.Name)\$($oldVersion.Version)"
Write-Host -ForegroundColor Magenta '`Uninstall-PSResource` is not working properly in OneDrive, so the script will use `Remove-Item -Recurse -Force` instead. See https://github.com/PowerShell/PSResourceGet/issues/1793 for more details.'
if (Test-Path -Path $installedLocation) {
Write-Host -ForegroundColor Cyan "$($Resource.Name) - Remove the folder $installedLocation"
Remove-Item -Path $installedLocation -Recurse -Force -ErrorAction Stop -ProgressAction SilentlyContinue
}
else {
Write-Warning "$($Resource.Name) - The folder $installedLocation does not exist, so cannot be removed"
}
}
else {
# module installed in current user location
Uninstall-PSResource -Name $($Resource.Name) -Version $oldVersion.Version -Scope CurrentUser -ErrorAction Stop
}
}
}
}
catch {
Write-Warning "$($Resource.Name) - $($_.Exception.Message)"
}
}
if ($IncludedModules) {
Write-Host -ForegroundColor Cyan "Get PowerShell modules like $IncludedModules"
$modules = Get-PSResource | Where-Object { $_.Name -like $IncludedModules -and $_.Type -ne 'Script' }
}
else {
Write-Host -ForegroundColor Cyan 'Get all PowerShell modules'
$modules = Get-PSResource | Where-Object { $_.Type -ne 'Script' }
}
foreach ($module in $modules) {
$moduleName = $module.Name
if ($ExcludedModules -contains $moduleName) {
Write-Host -ForegroundColor Yellow "Module $moduleName is excluded from the update process"
continue
}
elseif ($moduleName -like "$excludedModules") {
Write-Host -ForegroundColor Yellow "Module $moduleName is excluded from the update process (match $excludeModules)"
continue
}
$currentVersion = $null
try {
$currentVersion = $module.Version
}
catch {
Write-Warning "$moduleName - $($_.Exception.Message)"
continue
}
try {
$moduleGalleryInfo = Find-PSResource -Name $moduleName -ErrorAction Stop
}
catch {
Write-Warning "$moduleName 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 "$moduleName - Install from PowerShellGallery version $($moduleGalleryInfo.Version) - Release date: $publishedDate"
if (-not($SimulationMode)) {
try {
if ($module.InstalledLocation -like "$env:programfiles\*") {
Install-PSResource -Name $moduleName -Force -ErrorAction Stop
}
else {
Write-Host -ForegroundColor Cyan "Install $moduleName in CurrentUser scope because the module is installed in $($module.InstalledLocation)"
Install-PSResource -Name $moduleName -Scope CurrentUser -Force -ErrorAction Stop
}
}
catch {
Write-Warning "$moduleName - $($_.Exception.Message)"
}
}
}
elseif ($moduleGalleryInfo.Version -eq $currentVersion) {
Write-Host -ForegroundColor Green "$moduleName - 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 "$moduleName is installed in $($currentVersion.count) versions:" -NoNewline
Write-Host -ForegroundColor White " $($currentVersion -join ' | ')"
Write-Host -ForegroundColor Cyan "$moduleName - Uninstall previous $moduleName version(s) below the latest version" -NoNewline
Write-Host -ForegroundColor White " ($($moduleGalleryInfo.Version))"
Remove-LegacyPSResource -Resource $module -GalleryVersion $moduleGalleryInfo.Version
# Check again the current Version as we uninstalled some old versions
$currentVersion = (Get-PSResource -Name $moduleName).Version
if ($moduleGalleryVersion -ne $currentVersion) {
Write-Host -ForegroundColor Cyan "$moduleName - 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 {
if ($module.InstalledLocation -like "$env:programfiles\*") {
Install-PSResource -Name $moduleName -Force -ErrorAction Stop
}
else {
Write-Host -ForegroundColor Cyan "Install $moduleName in CurrentUser scope because the module is installed in $($module.InstalledLocation)"
Install-PSResource -Name $moduleName -Scope CurrentUser -Force -ErrorAction Stop
}
Remove-LegacyPSResource -Resource $module -GalleryVersion $moduleGalleryInfo.Version
}
catch {
Write-Warning "$moduleName - $($_.Exception.Message)"
}
}
}
}
# https://invoke-thebrain.com/2018/12/comparing-version-numbers-powershell/
elseif ([version]$currentVersion -gt [version]$moduleGalleryVersion) {
Write-Host -ForegroundColor Yellow "$moduleName - 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 "$moduleName - 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-PSResource -Name $moduleName -Force -ErrorAction Stop
Remove-LegacyPSResource -Resource $module -GalleryVersion $moduleGalleryInfo.Version
}
catch {
if ($_.Exception.Message -match 'Authenticode') {
Write-Host -ForegroundColor Yellow "$moduleName - 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 "$moduleName - SkipPublisherCheck Parameter is present, so install will run without Authenticode check"
Write-Host -ForegroundColor Cyan "$moduleName - Install from PowerShellGallery version $($moduleGalleryInfo.Version) - Release date: $publishedDate"
try {
if ($module.InstalledLocation -like "$env:programfiles\*") {
Install-PSResource -Name $moduleName -Force -SkipPublisherCheck -ErrorAction Stop
}
else {
Write-Host -ForegroundColor Cyan "Install $moduleName in CurrentUser scope because the module is installed in $($module.InstalledLocation)"
Install-PSResource -Name $moduleName -Scope CurrentUser -Force -SkipPublisherCheck -ErrorAction Stop
}
}
catch {
Write-Warning "$module - $($_.Exception.Message)"
}
Remove-LegacyPSResource -Resource $module -GalleryVersion $moduleGalleryInfo.Version
}
else {
Write-Warning "$moduleName - If you want to update this module, run again with -SkipPublisherCheck switch, but please keep in mind the security risk"
}
}
else {
Write-Warning "$moduleName - $($_.Exception.Message)"
}
}
}
}
}
if ($IncludedModules) {
Write-Host -ForegroundColor Cyan "Get PowerShell script modules like $IncludedModules"
$scripts = Get-PSResource | Where-Object { $_.Name -like $IncludedModules -and $_.Type -eq 'Script' }
}
else {
Write-Host -ForegroundColor Cyan 'Get all PowerShell script modules'
$scripts = Get-PSResource | Where-Object Type -eq 'Script'
}
foreach ($script in $scripts) {
try {
$scriptCurrentVersion = Find-PSResource -Name $script.Name -ErrorAction Stop
}
catch {
Write-Warning "$($script.Name) is a script module, so it is excluded from the update process"
}
if ($scriptCurrentVersion.Version -like '*-preview') {
Write-Warning 'The script module in PowerShell Gallery is a preview version, it will not tested bt this script'
continue
}
elseif ([version]$scriptCurrentVersion.Version -ne [version]$script.Version) {
if ($script.InstalledLocation -like "$env:programfiles\*") {
Write-Host -ForegroundColor Cyan "(script) $($script.Name) - Update from PowerShellGallery version with AllUsers scope" -NoNewline
Update-PSResource -Name $script.Name -Force
}
else {
Write-Host -ForegroundColor Cyan "(script) $($script.Name) - Update from PowerShellGallery version with CurrentUser scope" -NoNewline
Update-PSResource -Name $script.Name -Scope CurrentUser -Force
}
}
else {
Write-Host -ForegroundColor Green "(script) $($script.Name) - already in latest version: " -NoNewline
Write-Host -ForegroundColor White "$($script.Version)" -NoNewline
Write-Host -ForegroundColor Green ' - Release date:' -NoNewline
$scriptPublishedDate = [datetime]$scriptCurrentVersion.PublishedDate
$scriptPublishedDate = $scriptPublishedDate.ToString('yyyy/MM/dd HH:mm:ss')
Write-Host -ForegroundColor White " $scriptPublishedDate"
}
}