Skip to content

Commit

Permalink
Merge pull request #12 from rstolpe/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
rstolpe committed Nov 30, 2022
2 parents 74d592b + 8035fe5 commit aae0473
Show file tree
Hide file tree
Showing 13 changed files with 7,875 additions and 239 deletions.
4 changes: 2 additions & 2 deletions .src/MaintainModule.psd1.source
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<#
Copyright (C) {{year}} Robin Stolpe.
Copyright (C) {{year}} Robin Stolpe.
<https://stolpe.io>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -125,7 +125,7 @@
ReleaseNotes = 'https://github.com/rstolpe/MaintainModule/releases'

# Prerelease string of this module
Prerelease = '{{preReleaseTag}}'
# Prerelease = '{{preReleaseTag}}'

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
RequireLicenseAcceptance = $false
Expand Down
45 changes: 28 additions & 17 deletions .src/Public/Function/Uninstall-RSModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel"
# This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system.
.RELATED LINKS
.LINK
https://github.com/rstolpe/MaintainModule/blob/main/README.md
.NOTES
Expand All @@ -38,7 +38,6 @@
Write-Output "`n=== Starting to uninstall older versions of modules ===`n"
Write-Output "Please wait, this can take some time..."

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

Expand All @@ -49,33 +48,45 @@
}
else {
Write-Verbose "User has added modules to the Module parameter, splitting them"
$Module = $Module.Split(",").Trim()
$OldModule = $Module.Split(",").Trim()

[System.Collections.ArrayList]$Module = @()
Write-Verbose "Looking so the modules exists in the system..."
foreach ($m in $OldModule) {
if ($m -in $InstalledModules.name) {
Write-Verbose "$($m) did exists in the system..."
[void]($Module.Add($m))
}
else {
Write-Warning "$($m) did not exists in the system, skipping this module..."
}
}
}

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

