Skip to content

Commit aae0473

Browse files
author
Robin Stolpe
authored
Merge pull request #12 from rstolpe/beta
Beta
2 parents 74d592b + 8035fe5 commit aae0473

13 files changed

+7875
-239
lines changed

.src/MaintainModule.psd1.source

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<#
2-
Copyright (C) {{year}} Robin Stolpe.
2+
Copyright (C) {{year}} Robin Stolpe.
33
<https://stolpe.io>
44
This program is free software: you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -125,7 +125,7 @@
125125
ReleaseNotes = 'https://github.com/rstolpe/MaintainModule/releases'
126126

127127
# Prerelease string of this module
128-
Prerelease = '{{preReleaseTag}}'
128+
# Prerelease = '{{preReleaseTag}}'
129129

130130
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
131131
RequireLicenseAcceptance = $false

.src/Public/Function/Uninstall-RSModule.ps1

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel"
1818
# This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system.
1919
20-
.RELATED LINKS
20+
.LINK
2121
https://github.com/rstolpe/MaintainModule/blob/main/README.md
2222
2323
.NOTES
@@ -38,7 +38,6 @@
3838
Write-Output "`n=== Starting to uninstall older versions of modules ===`n"
3939
Write-Output "Please wait, this can take some time..."
4040

41-
# Collect all installed modules from the system
4241
Write-Verbose "Caching all installed modules from the system..."
4342
$InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name
4443

@@ -49,33 +48,45 @@
4948
}
5049
else {
5150
Write-Verbose "User has added modules to the Module parameter, splitting them"
52-
$Module = $Module.Split(",").Trim()
51+
$OldModule = $Module.Split(",").Trim()
52+
53+
[System.Collections.ArrayList]$Module = @()
54+
Write-Verbose "Looking so the modules exists in the system..."
55+
foreach ($m in $OldModule) {
56+
if ($m -in $InstalledModules.name) {
57+
Write-Verbose "$($m) did exists in the system..."
58+
[void]($Module.Add($m))
59+
}
60+
else {
61+
Write-Warning "$($m) did not exists in the system, skipping this module..."
62+
}
63+
}
5364
}
5465

