Skip to content

Commit f62841e

Browse files
author
Robin Stolpe
committed
update
1 parent ddde042 commit f62841e

1 file changed

Lines changed: 320 additions & 0 deletions

File tree

MaintainModule/MaintainModule.psm1

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6043,3 +6043,323 @@ Function Update-RSModule {
60436043
}
60446044

60456045
Update-RSModule -UninstallOldVersion -Scope CurrentUser
6046+
<#
6047+
Copyright (C) 2022 Robin Stolpe.
6048+
<https://stolpe.io>
6049+
This program is free software: you can redistribute it and/or modify
6050+
it under the terms of the GNU General Public License as published by
6051+
the Free Software Foundation, either version 3 of the License, or
6052+
(at your option) any later version.
6053+
This program is distributed in the hope that it will be useful,
6054+
but WITHOUT ANY WARRANTY; without even the implied warranty of
6055+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6056+
GNU General Public License for more details.
6057+
You should have received a copy of the GNU General Public License
6058+
along with this program. If not, see <https://www.gnu.org/licenses/>.
6059+
#>
6060+
#
6061+
#
6062+
Function Uninstall-RSModule {
6063+
<#
6064+
.SYNOPSIS
6065+
Uninstall older versions of your modules in a easy way.
6066+
6067+
.DESCRIPTION
6068+
This script let users uninstall older versions of the modules that are installed on the system.
6069+
6070+
.PARAMETER Module
6071+
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
6072+
6073+
.EXAMPLE
6074+
Uninstall-RSModule -Module "VMWare.PowerCLI"
6075+
# This will uninstall all older versions of the module VMWare.PowerCLI system.
6076+
6077+
.EXAMPLE
6078+
Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel"
6079+
# This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system.
6080+
6081+
.LINK
6082+
https://github.com/rstolpe/MaintainModule/blob/main/README.md
6083+
6084+
.NOTES
6085+
Author: Robin Stolpe
6086+
Mail: robin@stolpe.io
6087+
Website: https://stolpe.io
6088+
GitHub: https://github.com/rstolpe
6089+
Twitter: https://twitter.com/rstolpes
6090+
PSGallery: https://www.powershellgallery.com/profiles/rstolpe
6091+
#>
6092+
6093+
[CmdletBinding(SupportsShouldProcess)]
6094+
Param(
6095+
[Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")]
6096+
[string]$Module
6097+
)
6098+
6099+
Write-Output "`n=== Starting to uninstall older versions of modules ===`n"
6100+
Write-Output "Please wait, this can take some time..."
6101+
6102+
Write-Verbose "Caching all installed modules from the system..."
6103+
$InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name
6104+
6105+
# If Module parameter is empty populate it with all modules that are installed on the system
6106+
if ([string]::IsNullOrEmpty($Module)) {
6107+
Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..."
6108+
$Module = $InstalledModules.Name
6109+
}
6110+
else {
6111+
Write-Verbose "User has added modules to the Module parameter, splitting them"
6112+
$OldModule = $Module.Split(",").Trim()
6113+
6114+
[System.Collections.ArrayList]$Module = @()
6115+
Write-Verbose "Looking so the modules exists in the system..."
6116+
foreach ($m in $OldModule) {
6117+
if ($m -in $InstalledModules.name) {
6118+
Write-Verbose "$($m) did exists in the system..."
6119+
[void]($Module.Add($m))
6120+
}
6121+
else {
6122+
Write-Warning "$($m) did not exists in the system, skipping this module..."
6123+
}
6124+
}
6125+
}
6126+
6127+
foreach ($m in $Module.Split()) {
6128+
Write-Verbose "Collecting all installed version of the module $($m)"
6129+
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -ExpandProperty Version
6130+
6131+
# If the module has more then one version loop trough the versions and only keep the most current one
6132+
if ($GetAllInstalledVersions.Count -gt 1) {
6133+
$MostRecentVersion = $null
6134+
[version]$MostRecentVersion = $GetAllInstalledVersions[0]
6135+
Foreach ($Version in $GetAllInstalledVersions | Where-Object { [version]$_ -lt [version]$MostRecentVersion }) {
6136+
try {
6137+
Write-Output "Uninstalling previous version $($Version) of module $($m)..."
6138+
Uninstall-Module -Name $m -RequiredVersion $Version -Force -ErrorAction SilentlyContinue
6139+
Write-Output "Version $($Version) of $($m) are now uninstalled!"
6140+
}
6141+
catch {
6142+
Write-Error "$($PSItem.Exception)"
6143+
continue
6144+
}
6145+
}
6146+
# bygga in en check så att den verkligen kan verifiera detta
6147+
Write-Output "All older versions of $($m) are now uninstalled, the only installed version of $($m) is $($MostRecentVersion)"
6148+
}
6149+
else {
6150+
Write-Verbose "$($m) don't have any older versions installed then $($GetAllInstalledVersions), no need to uninstall anything."
6151+
}
6152+
}
6153+
Write-Output "`n---/// Script Finished! ///---"
6154+
}
6155+
Function Update-RSModule {
6156+
<#
6157+
.SYNOPSIS
6158+
This module let you maintain your installed modules in a easy way.
6159+
6160+
.DESCRIPTION
6161+
This function let you update all of your installed modules and also uninstall the old versions to keep things clean.
6162+
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.
6163+
6164+
.PARAMETER Module
6165+
Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated
6166+
6167+
.PARAMETER Scope
6168+
Need to specify scope of the installation/update for the module, either AllUsers or CurrentUser. Default is CurrentUser.
6169+
If this parameter is empty it will use CurrentUser
6170+
The parameter -Scope don't effect the uninstall-module function this is because of limitation from Microsoft.
6171+
- Scope effect Install/update module function.
6172+
6173+
.PARAMETER ImportModule
6174+
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.
6175+
This only works if you have specified modules in the Module parameter
6176+
6177+
.PARAMETER UninstallOldVersion
6178+
If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed
6179+
6180+
.PARAMETER InstallMissing
6181+
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.
6182+
6183+
.EXAMPLE
6184+
Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser
6185+
# This will update the modules PowerCLI, ImportExcel for the current user
6186+
6187+
.EXAMPLE
6188+
Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion
6189+
# This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel.
6190+
6191+
.EXAMPLE
6192+
Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing
6193+
# 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.
6194+
6195+
.EXAMPLE
6196+
Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion -ImportModule
6197+
# 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.
6198+
6199+
.LINK
6200+
https://github.com/rstolpe/MaintainModule/blob/main/README.md
6201+
6202+
.NOTES
6203+
Author: Robin Stolpe
6204+
Mail: robin@stolpe.io
6205+
Twitter: @rstolpes
6206+
Website: https://stolpe.io
6207+
GitHub: https://github.com/rstolpe
6208+
PSGallery: https://www.powershellgallery.com/profiles/rstolpe
6209+
#>
6210+
6211+
[CmdletBinding(SupportsShouldProcess)]
6212+
Param(
6213+
[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")]
6214+
[string]$Module,
6215+
[ValidateSet("CurrentUser", "AllUsers")]
6216+
[Parameter(Mandatory = $false, HelpMessage = "Enter CurrentUser or AllUsers depending on what scope you want to change your modules")]
6217+
[string]$Scope = "CurrentUser",
6218+
[Parameter(Mandatory = $false, HelpMessage = "Import modules that has been entered in the module parameter at the end of this function")]
6219+
[switch]$ImportModule = $false,
6220+
[Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")]
6221+
[switch]$UninstallOldVersion = $false,
6222+
[Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")]
6223+
[switch]$InstallMissing = $false
6224+
)
6225+
6226+
Write-Output "`n=== Starting module maintenance ===`n"
6227+
Write-Output "Please wait, this can take some time..."
6228+
6229+
# Collect all installed modules from the system
6230+
Write-Verbose "Caching all installed modules from the system..."
6231+
$InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name
6232+
$EmptyModule = $false
6233+
6234+
# If Module parameter is empty populate it with all modules that are installed on the system
6235+
if ([string]::IsNullOrEmpty($Module)) {
6236+
Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..."
6237+
$EmptyModule = $true
6238+
$Module = $InstalledModules.Name
6239+
}
6240+
else {
6241+
Write-Verbose "User has added modules to the Module parameter, splitting them"
6242+
$OldModule = $Module.Split(",").Trim()
6243+
6244+
[System.Collections.ArrayList]$Module = @()
6245+
if ($InstallMissing -eq $false) {
6246+
Write-Verbose "Looking so the modules exists in the system..."
6247+
foreach ($m in $OldModule) {
6248+
if ($m -in $InstalledModules.name) {
6249+
Write-Verbose "$($m) did exists in the system..."
6250+
[void]($Module.Add($m))
6251+
}
6252+
else {
6253+
Write-Warning "$($m) did not exists in the system, skipping this module..."
6254+
}
6255+
}
6256+
}
6257+
}
6258+
6259+
# Making sure that TLS 1.2 is used.
6260+
Write-Verbose "Making sure that TLS 1.2 is used..."
6261+
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
6262+
6263+
# Checking if PSGallery are set to trusted
6264+
Write-Verbose "Checking if PowerShell Gallery are set to trusted..."
6265+
if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") {
6266+
try {
6267+
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
6268+
Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!"
6269+
}
6270+
catch {
6271+
Write-Error "$($PSItem.Exception)"
6272+
continue
6273+
}
6274+
}
6275+
else {
6276+
Write-Verbose "PowerShell Gallery was already set to trusted, continuing!"
6277+
}
6278+
6279+
6280+
# Start looping trough every module that are stored in the string Module
6281+
foreach ($m in $Module.Split()) {
6282+
Write-Verbose "Checks if $($m) are installed"
6283+
if ($m -in $InstalledModules.Name) {
6284+
6285+
# Getting the latest installed version of the module
6286+
Write-Verbose "Collecting all installed version of $($m)..."
6287+
$GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object Version
6288+
[version]$LatestInstalledVersion = $($GetAllInstalledVersions | Select-Object Version -First 1).version
6289+
6290+
# Collects the latest version of module from the source where the module was installed from
6291+
Write-Output "Looking up the latest version of $($m)..."
6292+
[version]$CollectLatestVersion = $(Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object Version -First 1).version
6293+
6294+
# Looking if the version of the module are the latest version, it it's not the latest it will install the latest version.
6295+
if ($LatestInstalledVersion -lt $CollectLatestVersion) {
6296+
try {
6297+
Write-Output "Found a newer version of $($m), version $($CollectLatestVersion)"
6298+
Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..."
6299+
Update-Module -Name $($m) -Scope $Scope -Force
6300+
Write-Output "$($m) has now been updated to version $($CollectLatestVersion)!`n"
6301+
}
6302+
catch {
6303+
Write-Error "$($PSItem.Exception)"
6304+
continue
6305+
}
6306+
}
6307+
6308+
# If switch -UninstallOldVersion has been used then the old versions will be uninstalled from the module
6309+
if ($UninstallOldVersion -eq $true) {
6310+
if ($GetAllInstalledVersions.Count -gt 1) {
6311+
Uninstall-RSModule -Module $m
6312+
}
6313+
}
6314+
else {
6315+
Write-Verbose "$($m) already has the newest version installed, no need to install anything!"
6316+
}
6317+
}
6318+
else {
6319+
# If the switch InstallMissing are set to true the modules will get installed if they are missing
6320+
if ($InstallMissing -eq $true) {
6321+
try {
6322+
Write-Output "$($m) are not installed, installing $($m)..."
6323+
Install-Module -Name $($m) -Scope $Scope -Force
6324+
Write-Output "$($m) has now been installed!"
6325+
}
6326+
catch {
6327+
Write-Error "$($PSItem.Exception)"
6328+
continue
6329+
}
6330+
}
6331+
else {
6332+
Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!"
6333+
}
6334+
}
6335+
}
6336+
if ($EmptyModule -eq $false) {
6337+
if ($ImportModule -eq $true) {
6338+
# Collect all of the imported modules.
6339+
Write-Verbose "Collecting all of the installed modules..."
6340+
$ImportedModules = Get-Module | Select-Object Name, Version
6341+
6342+
# Import module if it's not imported
6343+
Write-Verbose "Starting to import the modules..."
6344+
foreach ($m in $Module.Split()) {
6345+
if ($m -in $ImportedModules.Name) {
6346+
Write-Verbose "$($m) are already imported!"
6347+
}
6348+
else {
6349+
try {
6350+
Write-Output "Importing $($m)..."
6351+
Import-Module -Name $m -Force
6352+
Write-Output "$($m) has been imported!"
6353+
}
6354+
catch {
6355+
Write-Error "$($PSItem.Exception)"
6356+
continue
6357+
}
6358+
}
6359+
}
6360+
}
6361+
}
6362+
Write-Output "`n---/// Script Finished! ///---"
6363+
}
6364+
6365+
Update-RSModule -UninstallOldVersion -Scope CurrentUser

0 commit comments

Comments
 (0)