Skip to content

Commit c2d907d

Browse files
author
Robin Stolpe
committed
Update
1 parent ebb8fb2 commit c2d907d

1 file changed

Lines changed: 318 additions & 0 deletions

File tree

MaintainModule/MaintainModule.psm1

Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7323,3 +7323,321 @@ Function Update-RSModule {
73237323
}
73247324

73257325
Update-RSModule -UninstallOldVersion -Scope CurrentUser
7326+
<#
7327+
Copyright (C) 2022 Robin Stolpe.
7328+
<https://stolpe.io>
7329+
This program is free software: you can redistribute it and/or modify
7330+
it under the terms of the GNU General Public License as published by
7331+
the Free Software Foundation, either version 3 of the License, or
7332+
(at your option) any later version.
7333+
This program is distributed in the hope that it will be useful,
7334+
but WITHOUT ANY WARRANTY; without even the implied warranty of
7335+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7336+
GNU General Public License for more details.
7337+
You should have received a copy of the GNU General Public License
7338+
along with this program. If not, see <https://www.gnu.org/licenses/>.
7339+
#>
7340+
#
7341+
#
7342+
Function Uninstall-RSModule {
7343+
<#
7344+
.SYNOPSIS
7345+
Uninstall older versions of your modules in a easy way.
7346+
7347+
.DESCRIPTION
7348+
This script let users uninstall older versions of the modules that are installed on the system.
7349+
7350+
.PARAMETER Module
7351+
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
7352+
7353+
.EXAMPLE
7354+
Uninstall-RSModule -Module "VMWare.PowerCLI"
7355+
# This will uninstall all older versions of the module VMWare.PowerCLI system.
7356+
7357+
.EXAMPLE
7358+
Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel"
7359+
# This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system.
7360+
7361+
.LINK
7362+
https://github.com/rstolpe/MaintainModule/blob/main/README.md
7363+
7364+
.NOTES
7365+
Author: Robin Stolpe
7366+
Mail: robin@stolpe.io
7367+
Website: https://stolpe.io
7368+
GitHub: https://github.com/rstolpe
7369+
Twitter: https://twitter.com/rstolpes
7370+
PSGallery: https://www.powershellgallery.com/profiles/rstolpe
7371+
#>
7372+
7373+
[CmdletBinding(SupportsShouldProcess)]
7374+
Param(
7375+
[Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")]
7376+
[string]$Module
7377+
)
7378+
7379+
Write-Output "`n=== Starting to uninstall older versions of modules ===`n"
7380+
Write-Output "Please wait, this can take some time..."
7381+
7382+
Write-Verbose "Caching all installed modules from the system..."
7383+
$InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name
7384+
7385+
# If Module parameter is empty populate it with all modules that are installed on the system
7386+
if ([string]::IsNullOrEmpty($Module)) {
7387+
Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..."
7388+
$Module = $InstalledModules.Name
7389+
}
7390+
else {
7391+
Write-Verbose "User has added modules to the Module parameter, splitting them"
7392+
$OldModule = $Module.Split(",").Trim()
7393+
7394+
[System.Collections.ArrayList]$Module = @()
7395+
Write-Verbose "Looking so the modules exists in the system..."
7396+
foreach ($m in $OldModule) {
7397+
if ($m -in $InstalledModules.name) {
7398+
Write-Verbose "$($m) did exists in the system..."
7399+
[void]($Module.Add($m))
7400+
}
7401+
else {
7402+
Write-Warning "$($m) did not exists in the system, skipping this module..."
7403+
}
7404+
}
7405+
}
7406+
7407+
foreach ($m in $Module.Split()) {
7408+
Write-Verbose "Collecting all installed version of the module $($m)"
7409+
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -ExpandProperty Version
7410+
7411+
# If the module has more then one version loop trough the versions and only keep the most current one
7412+
if ($GetAllInstalledVersions.Count -gt 1) {
7413+
$MostRecentVersion = $null
7414+
[version]$MostRecentVersion = $GetAllInstalledVersions[0]
7415+
Foreach ($Version in $GetAllInstalledVersions | Where-Object { [version]$_ -lt [version]$MostRecentVersion }) {
7416+
try {
7417+
Write-Output "Uninstalling previous version $($Version) of module $($m)..."
7418+
Uninstall-Module -Name $m -RequiredVersion $Version -Force -ErrorAction SilentlyContinue
7419+
Write-Output "Version $($Version) of $($m) are now uninstalled!"
7420+
}
7421+
catch {
7422+
Write-Error "$($PSItem.Exception)"
7423+
continue
7424+
}
7425+
}
7426+
# bygga in en check så att den verkligen kan verifiera detta
7427+
Write-Output "All older versions of $($m) are now uninstalled, the only installed version of $($m) is $($MostRecentVersion)"
7428+
}
7429+
else {
7430+
Write-Verbose "$($m) don't have any older versions installed then $($GetAllInstalledVersions), no need to uninstall anything."
7431+
}
7432+
}
7433+
Write-Output "`n---/// Script Finished! ///---"
7434+
}
7435+
Function Update-RSModule {
7436+
<#
7437+
.SYNOPSIS
7438+
This module let you maintain your installed modules in a easy way.
7439+
7440+
.DESCRIPTION
7441+
This function let you update all of your installed modules and also uninstall the old versions to keep things clean.
7442+
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.
7443+
7444+
.PARAMETER Module
7445+
Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated
7446+
7447+
.PARAMETER Scope
7448+
Need to specify scope of the installation/update for the module, either AllUsers or CurrentUser. Default is CurrentUser.
7449+
If this parameter is empty it will use CurrentUser
7450+
The parameter -Scope don't effect the uninstall-module function this is because of limitation from Microsoft.
7451+
- Scope effect Install/update module function.
7452+
7453+
.PARAMETER ImportModule
7454+
If this switch are used the module will import all the modules that are specified in the Module parameter at the end of the script.
7455+
This only works if you have specified modules in the Module parameter
7456+
7457+
.PARAMETER UninstallOldVersion
7458+
If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed
7459+
7460+
.PARAMETER InstallMissing
7461+
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.
7462+
7463+
.EXAMPLE
7464+
Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser
7465+
# This will update the modules PowerCLI, ImportExcel for the current user
7466+
7467+
.EXAMPLE
7468+
Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion
7469+
# This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel.
7470+
7471+
.EXAMPLE
7472+
Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing
7473+
# 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.
7474+
7475+
.EXAMPLE
7476+
Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion -ImportModule
7477+
# 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.
7478+
7479+
.LINK
7480+
https://github.com/rstolpe/MaintainModule/blob/main/README.md
7481+
7482+
.NOTES
7483+
Author: Robin Stolpe
7484+
Mail: robin@stolpe.io
7485+
Twitter: @rstolpes
7486+
Website: https://stolpe.io
7487+
GitHub: https://github.com/rstolpe
7488+
PSGallery: https://www.powershellgallery.com/profiles/rstolpe
7489+
#>
7490+
7491+
[CmdletBinding(SupportsShouldProcess)]
7492+
Param(
7493+
[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")]
7494+
[string]$Module,
7495+
[ValidateSet("CurrentUser", "AllUsers")]
7496+
[Parameter(Mandatory = $false, HelpMessage = "Enter CurrentUser or AllUsers depending on what scope you want to change your modules")]
7497+
[string]$Scope = "CurrentUser",
7498+
[Parameter(Mandatory = $false, HelpMessage = "Import modules that has been entered in the module parameter at the end of this function")]
7499+
[switch]$ImportModule = $false,
7500+
[Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")]
7501+
[switch]$UninstallOldVersion = $false,
7502+
[Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")]
7503+
[switch]$InstallMissing = $false
7504+
)
7505+
7506+
Write-Output "`n=== Starting module maintenance ===`n"
7507+
Write-Output "Please wait, this can take some time..."
7508+
7509+
# Collect all installed modules from the system
7510+
Write-Verbose "Caching all installed modules from the system..."
7511+
$InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name
7512+
$EmptyModule = $false
7513+
7514+
# If Module parameter is empty populate it with all modules that are installed on the system
7515+
if ([string]::IsNullOrEmpty($Module)) {
7516+
Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..."
7517+
$EmptyModule = $true
7518+
$Module = $InstalledModules.Name
7519+
}
7520+
else {
7521+
Write-Verbose "User has added modules to the Module parameter, splitting them"
7522+
$OldModule = $Module.Split(",").Trim()
7523+
7524+
[System.Collections.ArrayList]$Module = @()
7525+
if ($InstallMissing -eq $false) {
7526+
Write-Verbose "Looking so the modules exists in the system..."
7527+
foreach ($m in $OldModule) {
7528+
if ($m -in $InstalledModules.name) {
7529+
Write-Verbose "$($m) did exists in the system..."
7530+
[void]($Module.Add($m))
7531+
}
7532+
else {
7533+
Write-Warning "$($m) did not exists in the system, skipping this module..."
7534+
}
7535+
}
7536+
}
7537+
}
7538+
7539+
# Making sure that TLS 1.2 is used.
7540+
Write-Verbose "Making sure that TLS 1.2 is used..."
7541+
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
7542+
7543+
# Checking if PSGallery are set to trusted
7544+
Write-Verbose "Checking if PowerShell Gallery are set to trusted..."
7545+
if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") {
7546+
try {
7547+
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
7548+
Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!"
7549+
}
7550+
catch {
7551+
Write-Error "$($PSItem.Exception)"
7552+
continue
7553+
}
7554+
}
7555+
else {
7556+
Write-Verbose "PowerShell Gallery was already set to trusted, continuing!"
7557+
}
7558+
7559+
7560+
# Start looping trough every module that are stored in the string Module
7561+
foreach ($m in $Module.Split()) {
7562+
Write-Verbose "Checks if $($m) are installed"
7563+
if ($m -in $InstalledModules.Name) {
7564+
7565+
# Getting the latest installed version of the module
7566+
Write-Verbose "Collecting all installed version of $($m)..."
7567+
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object Version
7568+
[version]$LatestInstalledVersion = $($GetAllInstalledVersions | Select-Object Version -First 1).version
7569+
7570+
# Collects the latest version of module from the source where the module was installed from
7571+
Write-Output "Looking up the latest version of $($m)..."
7572+
[version]$CollectLatestVersion = $(Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object Version -First 1).version
7573+
7574+
# Looking if the version of the module are the latest version, it it's not the latest it will install the latest version.
7575+
if ($LatestInstalledVersion -lt $CollectLatestVersion) {
7576+
try {
7577+
Write-Output "Found a newer version of $($m), version $($CollectLatestVersion)"
7578+
Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..."
7579+
Update-Module -Name $($m) -Scope $Scope -Force
7580+
Write-Output "$($m) has now been updated to version $($CollectLatestVersion)!`n"
7581+
}
7582+
catch {
7583+
Write-Error "$($PSItem.Exception)"
7584+
continue
7585+
}
7586+
}
7587+
7588+
# If switch -UninstallOldVersion has been used then the old versions will be uninstalled from the module
7589+
if ($UninstallOldVersion -eq $true) {
7590+
if ($GetAllInstalledVersions.Count -gt 1) {
7591+
Uninstall-RSModule -Module $m
7592+
}
7593+
}
7594+
else {
7595+
Write-Verbose "$($m) already has the newest version installed, no need to install anything!"
7596+
}
7597+
}
7598+
else {
7599+
# If the switch InstallMissing are set to true the modules will get installed if they are missing
7600+
if ($InstallMissing -eq $true) {
7601+
try {
7602+
Write-Output "$($m) are not installed, installing $($m)..."
7603+
Install-Module -Name $($m) -Scope $Scope -Force
7604+
Write-Output "$($m) has now been installed!"
7605+
}
7606+
catch {
7607+
Write-Error "$($PSItem.Exception)"
7608+
continue
7609+
}
7610+
}
7611+
else {
7612+
Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!"
7613+
}
7614+
}
7615+
}
7616+
if ($EmptyModule -eq $false) {
7617+
if ($ImportModule -eq $true) {
7618+
# Collect all of the imported modules.
7619+
Write-Verbose "Collecting all of the installed modules..."
7620+
$ImportedModules = Get-Module | Select-Object Name, Version
7621+
7622+
# Import module if it's not imported
7623+
Write-Verbose "Starting to import the modules..."
7624+
foreach ($m in $Module.Split()) {
7625+
if ($m -in $ImportedModules.Name) {
7626+
Write-Verbose "$($m) are already imported!"
7627+
}
7628+
else {
7629+
try {
7630+
Write-Output "Importing $($m)..."
7631+
Import-Module -Name $m -Force
7632+
Write-Output "$($m) has been imported!"
7633+
}
7634+
catch {
7635+
Write-Error "$($PSItem.Exception)"
7636+
continue
7637+
}
7638+
}
7639+
}
7640+
}
7641+
}
7642+
Write-Output "`n---/// Script Finished! ///---"
7643+
}

0 commit comments

Comments
 (0)