Skip to content

Commit f7d05ca

Browse files
author
Robin Stolpe
authored
Merge pull request #10 from rstolpe/dev
Dev
2 parents fc183c4 + 3c6a568 commit f7d05ca

11 files changed

Lines changed: 7183 additions & 101 deletions

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

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
@@ -190,4 +206,6 @@
190206
}
191207
}
192208
Write-Output "`n---/// Script Finished! ///---"
193-
}
209+
}
210+
211+
Update-RSModule -UninstallOldVersion -Scope CurrentUser

MaintainModule/MaintainModule.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<#
1+
<#
22
Copyright (C) 2022 Robin Stolpe.
33
<https://stolpe.io>
44
This program is free software: you can redistribute it and/or modify
@@ -18,7 +18,7 @@
1818
#
1919
# Generated by: Robin Stolpe
2020
#
21-
# Generated on: 2022-11-29
21+
# Generated on: 2022-11-30
2222
#
2323

2424
@{
@@ -27,7 +27,7 @@
2727
RootModule = '.\MaintainModule.psm1'
2828

2929
# Version number of this module.
30-
ModuleVersion = '1.0.0'
30+
ModuleVersion = '0.0.8'
3131

3232
# Supported PSEditions
3333
# CompatiblePSEditions = @()

0 commit comments

Comments
 (0)