-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaintainModule.psm1
More file actions
338 lines (286 loc) · 15.7 KB
/
Copy pathMaintainModule.psm1
File metadata and controls
338 lines (286 loc) · 15.7 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
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
<#
MIT License
Copyright (C) 2025 Robin Widmark.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#>
Function Uninstall-rsModule {
<#
.SYNOPSIS
Uninstall older versions of your modules in a easy way.
.DESCRIPTION
This script let users uninstall older versions of the modules that are installed on the system.
.PARAMETER Module
Specify modules that you want to uninstall older versions from, if this is left empty all of the older versions of the systems modules will be uninstalled
.EXAMPLE
Uninstall-rsModule -Module "VMWare.PowerCLI"
# This will uninstall all older versions of the module VMWare.PowerCLI system.
.EXAMPLE
Uninstall-rsModule -Module "VMWare.PowerCLI", "ImportExcel"
# This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system.
.EXAMPLE
Uninstall-rsModule
# This will uninstall all older versions of all modules in the system
.LINK
https://github.com/rwidmark/MaintainModule/blob/main/README.md
.NOTES
Author: Robin Widmark
Mail: robin@widmark.dev
Website/Blog: https://widmark.dev
X: https://x.com/widmark_robin
Mastodon: https://mastodon.social/@rwidmark
YouTube: https://www.youtube.com/@rwidmark
Linkedin: https://www.linkedin.com/in/rwidmark/
GitHub: https://github.com/rwidmark
#>
[CmdletBinding(SupportsShouldProcess)]
Param(
[Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules you want to uninstall older version of, if not used all older versions will be uninstalled")]
[string]$Module,
[Parameter(Mandatory = $false, HelpMessage = ".")]
[string[]]$OldVersion,
[Parameter(Mandatory = $false, HelpMessage = "If this is used updates etc. be for prerelease")]
[bool]$AllowPrerelease = $false
)
Write-Output "START - Uninstall older versions of $($Module)"
Write-Output "Please wait, this can take some time..."
foreach ($_version in $OldVersion) {
Write-Verbose "Uninstalling version $($_version) of $($Module)..."
try {
Uninstall-Module -Name $Module -RequiredVersion $_version -AllowPrerelease:$AllowPrerelease -Force -ErrorAction SilentlyContinue
}
catch {
Write-Error "$($PSItem.Exception)"
continue
}
}
Write-Output "FINISHED - All older versions of $($Module) are now uninstalled!"
}
Function Get-rsInstalledModule {
[CmdletBinding(SupportsShouldProcess)]
Param(
[Parameter(Mandatory = $false, HelpMessage = "Enter module or modules that you want to update, if you don't enter any, all of the modules will be updated")]
[string[]]$Module
)
$ReturnCode = 0
$ReturnData = [ordered]@{}
# Collect all installed modules from the system
Write-Verbose "Caching all installed modules from the system..."
$GetInstalledModules = Get-InstalledModule | Select-Object Name | Sort-Object -Descending
if ([string]::IsNullOrEmpty($Module)) {
Write-Verbose "Parameter Module are empty, populate it with all installed modules from the system..."
$ReturnModule = foreach ($_module in $GetInstalledModules) {
Write-Verbose "Collecting information about module $($_module.name)..."
$GetAllInstalledVersions = Get-InstalledModule -Name $_module.name -AllVersions | Sort-Object { $_.Version -as [version] } -Descending
# Get latest version
[version]$LatestVersion = $($GetAllInstalledVersions | Select-Object Version -First 1).version
# Get get all old versions
[version]$OldVersions = $GetAllInstalledVersions | Where-Object { $_.Version -ne $LatestVersion } | Select-Object -ExpandProperty Version
[PSCustomObject]@{
Name = $_module.Name
Repository = $GetAllInstalledVersions.Repository
OldVersion = $OldVersions
LatestVersion = $LatestVersion
}
}
}
else {
Write-Verbose "Looking so the modules exists in the system..."
$ReturnModule = foreach ($_module in $Module) {
if ($_module -in $GetInstalledModules.name) {
Write-Verbose "$($_module) is installed, collecting information about it..."
$GetAllInstalledVersions = Get-InstalledModule -Name $_module -AllVersions | Sort-Object { $_.Version -as [version] } -Descending
# Get latest version
[version]$LatestVersion = $($GetAllInstalledVersions | Select-Object Version -First 1).version
# Get get all old versions
[version]$OldVersions = $GetAllInstalledVersions | Where-Object { $_.Version -ne $LatestVersion } | Select-Object -ExpandProperty Version
[PSCustomObject]@{
Name = $_module
Repository = $GetAllInstalledVersions.Repository
OldVersion = $OldVersions
LatestVersion = $LatestVersion
}
}
else {
Write-Warning "$($_module) is not installed, skipping this module..."
}
}
}
if ($null -eq $ReturnModule) {
$ReturnCode = 1
Write-Warning "No modules was found..."
$ReturnModule = $null
}
$ReturnData.Add("ReturnCode", $ReturnCode)
$ReturnData.Add("Module", $ReturnModule)
return $ReturnData
}
Function Test-rsComponent {
[CmdletBinding(SupportsShouldProcess)]
Param(
)
# Making sure that TLS 1.2 is used.
Write-Verbose "Making sure that TLS 1.2 is used..."
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
# Checking if PSGallery are set to trusted
Write-Verbose "Checking if PowerShell Gallery are set to trusted..."
if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") {
try {
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!"
}
catch {
Write-Error "$($PSItem.Exception)"
continue
}
}
else {
Write-Verbose "PowerShell Gallery was already set to trusted, continuing!"
}
}
Function Update-rsModule {
<#
.SYNOPSIS
This module let you maintain your installed modules in a easy way.
.DESCRIPTION
This function let you update all of your installed modules and also uninstall the old versions to keep things clean.
You can also specify module or modules that you want to update. It's also possible to install the module if it's missing and import the modules in the end of the script.
.PARAMETER Module
Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated
.PARAMETER Scope
Need to specify scope of the installation/update for the module, either AllUsers or CurrentUser. Default is CurrentUser.
If this parameter is empty it will use CurrentUser
The parameter -Scope don't effect the uninstall-module function this is because of limitation from Microsoft.
- Scope effect Install/update module function.
.PARAMETER UninstallOldVersion
If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed
.PARAMETER InstallMissing
If you use this switch and the modules that are specified in the Module parameter are not installed on the system they will be installed.
.PARAMETER AllowPrerelease
If you set this to $true Pre-Releases are going to be installed / updated
.PARAMETER SkipPublisherCheck
If you set this to $true PublisherCheck will be ignored, this is something that for example are needed for Pester and PowerCLI because there certificate are not valid for some reason.
.EXAMPLE
Update-rsModule -Module "PowerCLI", "ImportExcel" -Scope "CurrentUser"
# This will update the modules PowerCLI, ImportExcel for the current user
.EXAMPLE
Update-rsModule -Module "PowerCLI", "ImportExcel" -UninstallOldVersion
# This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel.
.EXAMPLE
Update-rsModule -Module "PowerCLI", "ImportExcel" -InstallMissing
# This will install the modules PowerCLI and/or ImportExcel on the system if they are missing, if the modules are installed already they will only get updated.
.EXAMPLE
Update-rsModule -Module "PowerCLI", "ImportExcel" -UninstallOldVersion -ImportModule
# This will update the modules PowerCLI and ImportExcel and delete all of the old versions that are installed of PowerCLI and ImportExcel and then import the modules.
.LINK
https://github.com/rwidmark/MaintainModule/blob/main/README.md
.NOTES
Author: Robin Widmark
Mail: robin@widmark.dev
Website/Blog: https://widmark.dev
X: https://x.com/widmark_robin
Mastodon: https://mastodon.social/@rwidmark
YouTube: https://www.youtube.com/@rwidmark
Linkedin: https://www.linkedin.com/in/rwidmark/
GitHub: https://github.com/rwidmark
#>
[CmdletBinding(SupportsShouldProcess)]
Param(
[Parameter(Mandatory = $false, HelpMessage = "Enter module or modules that you want to update, if you don't enter any, all of the modules will be updated")]
[string[]]$Module,
[Parameter(Mandatory = $false, HelpMessage = "Enter CurrentUser or AllUsers depending on what scope you want to change your modules, default is CurrentUser")]
[ValidateSet("CurrentUser", "AllUsers")]
[string]$Scope = "CurrentUser",
[Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")]
[switch]$UninstallOldVersion = $false,
[Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")]
[switch]$InstallMissing = $false,
[Parameter(Mandatory = $false, HelpMessage = "Don't check publishers certificate")]
[switch]$SkipPublisherCheck = $false,
[Parameter(Mandatory = $false, HelpMessage = "If this is used updates etc. be for prerelease")]
[bool]$AllowPrerelease = $false
)
Write-Output "`n=== Module Maintenance - Widmark.dev 2025 ==="
Write-Output "Please wait, this can take some time...`n"
# Making sure that all needed components are installed
Test-rsComponent
Write-Output "START - Updating modules`n"
# Collect all installed modules from the system
$GetModuleInfo = Get-rsInstalledModule -Module $Module
# Start looping trough every module that are stored in the string Module
if ($GetModuleInfo.ReturnCode -eq 0) {
foreach ($_module in $GetModuleInfo.Module) {
# Getting the latest installed version of the module
Write-Verbose "Collecting all installed version of $($_module.Name)..."
# Collects the latest version of module from the source where the module was installed from
Write-Verbose "Looking up the latest version of $($_module)..."
[version]$CollectLatestVersion = $(Find-Module -Name $_module.Name -Repository $_module.Repository -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object Version -First 1).version
# Looking if the version of the module are the latest version, it it's not the latest it will install the latest version.
if ($_module.LatestVersion -lt $CollectLatestVersion) {
try {
Write-Output "Found a newer version of $($_module.Name), version $CollectLatestVersion"
Write-Output "Updating $($_module.Name) from $($_module.LatestVersion) to version $CollectLatestVersion..."
if ($SkipPublisherCheck -eq $true) {
Update-Module -Name $_module.Name -Scope $Scope -AllowPrerelease:$AllowPrerelease -SkipPublisherCheck -AcceptLicense -Force
}
else {
Update-Module -Name $_module.Name -Scope $Scope -AllowPrerelease:$AllowPrerelease -AcceptLicense -Force
}
Write-Output "$($_module.Name) has now been updated to version $($CollectLatestVersion)!"
# If switch -UninstallOldVersion has been used then the old versions will be uninstalled from the module
if ($UninstallOldVersion -eq $true -and $_module.OldVersion.Count -gt 0) {
Uninstall-rsModule -Module $_module.Name -OldVersion $_module.OldVersion -AllowPrerelease:$AllowPrerelease
Uninstall-rsModule -Module $_module.Name -OldVersion $_module.LatestVersion -AllowPrerelease:$AllowPrerelease
}
else {
Write-Verbose "$($_module.Name) don't have any older versions to uninstall!"
}
}
catch {
Write-Error "$($PSItem.Exception)"
continue
}
}
else {
Write-Verbose "$($_module.Name) are already up to date!"
}
}
}
#Install module if they want that
else {
# If the switch InstallMissing are set to true the modules will get installed if they are missing
if ($InstallMissing -eq $true) {
try {
Write-Output "$($_module.name) are not installed, installing $($_module.name)..."
if ($SkipPublisherCheck -eq $true) {
Install-Module -Name $_module.name -Scope $Scope -AllowPrerelease:$AllowPrerelease -SkipPublisherCheck -AcceptLicense -Force
}
else {
Install-Module -Name $_module.name -Scope $Scope -AllowPrerelease:$AllowPrerelease -AcceptLicense -Force
}
Write-Output "$($_module.name) has now been installed!"
}
catch {
Write-Error "$($PSItem.Exception)"
continue
}
}
else {
Write-Verbose "$($_module.name) are not installed, you have not chosen to install missing modules"
}
}
Write-Output "`n=== \\\ Script Finished! /// ===`n"
}