5566
foreach ($m in $Module.Split()) {
5667
Write-Verbose "Collecting all installed version of the module $($m)"
57-
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object PublishedDate -Descending
68+
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -ExpandProperty Version
5869

5970
# If the module has more then one version loop trough the versions and only keep the most current one
6071
if ($GetAllInstalledVersions.Count -gt 1) {
61-
$MostRecentVersion = $GetAllInstalledVersions[0].Version
62-
Foreach ($Version in $GetAllInstalledVersions.Version) {
63-
if ($Version -ne $MostRecentVersion) {
64-
try {
65-
Write-Output "Uninstalling previous version $($Version) of module $($m)..."
66-
Uninstall-Module -Name $m -RequiredVersion $Version -Force -ErrorAction SilentlyContinue
67-
Write-Output "Version $($Version) of $($m) are now uninstalled!"
68-
}
69-
catch {
70-
Write-Error "$($PSItem.Exception)"
71-
continue
72-
}
72+
$MostRecentVersion = $null
73+
[version]$MostRecentVersion = $GetAllInstalledVersions[0]
74+
Foreach ($Version in $GetAllInstalledVersions | Where-Object { [version]$_ -lt [version]$MostRecentVersion }) {
75+
try {
76+
Write-Output "Uninstalling previous version $($Version) of module $($m)..."
77+
Uninstall-Module -Name $m -RequiredVersion $Version -Force -ErrorAction SilentlyContinue
78+
Write-Output "Version $($Version) of $($m) are now uninstalled!"
79+
}
80+
catch {
81+
Write-Error "$($PSItem.Exception)"
82+
continue
7383
}
7484
}
85+
# bygga in en check så att den verkligen kan verifiera detta
7586
Write-Output "All older versions of $($m) are now uninstalled, the only installed version of $($m) is $($MostRecentVersion)"
7687
}
7788
else {
78-
Write-Verbose "$($m) don't have any older versions installed then the most current one, no need to uninstall anything."
89+
Write-Verbose "$($m) don't have any older versions installed then $($GetAllInstalledVersions), no need to uninstall anything."
7990
}
8091
}
8192
Write-Output "`n---/// Script Finished! ///---"

.src/Public/Function/Update-RSModule.ps1

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function Update-RSModule {
2-
<#
2+
<#
33
.SYNOPSIS
44
This module let you maintain your installed modules in a easy way.
55
@@ -13,7 +13,7 @@
1313
.PARAMETER Scope
1414
Need to specify scope of the installation/update for the module, either AllUsers or CurrentUser. Default is CurrentUser.
1515
If this parameter is empty it will use CurrentUser
16-
The parameter -Scope don't effect the uninstall-module function this is because of limitation from Microsoft.
16+
The parameter -Scope don't effect the uninstall-module function this is because of limitation from Microsoft.
1717
- Scope effect Install/update module function.
1818
1919
.PARAMETER ImportModule
@@ -42,7 +42,7 @@
4242
Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion -ImportModule
4343
# 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.
4444
45-
.RELATED LINKS
45+
.LINK
4646
https://github.com/rstolpe/MaintainModule/blob/main/README.md
4747
4848
.NOTES
@@ -58,8 +58,8 @@
5858
Param(
5959
[Parameter(Mandatory = $false, HelpMessage = "Enter module or modules (separated with ,) that you want to update, if you don't enter any all of the modules will be updated")]
6060
[string]$Module,
61-
[ValidateSet("CurrentUser", "AllUsers")]
62-
[Parameter(Mandatory = $true, HelpMessage = "Enter CurrentUser or AllUsers depending on what scope you want to change your modules")]
61+
[ValidateSet("CurrentUser", "AllUsers")]
62+
[Parameter(Mandatory = $false, HelpMessage = "Enter CurrentUser or AllUsers depending on what scope you want to change your modules")]
6363
[string]$Scope = "CurrentUser",
6464
[Parameter(Mandatory = $false, HelpMessage = "Import modules that has been entered in the module parameter at the end of this function")]
6565
[switch]$ImportModule = $false,
@@ -85,7 +85,21 @@
8585
}
8686
else {
8787
Write-Verbose "User has added modules to the Module parameter, splitting them"
88-
$Module = $Module.Split(",").Trim()
88+
$OldModule = $Module.Split(",").Trim()
89+
90+
[System.Collections.ArrayList]$Module = @()
91+
if ($InstallMissing -eq $false) {
92+
Write-Verbose "Looking so the modules exists in the system..."
93+
foreach ($m in $OldModule) {
94+
if ($m -in $InstalledModules.name) {
95+
Write-Verbose "$($m) did exists in the system..."
96+
[void]($Module.Add($m))
97+
}
98+
else {
99+
Write-Warning "$($m) did not exists in the system, skipping this module..."
100+
}
101+
}
102+
}
89103
}
90104

91105
# Making sure that TLS 1.2 is used.
@@ -111,33 +125,35 @@
111125

112126
# Start looping trough every module that are stored in the string Module
113127
foreach ($m in $Module.Split()) {
114-
115128
Write-Verbose "Checks if $($m) are installed"
116129
if ($m -in $InstalledModules.Name) {
117130

118-
# Get all of the installed versions of the module
131+
# Getting the latest installed version of the module
119132
Write-Verbose "Collecting all installed version of $($m)..."
120-
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object PublishedDate -Descending
133+
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object Version
134+
[version]$LatestInstalledVersion = $($GetAllInstalledVersions | Select-Object Version -First 1).version
121135

122-
# Collects the latest version of module
123-
Write-Verbose "Looking up the latest version of $($m)..."
124-
$CollectLatestVersion = Find-Module -Name $m | Sort-Object Version -Descending | Select-Object Version -First 1
136+
# Collects the latest version of module from the source where the module was installed from
137+
Write-Output "Looking up the latest version of $($m)..."
138+
[version]$CollectLatestVersion = $(Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object Version -First 1).version
125139

126-
# Looking if the version of the module are the latest version
127-
if ($GetAllInstalledVersions.Version -lt $CollectLatestVersion.Version) {
140+
# Looking if the version of the module are the latest version, it it's not the latest it will install the latest version.
141+
if ($LatestInstalledVersion -lt $CollectLatestVersion) {
128142
try {
129-
Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)"
130-
Write-Output "Updating $($m) to version $($CollectLatestVersion.Version)..."
143+
Write-Output "Found a newer version of $($m), version $($CollectLatestVersion)"
144+
Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..."
131145
Update-Module -Name $($m) -Scope $Scope -Force
132-
Write-Output "$($m) has been updated to version $($CollectLatestVersion.Version)!"
146+
Write-Output "$($m) has now been updated to version $($CollectLatestVersion)!`n"
133147
}
134148
catch {
135149
Write-Error "$($PSItem.Exception)"
136150
continue
137151
}
152+
}
138153

139-
# If switch -UninstallOldVersion has been used then the old versions will be uninstalled from the module
140-
if ($UninstallOldVersion -eq $true) {
154+
# If switch -UninstallOldVersion has been used then the old versions will be uninstalled from the module
155+
if ($UninstallOldVersion -eq $true) {
156+
if ($GetAllInstalledVersions.Count -gt 1) {
141157
Uninstall-RSModule -Module $m
142158
}
143159
}
@@ -168,7 +184,7 @@
168184
# Collect all of the imported modules.
169185
Write-Verbose "Collecting all of the installed modules..."
170186
$ImportedModules = Get-Module | Select-Object Name, Version
171-
187+
172188
# Import module if it's not imported
173189
Write-Verbose "Starting to import the modules..."
174190
foreach ($m in $Module.Split()) {

MaintainModule/MaintainModule.psd1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<#
2-
Copyright (C) 2022 Robin Stolpe.
1+
<#
2+
Copyright (C) 2022 Robin Stolpe.
33
<https://stolpe.io>
44
This program is free software: you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -18,7 +18,7 @@
1818
#
1919
# Generated by: Robin Stolpe
2020
#
21-
# Generated on: 2022-11-27
21+
# Generated on: 2022-11-30
2222
#
2323

2424
@{
@@ -125,7 +125,7 @@
125125
ReleaseNotes = 'https://github.com/rstolpe/MaintainModule/releases'
126126

127127
# Prerelease string of this module
128-
Prerelease = 'beta'
128+
# Prerelease = ''
129129

130130
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
131131
RequireLicenseAcceptance = $false

0 commit comments

Comments
 (0)