# If the module has more then one version loop trough the versions and only keep the most current one
if ($GetAllInstalledVersions.Count -gt 1) {
$MostRecentVersion = $GetAllInstalledVersions[0].Version
Foreach ($Version in $GetAllInstalledVersions.Version) {
if ($Version -ne $MostRecentVersion) {
try {
Write-Output "Uninstalling previous version $($Version) of module $($m)..."
Uninstall-Module -Name $m -RequiredVersion $Version -Force -ErrorAction SilentlyContinue
Write-Output "Version $($Version) of $($m) are now uninstalled!"
}
catch {
Write-Error "$($PSItem.Exception)"
continue
}
$MostRecentVersion = $null
[version]$MostRecentVersion = $GetAllInstalledVersions[0]
Foreach ($Version in $GetAllInstalledVersions | Where-Object { [version]$_ -lt [version]$MostRecentVersion }) {
try {
Write-Output "Uninstalling previous version $($Version) of module $($m)..."
Uninstall-Module -Name $m -RequiredVersion $Version -Force -ErrorAction SilentlyContinue
Write-Output "Version $($Version) of $($m) are now uninstalled!"
}
catch {
Write-Error "$($PSItem.Exception)"
continue
}
}
# bygga in en check så att den verkligen kan verifiera detta
Write-Output "All older versions of $($m) are now uninstalled, the only installed version of $($m) is $($MostRecentVersion)"
}
else {
Write-Verbose "$($m) don't have any older versions installed then the most current one, no need to uninstall anything."
Write-Verbose "$($m) don't have any older versions installed then $($GetAllInstalledVersions), no need to uninstall anything."
}
}
Write-Output "`n---/// Script Finished! ///---"
Expand Down
56 changes: 36 additions & 20 deletions .src/Public/Function/Update-RSModule.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Function Update-RSModule {
<#
<#
.SYNOPSIS
This module let you maintain your installed modules in a easy way.
Expand All @@ -13,7 +13,7 @@
.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.
The parameter -Scope don't effect the uninstall-module function this is because of limitation from Microsoft.
- Scope effect Install/update module function.
.PARAMETER ImportModule
Expand Down Expand Up @@ -42,7 +42,7 @@
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.
.RELATED LINKS
.LINK
https://github.com/rstolpe/MaintainModule/blob/main/README.md
.NOTES
Expand All @@ -58,8 +58,8 @@
Param(
[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")]
[string]$Module,
[ValidateSet("CurrentUser", "AllUsers")]
[Parameter(Mandatory = $true, HelpMessage = "Enter CurrentUser or AllUsers depending on what scope you want to change your modules")]
[ValidateSet("CurrentUser", "AllUsers")]
[Parameter(Mandatory = $false, HelpMessage = "Enter CurrentUser or AllUsers depending on what scope you want to change your modules")]
[string]$Scope = "CurrentUser",
[Parameter(Mandatory = $false, HelpMessage = "Import modules that has been entered in the module parameter at the end of this function")]
[switch]$ImportModule = $false,
Expand All @@ -85,7 +85,21 @@
}
else {
Write-Verbose "User has added modules to the Module parameter, splitting them"
$Module = $Module.Split(",").Trim()
$OldModule = $Module.Split(",").Trim()

[System.Collections.ArrayList]$Module = @()
if ($InstallMissing -eq $false) {
Write-Verbose "Looking so the modules exists in the system..."
foreach ($m in $OldModule) {
if ($m -in $InstalledModules.name) {
Write-Verbose "$($m) did exists in the system..."
[void]($Module.Add($m))
}
else {
Write-Warning "$($m) did not exists in the system, skipping this module..."
}
}
}
}

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

# Start looping trough every module that are stored in the string Module
foreach ($m in $Module.Split()) {

Write-Verbose "Checks if $($m) are installed"
if ($m -in $InstalledModules.Name) {

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

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

# Looking if the version of the module are the latest version
if ($GetAllInstalledVersions.Version -lt $CollectLatestVersion.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 ($LatestInstalledVersion -lt $CollectLatestVersion) {
try {
Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)"
Write-Output "Updating $($m) to version $($CollectLatestVersion.Version)..."
Write-Output "Found a newer version of $($m), version $($CollectLatestVersion)"
Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..."
Update-Module -Name $($m) -Scope $Scope -Force
Write-Output "$($m) has been updated to version $($CollectLatestVersion.Version)!"
Write-Output "$($m) has now been updated to version $($CollectLatestVersion)!`n"
}
catch {
Write-Error "$($PSItem.Exception)"
continue
}
}

# If switch -UninstallOldVersion has been used then the old versions will be uninstalled from the module
if ($UninstallOldVersion -eq $true) {
# If switch -UninstallOldVersion has been used then the old versions will be uninstalled from the module
if ($UninstallOldVersion -eq $true) {
if ($GetAllInstalledVersions.Count -gt 1) {
Uninstall-RSModule -Module $m
}
}
Expand Down Expand Up @@ -168,7 +184,7 @@
# Collect all of the imported modules.
Write-Verbose "Collecting all of the installed modules..."
$ImportedModules = Get-Module | Select-Object Name, Version

# Import module if it's not imported
Write-Verbose "Starting to import the modules..."
foreach ($m in $Module.Split()) {
Expand Down
8 changes: 4 additions & 4 deletions MaintainModule/MaintainModule.psd1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<#
Copyright (C) 2022 Robin Stolpe.
<#
Copyright (C) 2022 Robin Stolpe.
<https://stolpe.io>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -18,7 +18,7 @@
#
# Generated by: Robin Stolpe
#
# Generated on: 2022-11-27
# Generated on: 2022-11-30
#

@{
Expand Down Expand Up @@ -125,7 +125,7 @@
ReleaseNotes = 'https://github.com/rstolpe/MaintainModule/releases'

# Prerelease string of this module
Prerelease = 'beta'
# Prerelease = ''

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

0 comments on commit aae0473

Please sign in to comment.