From 271e9326956b0fe5a10911d1642add0bb6533818 Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Wed, 30 Nov 2022 22:29:34 +0100 Subject: [PATCH 01/11] update --- MaintainModule/MaintainModule.psd1 | 2 +- MaintainModule/MaintainModule.psm1 | 7643 ---------------------------- RSModuleBuilder.ps1 | 12 +- 3 files changed, 10 insertions(+), 7647 deletions(-) delete mode 100644 MaintainModule/MaintainModule.psm1 diff --git a/MaintainModule/MaintainModule.psd1 b/MaintainModule/MaintainModule.psd1 index 5dcc12a..01f5993 100644 --- a/MaintainModule/MaintainModule.psd1 +++ b/MaintainModule/MaintainModule.psd1 @@ -27,7 +27,7 @@ RootModule = '.\MaintainModule.psm1' # Version number of this module. - ModuleVersion = '0.0.8' + ModuleVersion = '0.0.9' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/MaintainModule/MaintainModule.psm1 b/MaintainModule/MaintainModule.psm1 deleted file mode 100644 index c1a5730..0000000 --- a/MaintainModule/MaintainModule.psm1 +++ /dev/null @@ -1,7643 +0,0 @@ -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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) { - [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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetLatestInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$GetLatestInstalledVersions.Version -lt [version]$CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($GetLatestInstalledVersions.Version) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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) { - Uninstall-RSModule -Module $m - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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) { - [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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetLatestInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$GetLatestInstalledVersions.Version -lt [version]$CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($GetLatestInstalledVersions.Version) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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) { - Uninstall-RSModule -Module $m - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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) { - [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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetLatestInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$GetLatestInstalledVersions.Version -lt [version]$CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($GetLatestInstalledVersions.Version) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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) { - Uninstall-RSModule -Module $m - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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) { - [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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetLatestInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$GetLatestInstalledVersions.Version -lt [version]$CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($GetLatestInstalledVersions.Version) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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) { - Uninstall-RSModule -Module $m - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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) { - [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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetLatestInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$GetLatestInstalledVersions.Version -lt [version]$CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($GetLatestInstalledVersions.Version) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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) { - Uninstall-RSModule -Module $m - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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) { - [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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -ExpandProperty Version - [version]$MostRecentVersion = $GetAllInstalledVersions[0] - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$MostRecentVersion -lt [version]$CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($MostRecentVersion) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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) { - [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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -ExpandProperty Version - [version]$MostRecentVersion = $GetAllInstalledVersions[0] - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$MostRecentVersion -lt [version]$CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($MostRecentVersion) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # Start looping trough every module that are stored in the string Module - foreach ($m in $Module.Split()) { - Write-Verbose "Checks if $($m) are installed" - $MostRecentVersion = $null - if ($m -in $InstalledModules.Name) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -ExpandProperty Version - [version]$MostRecentVersion = $GetAllInstalledVersions[0] - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$MostRecentVersion -lt [version]$CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($MostRecentVersion) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # Start looping trough every module that are stored in the string Module - foreach ($m in $Module.Split()) { - Write-Verbose "Checks if $($m) are installed" - $MostRecentVersion = $null - if ($m -in $InstalledModules.Name) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -ExpandProperty Version - [version]$MostRecentVersion = $GetAllInstalledVersions[0] - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$MostRecentVersion -lt [version]$CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($MostRecentVersion) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # Start looping trough every module that are stored in the string Module - foreach ($m in $Module.Split()) { - Write-Verbose "Checks if $($m) are installed" - $MostRecentVersion = $null - if ($m -in $InstalledModules.Name) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -ExpandProperty Version - [version]$MostRecentVersion = $GetAllInstalledVersions[0] - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$MostRecentVersion -lt [version]$CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($MostRecentVersion) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # Start looping trough every module that are stored in the string Module - foreach ($m in $Module.Split()) { - Write-Verbose "Checks if $($m) are installed" - $MostRecentVersion = $null - if ($m -in $InstalledModules.Name) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -ExpandProperty Version - [version]$MostRecentVersion = $GetAllInstalledVersions[0] - - # Collects the latest version of module from the source where the module was installed from - Write-Verbose "Looking up the latest version of $($m)..." - $CollectLatestVersion = Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object -First 1 - - # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. - if ([version]$MostRecentVersion -lt $CollectLatestVersion.Version) { - try { - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" - Write-Output "Updating $($m) from $($MostRecentVersion) to version $($CollectLatestVersion.Version)..." - Update-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been updated to version $($CollectLatestVersion.Version)!`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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} - -Update-RSModule -UninstallOldVersion -Scope CurrentUser -<# - Copyright (C) 2022 Robin Stolpe. - - 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 - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . - #> -# -# -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} diff --git a/RSModuleBuilder.ps1 b/RSModuleBuilder.ps1 index 4c75c1b..c3640fb 100644 --- a/RSModuleBuilder.ps1 +++ b/RSModuleBuilder.ps1 @@ -1,7 +1,7 @@ param ( # Set this to true before releasing the module [Parameter(Mandatory = $false, HelpMessage = "Enter the version number of this release")] - [string]$Version = "0.0.8", + [string]$Version = "0.0.9", # Fix this [Parameter(Mandatory = $false, HelpMessage = ".")] [string]$preRelease = "Alpha", @@ -36,6 +36,12 @@ $TestPath = Join-Path -Path $scriptPath -ChildPath "test" Write-OutPut "`n== Building module $($ModuleName) ==`n" Write-OutPut "Starting to build the module, please wait..." +# Check so all the needed folders exists, if they don't they will get created. +Checkpoint-RSFolderFile -ModulePath $scriptPath -ModuleName $ModuleName -New $false + +# Deleting existing files that will get replaced by this script +Remove-RSContent -ModuleName $ModuleName -ScriptPath $scriptPath -ExistingModule + # Adding the text from the gnu3_add_file_licens.source to the to of the .psm1 file for licensing of GNU v3 # Let user choose between GNU 3 or MIT $psmLicens = Get-Content -Path "$($psmLicensPath)/gnu3_add_file_licens.source" -ErrorAction SilentlyContinue @@ -145,9 +151,9 @@ $ResultPSDPSM = foreach ($file in $CheckPSA) { # Import the module and save the Get-Help files to the $HelpPath for the module, files get saved in .md format Write-Verbose "Importing $($ModuleName) to the session..." -Import-Module -Name $ModuleFolderPath -MinimumVersion $Version -Force +Import-Module -Name $($ModuleFolderPath) -MinimumVersion $Version -Force -#Write-Verbose "Writing $($ModuleName) functions to help files in $($HelpPath)..." +Write-Verbose "Writing $($ModuleName) functions to help files in $($HelpPath)..." $mCommands = Get-Command -Module $ModuleName foreach ($m in $mCommands) { if ($null -ne $m) { From 4789164552bbca8f59a90cb15db5211c8ffc4c40 Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Wed, 30 Nov 2022 22:37:56 +0100 Subject: [PATCH 02/11] update --- MaintainModule/MaintainModule.psm1 | 318 ++++++++++++++++++ help/Uninstall-RSModule.md | 87 ----- help/Update-RSModule.md | 148 -------- ...Analyzer_MaintainModule.psd1_2022-11-30.md | 1 - ...Analyzer_MaintainModule.psm1_2022-11-30.md | 1 - ...riptAnalyzer_Update-RSModule_2022-11-30.md | 1 - 6 files changed, 318 insertions(+), 238 deletions(-) create mode 100644 MaintainModule/MaintainModule.psm1 delete mode 100644 help/Uninstall-RSModule.md delete mode 100644 help/Update-RSModule.md delete mode 100644 test/PSScriptAnalyzer_MaintainModule.psd1_2022-11-30.md delete mode 100644 test/PSScriptAnalyzer_MaintainModule.psm1_2022-11-30.md delete mode 100644 test/PSScriptAnalyzer_Update-RSModule_2022-11-30.md diff --git a/MaintainModule/MaintainModule.psm1 b/MaintainModule/MaintainModule.psm1 new file mode 100644 index 0000000..193272f --- /dev/null +++ b/MaintainModule/MaintainModule.psm1 @@ -0,0 +1,318 @@ +<# + Copyright (C) 2022 Robin Stolpe. + + 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . + #> +# +# +Function Uninstall-RSModule { + <# + .SYNOPSIS + Uninstall older versions of your modules in a easy way. + + .DESCRIPTION + This script let users uninstall older versions of the modules that are installed on the system. + + .PARAMETER Module + 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 + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI" + # This will uninstall all older versions of the module VMWare.PowerCLI system. + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" + # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + Twitter: https://twitter.com/rstolpes + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + Param( + [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] + [string]$Module + ) + + Write-Output "`n=== Starting to uninstall older versions of modules ===`n" + Write-Output "Please wait, this can take some time..." + + Write-Verbose "Caching all installed modules from the system..." + $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Update-RSModule { + <# + .SYNOPSIS + This module let you maintain your installed modules in a easy way. + + .DESCRIPTION + This function let you update all of your installed modules and also uninstall the old versions to keep things clean. + 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. + + .PARAMETER Module + Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated + + .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. + - Scope effect Install/update module function. + + .PARAMETER ImportModule + 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. + This only works if you have specified modules in the Module parameter + + .PARAMETER UninstallOldVersion + If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed + + .PARAMETER InstallMissing + 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. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser + # This will update the modules PowerCLI, ImportExcel for the current user + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion + # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing + # 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. + + .EXAMPLE + 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. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Twitter: @rstolpes + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + 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 = $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, + [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] + [switch]$UninstallOldVersion = $false, + [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] + [switch]$InstallMissing = $false + ) + + Write-Output "`n=== Starting module maintenance ===`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 + $EmptyModule = $false + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $EmptyModule = $true + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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. + Write-Verbose "Making sure that TLS 1.2 is used..." + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + + # Checking if PSGallery are set to trusted + Write-Verbose "Checking if PowerShell Gallery are set to trusted..." + if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { + try { + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted + Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" + } + + + # 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) { + + # Getting the latest installed version of the module + Write-Verbose "Collecting all installed version of $($m)..." + $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 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, 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)" + Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." + Update-Module -Name $($m) -Scope $Scope -Force + 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 ($GetAllInstalledVersions.Count -gt 1) { + Uninstall-RSModule -Module $m + } + } + else { + Write-Verbose "$($m) already has the newest version installed, no need to install anything!" + } + } + else { + # If the switch InstallMissing are set to true the modules will get installed if they are missing + if ($InstallMissing -eq $true) { + try { + Write-Output "$($m) are not installed, installing $($m)..." + Install-Module -Name $($m) -Scope $Scope -Force + Write-Output "$($m) has now been installed!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" + } + } + } + if ($EmptyModule -eq $false) { + if ($ImportModule -eq $true) { + # 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()) { + if ($m -in $ImportedModules.Name) { + Write-Verbose "$($m) are already imported!" + } + else { + try { + Write-Output "Importing $($m)..." + Import-Module -Name $m -Force + Write-Output "$($m) has been imported!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + } + } + } + Write-Output "`n---/// Script Finished! ///---" +} diff --git a/help/Uninstall-RSModule.md b/help/Uninstall-RSModule.md deleted file mode 100644 index b10f261..0000000 --- a/help/Uninstall-RSModule.md +++ /dev/null @@ -1,87 +0,0 @@ - -NAME - Uninstall-RSModule - -SYNOPSIS - Uninstall older versions of your modules in a easy way. - - -SYNTAX - Uninstall-RSModule [[-Module] ] [-WhatIf] [-Confirm] [] - - -DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - -PARAMETERS - -Module - 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 - - Required? false - Position? 1 - Default value - Accept pipeline input? false - Accept wildcard characters? false - - -WhatIf [] - - Required? false - Position? named - Default value - Accept pipeline input? false - Accept wildcard characters? false - - -Confirm [] - - Required? false - Position? named - Default value - Accept pipeline input? false - Accept wildcard characters? false - - - This cmdlet supports the common parameters: Verbose, Debug, - ErrorAction, ErrorVariable, WarningAction, WarningVariable, - OutBuffer, PipelineVariable, and OutVariable. For more information, see - about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216). - -INPUTS - -OUTPUTS - -NOTES - - - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - - -------------------------- EXAMPLE 1 -------------------------- - - PS > Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - - - - - - -------------------------- EXAMPLE 2 -------------------------- - - PS > Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - - - - - - -RELATED LINKS - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - diff --git a/help/Update-RSModule.md b/help/Update-RSModule.md deleted file mode 100644 index 8a2bc34..0000000 --- a/help/Update-RSModule.md +++ /dev/null @@ -1,148 +0,0 @@ - -NAME - Update-RSModule - -SYNOPSIS - This module let you maintain your installed modules in a easy way. - - -SYNTAX - Update-RSModule [[-Module] ] [[-Scope] ] [-ImportModule] [-UninstallOldVersion] [-InstallMissing] [-WhatIf] [-Confirm] [] - - -DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - -PARAMETERS - -Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - Required? false - Position? 1 - Default value - Accept pipeline input? false - Accept wildcard characters? false - - -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. - - Scope effect Install/update module function. - - Required? false - Position? 2 - Default value CurrentUser - Accept pipeline input? false - Accept wildcard characters? false - - -ImportModule [] - 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. - This only works if you have specified modules in the Module parameter - - Required? false - Position? named - Default value False - Accept pipeline input? false - Accept wildcard characters? false - - -UninstallOldVersion [] - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - Required? false - Position? named - Default value False - Accept pipeline input? false - Accept wildcard characters? false - - -InstallMissing [] - 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. - - Required? false - Position? named - Default value False - Accept pipeline input? false - Accept wildcard characters? false - - -WhatIf [] - - Required? false - Position? named - Default value - Accept pipeline input? false - Accept wildcard characters? false - - -Confirm [] - - Required? false - Position? named - Default value - Accept pipeline input? false - Accept wildcard characters? false - - - This cmdlet supports the common parameters: Verbose, Debug, - ErrorAction, ErrorVariable, WarningAction, WarningVariable, - OutBuffer, PipelineVariable, and OutVariable. For more information, see - about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216). - -INPUTS - -OUTPUTS - -NOTES - - - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - - -------------------------- EXAMPLE 1 -------------------------- - - PS > Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - - - - - - -------------------------- EXAMPLE 2 -------------------------- - - PS > Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - - - - - - -------------------------- EXAMPLE 3 -------------------------- - - PS > Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - - - - - - -------------------------- EXAMPLE 4 -------------------------- - - PS > 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 - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - diff --git a/test/PSScriptAnalyzer_MaintainModule.psd1_2022-11-30.md b/test/PSScriptAnalyzer_MaintainModule.psd1_2022-11-30.md deleted file mode 100644 index b30bd6b..0000000 --- a/test/PSScriptAnalyzer_MaintainModule.psd1_2022-11-30.md +++ /dev/null @@ -1 +0,0 @@ -0 rule violations found. diff --git a/test/PSScriptAnalyzer_MaintainModule.psm1_2022-11-30.md b/test/PSScriptAnalyzer_MaintainModule.psm1_2022-11-30.md deleted file mode 100644 index b30bd6b..0000000 --- a/test/PSScriptAnalyzer_MaintainModule.psm1_2022-11-30.md +++ /dev/null @@ -1 +0,0 @@ -0 rule violations found. diff --git a/test/PSScriptAnalyzer_Update-RSModule_2022-11-30.md b/test/PSScriptAnalyzer_Update-RSModule_2022-11-30.md deleted file mode 100644 index b30bd6b..0000000 --- a/test/PSScriptAnalyzer_Update-RSModule_2022-11-30.md +++ /dev/null @@ -1 +0,0 @@ -0 rule violations found. From 899f4225028c112fed302eed4a7673e95f33d5e3 Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Thu, 1 Dec 2022 07:44:13 +0100 Subject: [PATCH 03/11] update --- MaintainModule/MaintainModule.psd1 | 2 +- RSModuleBuilder.ps1 | 16 ++++++++-------- ...iptAnalyzer_Uninstall-RSModule_2022-12-01.md} | 0 3 files changed, 9 insertions(+), 9 deletions(-) rename test/{PSScriptAnalyzer_Uninstall-RSModule_2022-11-30.md => PSScriptAnalyzer_Uninstall-RSModule_2022-12-01.md} (100%) diff --git a/MaintainModule/MaintainModule.psd1 b/MaintainModule/MaintainModule.psd1 index 01f5993..32ddb48 100644 --- a/MaintainModule/MaintainModule.psd1 +++ b/MaintainModule/MaintainModule.psd1 @@ -18,7 +18,7 @@ # # Generated by: Robin Stolpe # -# Generated on: 2022-11-30 +# Generated on: 2022-12-01 # @{ diff --git a/RSModuleBuilder.ps1 b/RSModuleBuilder.ps1 index c3640fb..cfc9aae 100644 --- a/RSModuleBuilder.ps1 +++ b/RSModuleBuilder.ps1 @@ -151,16 +151,16 @@ $ResultPSDPSM = foreach ($file in $CheckPSA) { # Import the module and save the Get-Help files to the $HelpPath for the module, files get saved in .md format Write-Verbose "Importing $($ModuleName) to the session..." -Import-Module -Name $($ModuleFolderPath) -MinimumVersion $Version -Force +#Import-Module -Name $($ModuleFolderPath) -MinimumVersion $Version -Force Write-Verbose "Writing $($ModuleName) functions to help files in $($HelpPath)..." -$mCommands = Get-Command -Module $ModuleName -foreach ($m in $mCommands) { - if ($null -ne $m) { - Write-Verbose "Creating help file of function $($m.Name)..." - Get-Help -name $m.Name -Full | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $HelpPath -ChildPath "$($m.Name).md") - } -} +#$mCommands = Get-Command -Module $ModuleName +#foreach ($m in $mCommands) { +# if ($null -ne $m) { +# Write-Verbose "Creating help file of function $($m.Name)..." +# Get-Help -name $m.Name -Full | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $HelpPath -ChildPath "$($m.Name).md") +# } +#} Write-Output "`n== Summery of PSScriptAnalyzer ==" $ResultPS1 diff --git a/test/PSScriptAnalyzer_Uninstall-RSModule_2022-11-30.md b/test/PSScriptAnalyzer_Uninstall-RSModule_2022-12-01.md similarity index 100% rename from test/PSScriptAnalyzer_Uninstall-RSModule_2022-11-30.md rename to test/PSScriptAnalyzer_Uninstall-RSModule_2022-12-01.md From 4ccc53672c43608e4ee8166db50e8ced63db1cf9 Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Thu, 1 Dec 2022 07:49:00 +0100 Subject: [PATCH 04/11] update --- RSModuleBuilder.ps1 | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/RSModuleBuilder.ps1 b/RSModuleBuilder.ps1 index cfc9aae..c78e94e 100644 --- a/RSModuleBuilder.ps1 +++ b/RSModuleBuilder.ps1 @@ -151,25 +151,25 @@ $ResultPSDPSM = foreach ($file in $CheckPSA) { # Import the module and save the Get-Help files to the $HelpPath for the module, files get saved in .md format Write-Verbose "Importing $($ModuleName) to the session..." -#Import-Module -Name $($ModuleFolderPath) -MinimumVersion $Version -Force +Import-Module -Name $($ModuleFolderPath) -MinimumVersion $Version -Force Write-Verbose "Writing $($ModuleName) functions to help files in $($HelpPath)..." -#$mCommands = Get-Command -Module $ModuleName -#foreach ($m in $mCommands) { -# if ($null -ne $m) { -# Write-Verbose "Creating help file of function $($m.Name)..." -# Get-Help -name $m.Name -Full | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $HelpPath -ChildPath "$($m.Name).md") -# } -#} +$mCommands = Get-Command -Module $ModuleName +foreach ($m in $mCommands) { + if ($null -ne $m) { + Write-Verbose "Creating help file of function $($m.Name)..." + Get-Help -name $m.Name -Full | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $HelpPath -ChildPath "$($m.Name).md") + } +} -Write-Output "`n== Summery of PSScriptAnalyzer ==" -$ResultPS1 -$ResultPSDPSM +#Write-Output "`n== Summery of PSScriptAnalyzer ==" +#$ResultPS1 +#$ResultPSDPSM -if ($ResultPS1.Severity -contains "Warning" -or $ResultPSM.Severity -contains "Warning") { - Write-Error "PSAnalyzer severity did contain Warning, please fix this and run the RSModuleBuilder again. You can se the results from PSScriptAnalyzer below." - Break -} +#if ($ResultPS1.Severity -contains "Warning" -or $ResultPSM.Severity -contains "Warning") { +# Write-Error "PSAnalyzer severity did contain Warning, please fix this and run the RSModuleBuilder again. You can se the results from PSScriptAnalyzer below." +# Break +#} # Add so it check if it has any other flags in the analyzer then just inform about it. From a642466dfc9143b16e2d2ff27bc6e8b28d93bbbf Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Thu, 1 Dec 2022 08:04:32 +0100 Subject: [PATCH 05/11] update --- MaintainModule/MaintainModule.psd1 | 2 +- MaintainModule/MaintainModule.psm1 | 1812 ++++++++++++++++++++++++++++ RSModuleBuilder.ps1 | 46 +- 3 files changed, 1838 insertions(+), 22 deletions(-) diff --git a/MaintainModule/MaintainModule.psd1 b/MaintainModule/MaintainModule.psd1 index 32ddb48..bf2d025 100644 --- a/MaintainModule/MaintainModule.psd1 +++ b/MaintainModule/MaintainModule.psd1 @@ -84,7 +84,7 @@ # NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. - FunctionsToExport = "Uninstall-RSModule", "Update-RSModule" + FunctionsToExport = @() # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() diff --git a/MaintainModule/MaintainModule.psm1 b/MaintainModule/MaintainModule.psm1 index 193272f..bede72d 100644 --- a/MaintainModule/MaintainModule.psm1 +++ b/MaintainModule/MaintainModule.psm1 @@ -232,6 +232,1818 @@ Function Update-RSModule { } + # 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) { + + # Getting the latest installed version of the module + Write-Verbose "Collecting all installed version of $($m)..." + $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 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, 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)" + Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." + Update-Module -Name $($m) -Scope $Scope -Force + 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 ($GetAllInstalledVersions.Count -gt 1) { + Uninstall-RSModule -Module $m + } + } + else { + Write-Verbose "$($m) already has the newest version installed, no need to install anything!" + } + } + else { + # If the switch InstallMissing are set to true the modules will get installed if they are missing + if ($InstallMissing -eq $true) { + try { + Write-Output "$($m) are not installed, installing $($m)..." + Install-Module -Name $($m) -Scope $Scope -Force + Write-Output "$($m) has now been installed!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" + } + } + } + if ($EmptyModule -eq $false) { + if ($ImportModule -eq $true) { + # 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()) { + if ($m -in $ImportedModules.Name) { + Write-Verbose "$($m) are already imported!" + } + else { + try { + Write-Output "Importing $($m)..." + Import-Module -Name $m -Force + Write-Output "$($m) has been imported!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + } + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Uninstall-RSModule { + <# + .SYNOPSIS + Uninstall older versions of your modules in a easy way. + + .DESCRIPTION + This script let users uninstall older versions of the modules that are installed on the system. + + .PARAMETER Module + 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 + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI" + # This will uninstall all older versions of the module VMWare.PowerCLI system. + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" + # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + Twitter: https://twitter.com/rstolpes + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + Param( + [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] + [string]$Module + ) + + Write-Output "`n=== Starting to uninstall older versions of modules ===`n" + Write-Output "Please wait, this can take some time..." + + Write-Verbose "Caching all installed modules from the system..." + $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Update-RSModule { + <# + .SYNOPSIS + This module let you maintain your installed modules in a easy way. + + .DESCRIPTION + This function let you update all of your installed modules and also uninstall the old versions to keep things clean. + 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. + + .PARAMETER Module + Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated + + .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. + - Scope effect Install/update module function. + + .PARAMETER ImportModule + 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. + This only works if you have specified modules in the Module parameter + + .PARAMETER UninstallOldVersion + If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed + + .PARAMETER InstallMissing + 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. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser + # This will update the modules PowerCLI, ImportExcel for the current user + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion + # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing + # 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. + + .EXAMPLE + 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. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Twitter: @rstolpes + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + 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 = $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, + [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] + [switch]$UninstallOldVersion = $false, + [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] + [switch]$InstallMissing = $false + ) + + Write-Output "`n=== Starting module maintenance ===`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 + $EmptyModule = $false + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $EmptyModule = $true + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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. + Write-Verbose "Making sure that TLS 1.2 is used..." + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + + # Checking if PSGallery are set to trusted + Write-Verbose "Checking if PowerShell Gallery are set to trusted..." + if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { + try { + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted + Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" + } + + + # 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) { + + # Getting the latest installed version of the module + Write-Verbose "Collecting all installed version of $($m)..." + $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 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, 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)" + Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." + Update-Module -Name $($m) -Scope $Scope -Force + 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 ($GetAllInstalledVersions.Count -gt 1) { + Uninstall-RSModule -Module $m + } + } + else { + Write-Verbose "$($m) already has the newest version installed, no need to install anything!" + } + } + else { + # If the switch InstallMissing are set to true the modules will get installed if they are missing + if ($InstallMissing -eq $true) { + try { + Write-Output "$($m) are not installed, installing $($m)..." + Install-Module -Name $($m) -Scope $Scope -Force + Write-Output "$($m) has now been installed!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" + } + } + } + if ($EmptyModule -eq $false) { + if ($ImportModule -eq $true) { + # 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()) { + if ($m -in $ImportedModules.Name) { + Write-Verbose "$($m) are already imported!" + } + else { + try { + Write-Output "Importing $($m)..." + Import-Module -Name $m -Force + Write-Output "$($m) has been imported!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + } + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Uninstall-RSModule { + <# + .SYNOPSIS + Uninstall older versions of your modules in a easy way. + + .DESCRIPTION + This script let users uninstall older versions of the modules that are installed on the system. + + .PARAMETER Module + 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 + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI" + # This will uninstall all older versions of the module VMWare.PowerCLI system. + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" + # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + Twitter: https://twitter.com/rstolpes + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + Param( + [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] + [string]$Module + ) + + Write-Output "`n=== Starting to uninstall older versions of modules ===`n" + Write-Output "Please wait, this can take some time..." + + Write-Verbose "Caching all installed modules from the system..." + $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Update-RSModule { + <# + .SYNOPSIS + This module let you maintain your installed modules in a easy way. + + .DESCRIPTION + This function let you update all of your installed modules and also uninstall the old versions to keep things clean. + 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. + + .PARAMETER Module + Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated + + .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. + - Scope effect Install/update module function. + + .PARAMETER ImportModule + 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. + This only works if you have specified modules in the Module parameter + + .PARAMETER UninstallOldVersion + If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed + + .PARAMETER InstallMissing + 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. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser + # This will update the modules PowerCLI, ImportExcel for the current user + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion + # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing + # 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. + + .EXAMPLE + 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. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Twitter: @rstolpes + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + 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 = $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, + [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] + [switch]$UninstallOldVersion = $false, + [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] + [switch]$InstallMissing = $false + ) + + Write-Output "`n=== Starting module maintenance ===`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 + $EmptyModule = $false + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $EmptyModule = $true + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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. + Write-Verbose "Making sure that TLS 1.2 is used..." + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + + # Checking if PSGallery are set to trusted + Write-Verbose "Checking if PowerShell Gallery are set to trusted..." + if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { + try { + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted + Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" + } + + + # 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) { + + # Getting the latest installed version of the module + Write-Verbose "Collecting all installed version of $($m)..." + $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 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, 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)" + Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." + Update-Module -Name $($m) -Scope $Scope -Force + 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 ($GetAllInstalledVersions.Count -gt 1) { + Uninstall-RSModule -Module $m + } + } + else { + Write-Verbose "$($m) already has the newest version installed, no need to install anything!" + } + } + else { + # If the switch InstallMissing are set to true the modules will get installed if they are missing + if ($InstallMissing -eq $true) { + try { + Write-Output "$($m) are not installed, installing $($m)..." + Install-Module -Name $($m) -Scope $Scope -Force + Write-Output "$($m) has now been installed!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" + } + } + } + if ($EmptyModule -eq $false) { + if ($ImportModule -eq $true) { + # 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()) { + if ($m -in $ImportedModules.Name) { + Write-Verbose "$($m) are already imported!" + } + else { + try { + Write-Output "Importing $($m)..." + Import-Module -Name $m -Force + Write-Output "$($m) has been imported!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + } + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Uninstall-RSModule { + <# + .SYNOPSIS + Uninstall older versions of your modules in a easy way. + + .DESCRIPTION + This script let users uninstall older versions of the modules that are installed on the system. + + .PARAMETER Module + 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 + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI" + # This will uninstall all older versions of the module VMWare.PowerCLI system. + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" + # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + Twitter: https://twitter.com/rstolpes + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + Param( + [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] + [string]$Module + ) + + Write-Output "`n=== Starting to uninstall older versions of modules ===`n" + Write-Output "Please wait, this can take some time..." + + Write-Verbose "Caching all installed modules from the system..." + $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Update-RSModule { + <# + .SYNOPSIS + This module let you maintain your installed modules in a easy way. + + .DESCRIPTION + This function let you update all of your installed modules and also uninstall the old versions to keep things clean. + 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. + + .PARAMETER Module + Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated + + .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. + - Scope effect Install/update module function. + + .PARAMETER ImportModule + 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. + This only works if you have specified modules in the Module parameter + + .PARAMETER UninstallOldVersion + If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed + + .PARAMETER InstallMissing + 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. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser + # This will update the modules PowerCLI, ImportExcel for the current user + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion + # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing + # 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. + + .EXAMPLE + 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. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Twitter: @rstolpes + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + 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 = $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, + [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] + [switch]$UninstallOldVersion = $false, + [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] + [switch]$InstallMissing = $false + ) + + Write-Output "`n=== Starting module maintenance ===`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 + $EmptyModule = $false + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $EmptyModule = $true + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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. + Write-Verbose "Making sure that TLS 1.2 is used..." + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + + # Checking if PSGallery are set to trusted + Write-Verbose "Checking if PowerShell Gallery are set to trusted..." + if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { + try { + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted + Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" + } + + + # 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) { + + # Getting the latest installed version of the module + Write-Verbose "Collecting all installed version of $($m)..." + $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 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, 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)" + Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." + Update-Module -Name $($m) -Scope $Scope -Force + 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 ($GetAllInstalledVersions.Count -gt 1) { + Uninstall-RSModule -Module $m + } + } + else { + Write-Verbose "$($m) already has the newest version installed, no need to install anything!" + } + } + else { + # If the switch InstallMissing are set to true the modules will get installed if they are missing + if ($InstallMissing -eq $true) { + try { + Write-Output "$($m) are not installed, installing $($m)..." + Install-Module -Name $($m) -Scope $Scope -Force + Write-Output "$($m) has now been installed!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" + } + } + } + if ($EmptyModule -eq $false) { + if ($ImportModule -eq $true) { + # 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()) { + if ($m -in $ImportedModules.Name) { + Write-Verbose "$($m) are already imported!" + } + else { + try { + Write-Output "Importing $($m)..." + Import-Module -Name $m -Force + Write-Output "$($m) has been imported!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + } + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Uninstall-RSModule { + <# + .SYNOPSIS + Uninstall older versions of your modules in a easy way. + + .DESCRIPTION + This script let users uninstall older versions of the modules that are installed on the system. + + .PARAMETER Module + 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 + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI" + # This will uninstall all older versions of the module VMWare.PowerCLI system. + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" + # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + Twitter: https://twitter.com/rstolpes + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + Param( + [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] + [string]$Module + ) + + Write-Output "`n=== Starting to uninstall older versions of modules ===`n" + Write-Output "Please wait, this can take some time..." + + Write-Verbose "Caching all installed modules from the system..." + $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Update-RSModule { + <# + .SYNOPSIS + This module let you maintain your installed modules in a easy way. + + .DESCRIPTION + This function let you update all of your installed modules and also uninstall the old versions to keep things clean. + 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. + + .PARAMETER Module + Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated + + .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. + - Scope effect Install/update module function. + + .PARAMETER ImportModule + 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. + This only works if you have specified modules in the Module parameter + + .PARAMETER UninstallOldVersion + If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed + + .PARAMETER InstallMissing + 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. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser + # This will update the modules PowerCLI, ImportExcel for the current user + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion + # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing + # 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. + + .EXAMPLE + 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. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Twitter: @rstolpes + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + 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 = $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, + [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] + [switch]$UninstallOldVersion = $false, + [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] + [switch]$InstallMissing = $false + ) + + Write-Output "`n=== Starting module maintenance ===`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 + $EmptyModule = $false + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $EmptyModule = $true + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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. + Write-Verbose "Making sure that TLS 1.2 is used..." + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + + # Checking if PSGallery are set to trusted + Write-Verbose "Checking if PowerShell Gallery are set to trusted..." + if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { + try { + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted + Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" + } + + + # 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) { + + # Getting the latest installed version of the module + Write-Verbose "Collecting all installed version of $($m)..." + $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 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, 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)" + Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." + Update-Module -Name $($m) -Scope $Scope -Force + 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 ($GetAllInstalledVersions.Count -gt 1) { + Uninstall-RSModule -Module $m + } + } + else { + Write-Verbose "$($m) already has the newest version installed, no need to install anything!" + } + } + else { + # If the switch InstallMissing are set to true the modules will get installed if they are missing + if ($InstallMissing -eq $true) { + try { + Write-Output "$($m) are not installed, installing $($m)..." + Install-Module -Name $($m) -Scope $Scope -Force + Write-Output "$($m) has now been installed!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" + } + } + } + if ($EmptyModule -eq $false) { + if ($ImportModule -eq $true) { + # 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()) { + if ($m -in $ImportedModules.Name) { + Write-Verbose "$($m) are already imported!" + } + else { + try { + Write-Output "Importing $($m)..." + Import-Module -Name $m -Force + Write-Output "$($m) has been imported!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + } + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Uninstall-RSModule { + <# + .SYNOPSIS + Uninstall older versions of your modules in a easy way. + + .DESCRIPTION + This script let users uninstall older versions of the modules that are installed on the system. + + .PARAMETER Module + 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 + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI" + # This will uninstall all older versions of the module VMWare.PowerCLI system. + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" + # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + Twitter: https://twitter.com/rstolpes + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + Param( + [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] + [string]$Module + ) + + Write-Output "`n=== Starting to uninstall older versions of modules ===`n" + Write-Output "Please wait, this can take some time..." + + Write-Verbose "Caching all installed modules from the system..." + $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Update-RSModule { + <# + .SYNOPSIS + This module let you maintain your installed modules in a easy way. + + .DESCRIPTION + This function let you update all of your installed modules and also uninstall the old versions to keep things clean. + 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. + + .PARAMETER Module + Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated + + .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. + - Scope effect Install/update module function. + + .PARAMETER ImportModule + 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. + This only works if you have specified modules in the Module parameter + + .PARAMETER UninstallOldVersion + If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed + + .PARAMETER InstallMissing + 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. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser + # This will update the modules PowerCLI, ImportExcel for the current user + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion + # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing + # 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. + + .EXAMPLE + 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. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Twitter: @rstolpes + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + 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 = $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, + [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] + [switch]$UninstallOldVersion = $false, + [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] + [switch]$InstallMissing = $false + ) + + Write-Output "`n=== Starting module maintenance ===`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 + $EmptyModule = $false + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $EmptyModule = $true + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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. + Write-Verbose "Making sure that TLS 1.2 is used..." + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + + # Checking if PSGallery are set to trusted + Write-Verbose "Checking if PowerShell Gallery are set to trusted..." + if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { + try { + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted + Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" + } + + + # 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) { + + # Getting the latest installed version of the module + Write-Verbose "Collecting all installed version of $($m)..." + $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 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, 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)" + Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." + Update-Module -Name $($m) -Scope $Scope -Force + 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 ($GetAllInstalledVersions.Count -gt 1) { + Uninstall-RSModule -Module $m + } + } + else { + Write-Verbose "$($m) already has the newest version installed, no need to install anything!" + } + } + else { + # If the switch InstallMissing are set to true the modules will get installed if they are missing + if ($InstallMissing -eq $true) { + try { + Write-Output "$($m) are not installed, installing $($m)..." + Install-Module -Name $($m) -Scope $Scope -Force + Write-Output "$($m) has now been installed!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" + } + } + } + if ($EmptyModule -eq $false) { + if ($ImportModule -eq $true) { + # 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()) { + if ($m -in $ImportedModules.Name) { + Write-Verbose "$($m) are already imported!" + } + else { + try { + Write-Output "Importing $($m)..." + Import-Module -Name $m -Force + Write-Output "$($m) has been imported!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + } + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Uninstall-RSModule { + <# + .SYNOPSIS + Uninstall older versions of your modules in a easy way. + + .DESCRIPTION + This script let users uninstall older versions of the modules that are installed on the system. + + .PARAMETER Module + 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 + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI" + # This will uninstall all older versions of the module VMWare.PowerCLI system. + + .EXAMPLE + Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" + # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + Twitter: https://twitter.com/rstolpes + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + Param( + [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] + [string]$Module + ) + + Write-Output "`n=== Starting to uninstall older versions of modules ===`n" + Write-Output "Please wait, this can take some time..." + + Write-Verbose "Caching all installed modules from the system..." + $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." + } + } + Write-Output "`n---/// Script Finished! ///---" +} +Function Update-RSModule { + <# + .SYNOPSIS + This module let you maintain your installed modules in a easy way. + + .DESCRIPTION + This function let you update all of your installed modules and also uninstall the old versions to keep things clean. + 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. + + .PARAMETER Module + Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated + + .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. + - Scope effect Install/update module function. + + .PARAMETER ImportModule + 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. + This only works if you have specified modules in the Module parameter + + .PARAMETER UninstallOldVersion + If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed + + .PARAMETER InstallMissing + 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. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser + # This will update the modules PowerCLI, ImportExcel for the current user + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion + # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. + + .EXAMPLE + Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing + # 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. + + .EXAMPLE + 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. + + .LINK + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + .NOTES + Author: Robin Stolpe + Mail: robin@stolpe.io + Twitter: @rstolpes + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + #> + + [CmdletBinding(SupportsShouldProcess)] + 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 = $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, + [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] + [switch]$UninstallOldVersion = $false, + [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] + [switch]$InstallMissing = $false + ) + + Write-Output "`n=== Starting module maintenance ===`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 + $EmptyModule = $false + + # If Module parameter is empty populate it with all modules that are installed on the system + if ([string]::IsNullOrEmpty($Module)) { + Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." + $EmptyModule = $true + $Module = $InstalledModules.Name + } + else { + Write-Verbose "User has added modules to the Module parameter, splitting them" + $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. + Write-Verbose "Making sure that TLS 1.2 is used..." + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + + # Checking if PSGallery are set to trusted + Write-Verbose "Checking if PowerShell Gallery are set to trusted..." + if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { + try { + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted + Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" + } + catch { + Write-Error "$($PSItem.Exception)" + continue + } + } + else { + Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" + } + + # Start looping trough every module that are stored in the string Module foreach ($m in $Module.Split()) { Write-Verbose "Checks if $($m) are installed" diff --git a/RSModuleBuilder.ps1 b/RSModuleBuilder.ps1 index c78e94e..13f383a 100644 --- a/RSModuleBuilder.ps1 +++ b/RSModuleBuilder.ps1 @@ -54,18 +54,20 @@ $MigrateFunction = @( $(Get-ChildItem -Path $srcPublicFunctionPath/*.ps1 | Selec # Looping trough the .ps1 files and migrating them to one singel .psm1 file and saving it in the module folder Write-Verbose "Start to migrate all functions in to the .psm1 file and collecting the function names to add in the FunctionToExport in the .psd1 file" foreach ($function in $MigrateFunction.FullName) { - # Migrates all of the .ps1 files that are located in src/Function in to one .psm1 file saved in the module folder - $Results = [System.Management.Automation.Language.Parser]::ParseFile($function, [ref]$null, [ref]$null) - $Functions = $Results.EndBlock.Extent.Text - $Functions | Add-Content -Path $outPSMFile - - # Converting the function name to fit the .psd1 file for exporting - $function = $function -split "/" -replace ".ps1" | Select-Object -Last 1 - $function = """$($function)""," - [void]($function.trim()) - - # Collect the name of all .ps1 files so it can be added as functions in the psd1 file. - [void]($FunctionPSD.Add($function)) + if ($null -ne $function) { + # Migrates all of the .ps1 files that are located in src/Function in to one .psm1 file saved in the module folder + $Results = [System.Management.Automation.Language.Parser]::ParseFile($function, [ref]$null, [ref]$null) + $Functions = $Results.EndBlock.Extent.Text + $Functions | Add-Content -Path $outPSMFile + + # Converting the function name to fit the .psd1 file for exporting + $function = $function -split "/" -replace ".ps1" | Select-Object -Last 1 + $function = """$($function)""," + [void]($function.trim()) + + # Collect the name of all .ps1 files so it can be added as functions in the psd1 file. + [void]($FunctionPSD.Add($function)) + } } # if $MigrateFunction are not empty remove the last , from the $FunctionPSD ArrayList @@ -122,16 +124,18 @@ Set-Content -Path $outPSDFile -Value $PSDfileContent -Encoding utf8BOM -Force Write-Output "Running PSScriptAnalyzer on $($MigrateFunction.name)..." $ResultPS1 = foreach ($ps1 in $MigrateFunction.FullName) { - $ps1Name = $ps1 -split "/" -replace ".ps1" | Select-Object -Last 1 - Write-Verbose "Running PSScriptAnalyzer on $($ps1Name).ps1..." - $PSAnalyzerPS1 = Invoke-ScriptAnalyzer -Path $ps1 -ReportSummary - if ($null -ne $PSAnalyzerPS1) { - $PSAnalyzerPS1 | select-object * | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") - } - else { - Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") + if ($null -ne $ps1) { + $ps1Name = $ps1 -split "/" -replace ".ps1" | Select-Object -Last 1 + Write-Verbose "Running PSScriptAnalyzer on $($ps1Name).ps1..." + $PSAnalyzerPS1 = Invoke-ScriptAnalyzer -Path $ps1 -ReportSummary + if ($null -ne $PSAnalyzerPS1) { + $PSAnalyzerPS1 | select-object * | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") + } + else { + Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") + } + $PSAnalyzerPS1 } - $PSAnalyzerPS1 } Write-Output "Running PSScriptAnalyzer on $($outPSDFile) and $($outPSMFile)..." From 9061ae3bdbe2170d38d62320c19456b785554b1c Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Thu, 1 Dec 2022 08:13:37 +0100 Subject: [PATCH 06/11] update --- MaintainModule/MaintainModule.psd1 | 2 +- MaintainModule/MaintainModule.psm1 | 1812 ----------------- RSModuleBuilder.ps1 | 17 +- help/Uninstall-RSModule.md | 87 + help/Update-RSModule.md | 148 ++ ...tAnalyzer_Uninstall-RSModule_2022-12-01.md | 1 - 6 files changed, 245 insertions(+), 1822 deletions(-) create mode 100644 help/Uninstall-RSModule.md create mode 100644 help/Update-RSModule.md delete mode 100644 test/PSScriptAnalyzer_Uninstall-RSModule_2022-12-01.md diff --git a/MaintainModule/MaintainModule.psd1 b/MaintainModule/MaintainModule.psd1 index bf2d025..32ddb48 100644 --- a/MaintainModule/MaintainModule.psd1 +++ b/MaintainModule/MaintainModule.psd1 @@ -84,7 +84,7 @@ # NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. - FunctionsToExport = @() + FunctionsToExport = "Uninstall-RSModule", "Update-RSModule" # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() diff --git a/MaintainModule/MaintainModule.psm1 b/MaintainModule/MaintainModule.psm1 index bede72d..193272f 100644 --- a/MaintainModule/MaintainModule.psm1 +++ b/MaintainModule/MaintainModule.psm1 @@ -232,1818 +232,6 @@ Function Update-RSModule { } - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - - # 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) { - - # Getting the latest installed version of the module - Write-Verbose "Collecting all installed version of $($m)..." - $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 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, 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)" - Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." - Update-Module -Name $($m) -Scope $Scope -Force - 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 ($GetAllInstalledVersions.Count -gt 1) { - Uninstall-RSModule -Module $m - } - } - else { - Write-Verbose "$($m) already has the newest version installed, no need to install anything!" - } - } - else { - # If the switch InstallMissing are set to true the modules will get installed if they are missing - if ($InstallMissing -eq $true) { - try { - Write-Output "$($m) are not installed, installing $($m)..." - Install-Module -Name $($m) -Scope $Scope -Force - Write-Output "$($m) has now been installed!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Warning "$($m) module are not installed, and you have not chosen to install missing modules. Continuing without any actions!" - } - } - } - if ($EmptyModule -eq $false) { - if ($ImportModule -eq $true) { - # 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()) { - if ($m -in $ImportedModules.Name) { - Write-Verbose "$($m) are already imported!" - } - else { - try { - Write-Output "Importing $($m)..." - Import-Module -Name $m -Force - Write-Output "$($m) has been imported!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - } - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Uninstall-RSModule { - <# - .SYNOPSIS - Uninstall older versions of your modules in a easy way. - - .DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - .PARAMETER Module - 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 - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - .EXAMPLE - Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - Param( - [Parameter(Mandatory = $false, HelpMessage = "Enter the module or modules (separated with ,) you want to uninstall")] - [string]$Module - ) - - Write-Output "`n=== Starting to uninstall older versions of modules ===`n" - Write-Output "Please wait, this can take some time..." - - Write-Verbose "Caching all installed modules from the system..." - $InstalledModules = Get-InstalledModule | Select-Object Name, Version | Sort-Object Name - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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 { $_.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 = $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 $($GetAllInstalledVersions), no need to uninstall anything." - } - } - Write-Output "`n---/// Script Finished! ///---" -} -Function Update-RSModule { - <# - .SYNOPSIS - This module let you maintain your installed modules in a easy way. - - .DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - .PARAMETER Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - .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. - - Scope effect Install/update module function. - - .PARAMETER ImportModule - 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. - This only works if you have specified modules in the Module parameter - - .PARAMETER UninstallOldVersion - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - .PARAMETER InstallMissing - 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. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - .EXAMPLE - Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - .EXAMPLE - 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. - - .LINK - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - .NOTES - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - #> - - [CmdletBinding(SupportsShouldProcess)] - 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 = $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, - [Parameter(Mandatory = $false, HelpMessage = "Uninstalls all old versions of the modules")] - [switch]$UninstallOldVersion = $false, - [Parameter(Mandatory = $false, HelpMessage = "Install all of the modules that has been entered in module that are not installed on the system")] - [switch]$InstallMissing = $false - ) - - Write-Output "`n=== Starting module maintenance ===`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 - $EmptyModule = $false - - # If Module parameter is empty populate it with all modules that are installed on the system - if ([string]::IsNullOrEmpty($Module)) { - Write-Verbose "Parameter Module are empty populate it with all installed modules from the system..." - $EmptyModule = $true - $Module = $InstalledModules.Name - } - else { - Write-Verbose "User has added modules to the Module parameter, splitting them" - $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. - Write-Verbose "Making sure that TLS 1.2 is used..." - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - - # Checking if PSGallery are set to trusted - Write-Verbose "Checking if PowerShell Gallery are set to trusted..." - if ((Get-PSRepository -name PSGallery | Select-Object InstallationPolicy -ExpandProperty InstallationPolicy) -eq "Untrusted") { - try { - Set-PSRepository -Name PSGallery -InstallationPolicy Trusted - Write-Output "PowerShell Gallery was not set as trusted, it's now set as trusted!" - } - catch { - Write-Error "$($PSItem.Exception)" - continue - } - } - else { - Write-Verbose "PowerShell Gallery was already set to trusted, continuing!" - } - - # Start looping trough every module that are stored in the string Module foreach ($m in $Module.Split()) { Write-Verbose "Checks if $($m) are installed" diff --git a/RSModuleBuilder.ps1 b/RSModuleBuilder.ps1 index 13f383a..2f682bc 100644 --- a/RSModuleBuilder.ps1 +++ b/RSModuleBuilder.ps1 @@ -123,17 +123,18 @@ Write-Verbose "Setting the placeholders for $($outPSDFile)" Set-Content -Path $outPSDFile -Value $PSDfileContent -Encoding utf8BOM -Force Write-Output "Running PSScriptAnalyzer on $($MigrateFunction.name)..." -$ResultPS1 = foreach ($ps1 in $MigrateFunction.FullName) { +<#$ResultPS1 = foreach ($ps1 in $MigrateFunction.FullName) { + write-output "Är på fil $($ps1)" if ($null -ne $ps1) { $ps1Name = $ps1 -split "/" -replace ".ps1" | Select-Object -Last 1 Write-Verbose "Running PSScriptAnalyzer on $($ps1Name).ps1..." $PSAnalyzerPS1 = Invoke-ScriptAnalyzer -Path $ps1 -ReportSummary - if ($null -ne $PSAnalyzerPS1) { - $PSAnalyzerPS1 | select-object * | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") - } - else { - Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") - } + #if ($null -ne $PSAnalyzerPS1) { + # $PSAnalyzerPS1 | select-object * | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") + #} + #else { + # Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") + #} $PSAnalyzerPS1 } } @@ -151,7 +152,7 @@ $ResultPSDPSM = foreach ($file in $CheckPSA) { Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($psdPSMName)_$($TodaysDate).md") } $PSAnalyzer -} +}#> # Import the module and save the Get-Help files to the $HelpPath for the module, files get saved in .md format Write-Verbose "Importing $($ModuleName) to the session..." diff --git a/help/Uninstall-RSModule.md b/help/Uninstall-RSModule.md new file mode 100644 index 0000000..b10f261 --- /dev/null +++ b/help/Uninstall-RSModule.md @@ -0,0 +1,87 @@ + +NAME + Uninstall-RSModule + +SYNOPSIS + Uninstall older versions of your modules in a easy way. + + +SYNTAX + Uninstall-RSModule [[-Module] ] [-WhatIf] [-Confirm] [] + + +DESCRIPTION + This script let users uninstall older versions of the modules that are installed on the system. + + +PARAMETERS + -Module + 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 + + Required? false + Position? 1 + Default value + Accept pipeline input? false + Accept wildcard characters? false + + -WhatIf [] + + Required? false + Position? named + Default value + Accept pipeline input? false + Accept wildcard characters? false + + -Confirm [] + + Required? false + Position? named + Default value + Accept pipeline input? false + Accept wildcard characters? false + + + This cmdlet supports the common parameters: Verbose, Debug, + ErrorAction, ErrorVariable, WarningAction, WarningVariable, + OutBuffer, PipelineVariable, and OutVariable. For more information, see + about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216). + +INPUTS + +OUTPUTS + +NOTES + + + Author: Robin Stolpe + Mail: robin@stolpe.io + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + Twitter: https://twitter.com/rstolpes + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + + -------------------------- EXAMPLE 1 -------------------------- + + PS > Uninstall-RSModule -Module "VMWare.PowerCLI" + # This will uninstall all older versions of the module VMWare.PowerCLI system. + + + + + + + -------------------------- EXAMPLE 2 -------------------------- + + PS > Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" + # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. + + + + + + + +RELATED LINKS + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + diff --git a/help/Update-RSModule.md b/help/Update-RSModule.md new file mode 100644 index 0000000..8a2bc34 --- /dev/null +++ b/help/Update-RSModule.md @@ -0,0 +1,148 @@ + +NAME + Update-RSModule + +SYNOPSIS + This module let you maintain your installed modules in a easy way. + + +SYNTAX + Update-RSModule [[-Module] ] [[-Scope] ] [-ImportModule] [-UninstallOldVersion] [-InstallMissing] [-WhatIf] [-Confirm] [] + + +DESCRIPTION + This function let you update all of your installed modules and also uninstall the old versions to keep things clean. + 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. + + +PARAMETERS + -Module + Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated + + Required? false + Position? 1 + Default value + Accept pipeline input? false + Accept wildcard characters? false + + -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. + - Scope effect Install/update module function. + + Required? false + Position? 2 + Default value CurrentUser + Accept pipeline input? false + Accept wildcard characters? false + + -ImportModule [] + 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. + This only works if you have specified modules in the Module parameter + + Required? false + Position? named + Default value False + Accept pipeline input? false + Accept wildcard characters? false + + -UninstallOldVersion [] + If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed + + Required? false + Position? named + Default value False + Accept pipeline input? false + Accept wildcard characters? false + + -InstallMissing [] + 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. + + Required? false + Position? named + Default value False + Accept pipeline input? false + Accept wildcard characters? false + + -WhatIf [] + + Required? false + Position? named + Default value + Accept pipeline input? false + Accept wildcard characters? false + + -Confirm [] + + Required? false + Position? named + Default value + Accept pipeline input? false + Accept wildcard characters? false + + + This cmdlet supports the common parameters: Verbose, Debug, + ErrorAction, ErrorVariable, WarningAction, WarningVariable, + OutBuffer, PipelineVariable, and OutVariable. For more information, see + about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216). + +INPUTS + +OUTPUTS + +NOTES + + + Author: Robin Stolpe + Mail: robin@stolpe.io + Twitter: @rstolpes + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + + -------------------------- EXAMPLE 1 -------------------------- + + PS > Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser + # This will update the modules PowerCLI, ImportExcel for the current user + + + + + + + -------------------------- EXAMPLE 2 -------------------------- + + PS > Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion + # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. + + + + + + + -------------------------- EXAMPLE 3 -------------------------- + + PS > Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing + # 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. + + + + + + + -------------------------- EXAMPLE 4 -------------------------- + + PS > 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 + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + diff --git a/test/PSScriptAnalyzer_Uninstall-RSModule_2022-12-01.md b/test/PSScriptAnalyzer_Uninstall-RSModule_2022-12-01.md deleted file mode 100644 index b30bd6b..0000000 --- a/test/PSScriptAnalyzer_Uninstall-RSModule_2022-12-01.md +++ /dev/null @@ -1 +0,0 @@ -0 rule violations found. From efb6fc5ee8546de3e3e1fc233c82f1d78bf3b042 Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Thu, 1 Dec 2022 08:14:33 +0100 Subject: [PATCH 07/11] update --- RSModuleBuilder.ps1 | 16 ++-- help/Uninstall-RSModule.md | 87 ---------------------- help/Update-RSModule.md | 148 ------------------------------------- 3 files changed, 8 insertions(+), 243 deletions(-) delete mode 100644 help/Uninstall-RSModule.md delete mode 100644 help/Update-RSModule.md diff --git a/RSModuleBuilder.ps1 b/RSModuleBuilder.ps1 index 2f682bc..b545e10 100644 --- a/RSModuleBuilder.ps1 +++ b/RSModuleBuilder.ps1 @@ -123,22 +123,22 @@ Write-Verbose "Setting the placeholders for $($outPSDFile)" Set-Content -Path $outPSDFile -Value $PSDfileContent -Encoding utf8BOM -Force Write-Output "Running PSScriptAnalyzer on $($MigrateFunction.name)..." -<#$ResultPS1 = foreach ($ps1 in $MigrateFunction.FullName) { +$ResultPS1 = foreach ($ps1 in $MigrateFunction.FullName) { write-output "Är på fil $($ps1)" if ($null -ne $ps1) { $ps1Name = $ps1 -split "/" -replace ".ps1" | Select-Object -Last 1 Write-Verbose "Running PSScriptAnalyzer on $($ps1Name).ps1..." $PSAnalyzerPS1 = Invoke-ScriptAnalyzer -Path $ps1 -ReportSummary - #if ($null -ne $PSAnalyzerPS1) { - # $PSAnalyzerPS1 | select-object * | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") - #} - #else { - # Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") - #} + if ($null -ne $PSAnalyzerPS1) { + $PSAnalyzerPS1 | select-object * | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") + } + else { + Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") + } $PSAnalyzerPS1 } } - +<# Write-Output "Running PSScriptAnalyzer on $($outPSDFile) and $($outPSMFile)..." $CheckPSA = @($outPSDFile, $outPSMFile) $ResultPSDPSM = foreach ($file in $CheckPSA) { diff --git a/help/Uninstall-RSModule.md b/help/Uninstall-RSModule.md deleted file mode 100644 index b10f261..0000000 --- a/help/Uninstall-RSModule.md +++ /dev/null @@ -1,87 +0,0 @@ - -NAME - Uninstall-RSModule - -SYNOPSIS - Uninstall older versions of your modules in a easy way. - - -SYNTAX - Uninstall-RSModule [[-Module] ] [-WhatIf] [-Confirm] [] - - -DESCRIPTION - This script let users uninstall older versions of the modules that are installed on the system. - - -PARAMETERS - -Module - 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 - - Required? false - Position? 1 - Default value - Accept pipeline input? false - Accept wildcard characters? false - - -WhatIf [] - - Required? false - Position? named - Default value - Accept pipeline input? false - Accept wildcard characters? false - - -Confirm [] - - Required? false - Position? named - Default value - Accept pipeline input? false - Accept wildcard characters? false - - - This cmdlet supports the common parameters: Verbose, Debug, - ErrorAction, ErrorVariable, WarningAction, WarningVariable, - OutBuffer, PipelineVariable, and OutVariable. For more information, see - about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216). - -INPUTS - -OUTPUTS - -NOTES - - - Author: Robin Stolpe - Mail: robin@stolpe.io - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - Twitter: https://twitter.com/rstolpes - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - - -------------------------- EXAMPLE 1 -------------------------- - - PS > Uninstall-RSModule -Module "VMWare.PowerCLI" - # This will uninstall all older versions of the module VMWare.PowerCLI system. - - - - - - - -------------------------- EXAMPLE 2 -------------------------- - - PS > Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" - # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. - - - - - - - -RELATED LINKS - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - diff --git a/help/Update-RSModule.md b/help/Update-RSModule.md deleted file mode 100644 index 8a2bc34..0000000 --- a/help/Update-RSModule.md +++ /dev/null @@ -1,148 +0,0 @@ - -NAME - Update-RSModule - -SYNOPSIS - This module let you maintain your installed modules in a easy way. - - -SYNTAX - Update-RSModule [[-Module] ] [[-Scope] ] [-ImportModule] [-UninstallOldVersion] [-InstallMissing] [-WhatIf] [-Confirm] [] - - -DESCRIPTION - This function let you update all of your installed modules and also uninstall the old versions to keep things clean. - 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. - - -PARAMETERS - -Module - Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated - - Required? false - Position? 1 - Default value - Accept pipeline input? false - Accept wildcard characters? false - - -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. - - Scope effect Install/update module function. - - Required? false - Position? 2 - Default value CurrentUser - Accept pipeline input? false - Accept wildcard characters? false - - -ImportModule [] - 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. - This only works if you have specified modules in the Module parameter - - Required? false - Position? named - Default value False - Accept pipeline input? false - Accept wildcard characters? false - - -UninstallOldVersion [] - If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed - - Required? false - Position? named - Default value False - Accept pipeline input? false - Accept wildcard characters? false - - -InstallMissing [] - 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. - - Required? false - Position? named - Default value False - Accept pipeline input? false - Accept wildcard characters? false - - -WhatIf [] - - Required? false - Position? named - Default value - Accept pipeline input? false - Accept wildcard characters? false - - -Confirm [] - - Required? false - Position? named - Default value - Accept pipeline input? false - Accept wildcard characters? false - - - This cmdlet supports the common parameters: Verbose, Debug, - ErrorAction, ErrorVariable, WarningAction, WarningVariable, - OutBuffer, PipelineVariable, and OutVariable. For more information, see - about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216). - -INPUTS - -OUTPUTS - -NOTES - - - Author: Robin Stolpe - Mail: robin@stolpe.io - Twitter: @rstolpes - Website: https://stolpe.io - GitHub: https://github.com/rstolpe - PSGallery: https://www.powershellgallery.com/profiles/rstolpe - - -------------------------- EXAMPLE 1 -------------------------- - - PS > Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser - # This will update the modules PowerCLI, ImportExcel for the current user - - - - - - - -------------------------- EXAMPLE 2 -------------------------- - - PS > Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion - # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. - - - - - - - -------------------------- EXAMPLE 3 -------------------------- - - PS > Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing - # 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. - - - - - - - -------------------------- EXAMPLE 4 -------------------------- - - PS > 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 - https://github.com/rstolpe/MaintainModule/blob/main/README.md - - From a8871a65910cd887636b5eddd73d20a340a3b991 Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Thu, 1 Dec 2022 08:28:42 +0100 Subject: [PATCH 08/11] update --- RSModuleBuilder.ps1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/RSModuleBuilder.ps1 b/RSModuleBuilder.ps1 index b545e10..19a8e38 100644 --- a/RSModuleBuilder.ps1 +++ b/RSModuleBuilder.ps1 @@ -21,7 +21,7 @@ Import-Module -Name EasyModuleBuild -Force $Year = (Get-Date).Year $TodaysDate = Get-Date -Format "yyyy-MM-dd" $ModuleName = $(Get-Location) -split "/" | Select-Object -last 1 -$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition +$scriptPath = "/Users/rstolpe/Dev/GitHub/MaintainModule" #split-path -parent $MyInvocation.MyCommand.Definition $HelpPath = Join-Path -Path $scriptPath -ChildPath "help" $ModuleFolderPath = Join-Path -Path $scriptPath -ChildPath $ModuleName $srcPath = Join-Path -Path $scriptPath -ChildPath ".src" @@ -125,7 +125,8 @@ Set-Content -Path $outPSDFile -Value $PSDfileContent -Encoding utf8BOM -Force Write-Output "Running PSScriptAnalyzer on $($MigrateFunction.name)..." $ResultPS1 = foreach ($ps1 in $MigrateFunction.FullName) { write-output "Är på fil $($ps1)" - if ($null -ne $ps1) { + $PSAnalyzerPS1 = Invoke-ScriptAnalyzer -Path $ps1 -ReportSummary + <#if ($null -ne $ps1) { $ps1Name = $ps1 -split "/" -replace ".ps1" | Select-Object -Last 1 Write-Verbose "Running PSScriptAnalyzer on $($ps1Name).ps1..." $PSAnalyzerPS1 = Invoke-ScriptAnalyzer -Path $ps1 -ReportSummary @@ -136,7 +137,7 @@ $ResultPS1 = foreach ($ps1 in $MigrateFunction.FullName) { Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") } $PSAnalyzerPS1 - } + }#> } <# Write-Output "Running PSScriptAnalyzer on $($outPSDFile) and $($outPSMFile)..." From 3c5146f120028229d2049a4b71f94f5ea60710f1 Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Thu, 1 Dec 2022 08:32:37 +0100 Subject: [PATCH 09/11] update --- RSModuleBuilder.ps1 | 36 ++----------------- ...Analyzer_MaintainModule.psd1_2022-12-01.md | 1 + 2 files changed, 3 insertions(+), 34 deletions(-) create mode 100644 test/PSScriptAnalyzer_MaintainModule.psd1_2022-12-01.md diff --git a/RSModuleBuilder.ps1 b/RSModuleBuilder.ps1 index 19a8e38..ddb7347 100644 --- a/RSModuleBuilder.ps1 +++ b/RSModuleBuilder.ps1 @@ -122,38 +122,7 @@ else { Write-Verbose "Setting the placeholders for $($outPSDFile)" Set-Content -Path $outPSDFile -Value $PSDfileContent -Encoding utf8BOM -Force -Write-Output "Running PSScriptAnalyzer on $($MigrateFunction.name)..." -$ResultPS1 = foreach ($ps1 in $MigrateFunction.FullName) { - write-output "Är på fil $($ps1)" - $PSAnalyzerPS1 = Invoke-ScriptAnalyzer -Path $ps1 -ReportSummary - <#if ($null -ne $ps1) { - $ps1Name = $ps1 -split "/" -replace ".ps1" | Select-Object -Last 1 - Write-Verbose "Running PSScriptAnalyzer on $($ps1Name).ps1..." - $PSAnalyzerPS1 = Invoke-ScriptAnalyzer -Path $ps1 -ReportSummary - if ($null -ne $PSAnalyzerPS1) { - $PSAnalyzerPS1 | select-object * | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") - } - else { - Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($ps1Name)_$($TodaysDate).md") - } - $PSAnalyzerPS1 - }#> -} -<# -Write-Output "Running PSScriptAnalyzer on $($outPSDFile) and $($outPSMFile)..." -$CheckPSA = @($outPSDFile, $outPSMFile) -$ResultPSDPSM = foreach ($file in $CheckPSA) { - $psdPSMName = $file -split "/" | Select-Object -Last 1 - Write-Verbose "Running PSScriptAnalyzer on $($psdPSMName)..." - $PSAnalyzer = Invoke-ScriptAnalyzer -Path $file -ReportSummary - if ($null -ne $PSAnalyzer) { - $PSAnalyzer | select-object * | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($psdPSMName)_$($TodaysDate).md") - } - else { - Write-Output "0 rule violations found." | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $TestPath -ChildPath "PSScriptAnalyzer_$($psdPSMName)_$($TodaysDate).md") - } - $PSAnalyzer -}#> + # Import the module and save the Get-Help files to the $HelpPath for the module, files get saved in .md format Write-Verbose "Importing $($ModuleName) to the session..." @@ -168,8 +137,7 @@ foreach ($m in $mCommands) { } } -#Write-Output "`n== Summery of PSScriptAnalyzer ==" -#$ResultPS1 +Write-Output "`n== Summery of PSScriptAnalyzer ==" #$ResultPSDPSM #if ($ResultPS1.Severity -contains "Warning" -or $ResultPSM.Severity -contains "Warning") { diff --git a/test/PSScriptAnalyzer_MaintainModule.psd1_2022-12-01.md b/test/PSScriptAnalyzer_MaintainModule.psd1_2022-12-01.md new file mode 100644 index 0000000..b30bd6b --- /dev/null +++ b/test/PSScriptAnalyzer_MaintainModule.psd1_2022-12-01.md @@ -0,0 +1 @@ +0 rule violations found. From 93b471122243d4d79114075d7aca976fd83f26cc Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Thu, 1 Dec 2022 08:33:41 +0100 Subject: [PATCH 10/11] update --- RSModuleBuilder.ps1 | 2 +- help/Uninstall-RSModule.md | 87 ++++++++++ help/Update-RSModule.md | 148 ++++++++++++++++++ ...Analyzer_MaintainModule.psd1_2022-12-01.md | 1 - 4 files changed, 236 insertions(+), 2 deletions(-) create mode 100644 help/Uninstall-RSModule.md create mode 100644 help/Update-RSModule.md delete mode 100644 test/PSScriptAnalyzer_MaintainModule.psd1_2022-12-01.md diff --git a/RSModuleBuilder.ps1 b/RSModuleBuilder.ps1 index ddb7347..e2fd067 100644 --- a/RSModuleBuilder.ps1 +++ b/RSModuleBuilder.ps1 @@ -21,7 +21,7 @@ Import-Module -Name EasyModuleBuild -Force $Year = (Get-Date).Year $TodaysDate = Get-Date -Format "yyyy-MM-dd" $ModuleName = $(Get-Location) -split "/" | Select-Object -last 1 -$scriptPath = "/Users/rstolpe/Dev/GitHub/MaintainModule" #split-path -parent $MyInvocation.MyCommand.Definition +$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition $HelpPath = Join-Path -Path $scriptPath -ChildPath "help" $ModuleFolderPath = Join-Path -Path $scriptPath -ChildPath $ModuleName $srcPath = Join-Path -Path $scriptPath -ChildPath ".src" diff --git a/help/Uninstall-RSModule.md b/help/Uninstall-RSModule.md new file mode 100644 index 0000000..b10f261 --- /dev/null +++ b/help/Uninstall-RSModule.md @@ -0,0 +1,87 @@ + +NAME + Uninstall-RSModule + +SYNOPSIS + Uninstall older versions of your modules in a easy way. + + +SYNTAX + Uninstall-RSModule [[-Module] ] [-WhatIf] [-Confirm] [] + + +DESCRIPTION + This script let users uninstall older versions of the modules that are installed on the system. + + +PARAMETERS + -Module + 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 + + Required? false + Position? 1 + Default value + Accept pipeline input? false + Accept wildcard characters? false + + -WhatIf [] + + Required? false + Position? named + Default value + Accept pipeline input? false + Accept wildcard characters? false + + -Confirm [] + + Required? false + Position? named + Default value + Accept pipeline input? false + Accept wildcard characters? false + + + This cmdlet supports the common parameters: Verbose, Debug, + ErrorAction, ErrorVariable, WarningAction, WarningVariable, + OutBuffer, PipelineVariable, and OutVariable. For more information, see + about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216). + +INPUTS + +OUTPUTS + +NOTES + + + Author: Robin Stolpe + Mail: robin@stolpe.io + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + Twitter: https://twitter.com/rstolpes + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + + -------------------------- EXAMPLE 1 -------------------------- + + PS > Uninstall-RSModule -Module "VMWare.PowerCLI" + # This will uninstall all older versions of the module VMWare.PowerCLI system. + + + + + + + -------------------------- EXAMPLE 2 -------------------------- + + PS > Uninstall-RSModule -Module "VMWare.PowerCLI, ImportExcel" + # This will uninstall all older versions of VMWare.PowerCLI and ImportExcel from the system. + + + + + + + +RELATED LINKS + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + diff --git a/help/Update-RSModule.md b/help/Update-RSModule.md new file mode 100644 index 0000000..8a2bc34 --- /dev/null +++ b/help/Update-RSModule.md @@ -0,0 +1,148 @@ + +NAME + Update-RSModule + +SYNOPSIS + This module let you maintain your installed modules in a easy way. + + +SYNTAX + Update-RSModule [[-Module] ] [[-Scope] ] [-ImportModule] [-UninstallOldVersion] [-InstallMissing] [-WhatIf] [-Confirm] [] + + +DESCRIPTION + This function let you update all of your installed modules and also uninstall the old versions to keep things clean. + 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. + + +PARAMETERS + -Module + Specify the module or modules that you want to update, if you don't specify any module all installed modules are updated + + Required? false + Position? 1 + Default value + Accept pipeline input? false + Accept wildcard characters? false + + -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. + - Scope effect Install/update module function. + + Required? false + Position? 2 + Default value CurrentUser + Accept pipeline input? false + Accept wildcard characters? false + + -ImportModule [] + 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. + This only works if you have specified modules in the Module parameter + + Required? false + Position? named + Default value False + Accept pipeline input? false + Accept wildcard characters? false + + -UninstallOldVersion [] + If this switch are used all of the old versions of your modules will get uninstalled and only the current version will be installed + + Required? false + Position? named + Default value False + Accept pipeline input? false + Accept wildcard characters? false + + -InstallMissing [] + 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. + + Required? false + Position? named + Default value False + Accept pipeline input? false + Accept wildcard characters? false + + -WhatIf [] + + Required? false + Position? named + Default value + Accept pipeline input? false + Accept wildcard characters? false + + -Confirm [] + + Required? false + Position? named + Default value + Accept pipeline input? false + Accept wildcard characters? false + + + This cmdlet supports the common parameters: Verbose, Debug, + ErrorAction, ErrorVariable, WarningAction, WarningVariable, + OutBuffer, PipelineVariable, and OutVariable. For more information, see + about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216). + +INPUTS + +OUTPUTS + +NOTES + + + Author: Robin Stolpe + Mail: robin@stolpe.io + Twitter: @rstolpes + Website: https://stolpe.io + GitHub: https://github.com/rstolpe + PSGallery: https://www.powershellgallery.com/profiles/rstolpe + + -------------------------- EXAMPLE 1 -------------------------- + + PS > Update-RSModule -Module "PowerCLI, ImportExcel" -Scope CurrentUser + # This will update the modules PowerCLI, ImportExcel for the current user + + + + + + + -------------------------- EXAMPLE 2 -------------------------- + + PS > Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion + # This will update the modules PowerCLI, ImportExcel and delete all of the old versions that are installed of PowerCLI, ImportExcel. + + + + + + + -------------------------- EXAMPLE 3 -------------------------- + + PS > Update-RSModule -Module "PowerCLI, ImportExcel" -InstallMissing + # 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. + + + + + + + -------------------------- EXAMPLE 4 -------------------------- + + PS > 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 + https://github.com/rstolpe/MaintainModule/blob/main/README.md + + diff --git a/test/PSScriptAnalyzer_MaintainModule.psd1_2022-12-01.md b/test/PSScriptAnalyzer_MaintainModule.psd1_2022-12-01.md deleted file mode 100644 index b30bd6b..0000000 --- a/test/PSScriptAnalyzer_MaintainModule.psd1_2022-12-01.md +++ /dev/null @@ -1 +0,0 @@ -0 rule violations found. From 4cdd23d0e34f61da28b76144802abcbd6f9bdd9b Mon Sep 17 00:00:00 2001 From: Robin Stolpe Date: Thu, 1 Dec 2022 08:34:12 +0100 Subject: [PATCH 11/11] update --- RSModuleBuilder.ps1 | 157 -------------------------------------------- 1 file changed, 157 deletions(-) delete mode 100644 RSModuleBuilder.ps1 diff --git a/RSModuleBuilder.ps1 b/RSModuleBuilder.ps1 deleted file mode 100644 index e2fd067..0000000 --- a/RSModuleBuilder.ps1 +++ /dev/null @@ -1,157 +0,0 @@ -param ( - # Set this to true before releasing the module - [Parameter(Mandatory = $false, HelpMessage = "Enter the version number of this release")] - [string]$Version = "0.0.9", - # Fix this - [Parameter(Mandatory = $false, HelpMessage = ".")] - [string]$preRelease = "Alpha", - [Parameter(Mandatory = $false, HelpMessage = "Use this switch to publish this module on PSGallery")] - [bool]$Publish = $false, - # Validate so if $Publish is true this is needed - [Parameter(Mandatory = $false, HelpMessage = "Enter API key for PSGallery")] - [string]$apiKey -) - -#Requires -Modules PSScriptAnalyzer -Import-Module -Name EasyModuleBuild -Force - -# Creating ArrayList for use later in the script -[System.Collections.ArrayList]$FunctionPSD = @() - -$Year = (Get-Date).Year -$TodaysDate = Get-Date -Format "yyyy-MM-dd" -$ModuleName = $(Get-Location) -split "/" | Select-Object -last 1 -$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition -$HelpPath = Join-Path -Path $scriptPath -ChildPath "help" -$ModuleFolderPath = Join-Path -Path $scriptPath -ChildPath $ModuleName -$srcPath = Join-Path -Path $scriptPath -ChildPath ".src" -$srcPublicFunctionPath = Join-Path -Path $srcPath -ChildPath "public/function" -$srcPrivateFunctionPath = Join-Path -Path $srcPath -ChildPath "private/function" -$outPSMFile = Join-Path -Path $ModuleFolderPath -ChildPath "$($ModuleName).psm1" -$outPSDFile = Join-Path -Path $ModuleFolderPath -ChildPath "$($ModuleName).psd1" -$psdTemplate = Join-Path -Path $srcPath -ChildPath "$($ModuleName).psd1.source" -$psmLicensPath = Join-Path -Path $srcPath -ChildPath "License" -$TestPath = Join-Path -Path $scriptPath -ChildPath "test" - -Write-OutPut "`n== Building module $($ModuleName) ==`n" -Write-OutPut "Starting to build the module, please wait..." - -# Check so all the needed folders exists, if they don't they will get created. -Checkpoint-RSFolderFile -ModulePath $scriptPath -ModuleName $ModuleName -New $false - -# Deleting existing files that will get replaced by this script -Remove-RSContent -ModuleName $ModuleName -ScriptPath $scriptPath -ExistingModule - -# Adding the text from the gnu3_add_file_licens.source to the to of the .psm1 file for licensing of GNU v3 -# Let user choose between GNU 3 or MIT -$psmLicens = Get-Content -Path "$($psmLicensPath)/gnu3_add_file_licens.source" -ErrorAction SilentlyContinue -$psmLicens | Add-Content -Path $outPSMFile - -# Collecting all .ps1 files that are located in .src private/function and public/function folders -Write-Verbose "Collecting all .ps1 files from $($srcPublicFunctionPath) and $($srcPrivateFunctionPath)" -$MigrateFunction = @( $(Get-ChildItem -Path $srcPublicFunctionPath/*.ps1 | Select-Object FullName, Name -ErrorAction SilentlyContinue), $(Get-ChildItem -Path $srcPrivateFunctionPath/*.ps1 | Select-Object FullName, Name -ErrorAction SilentlyContinue) ) - -# Looping trough the .ps1 files and migrating them to one singel .psm1 file and saving it in the module folder -Write-Verbose "Start to migrate all functions in to the .psm1 file and collecting the function names to add in the FunctionToExport in the .psd1 file" -foreach ($function in $MigrateFunction.FullName) { - if ($null -ne $function) { - # Migrates all of the .ps1 files that are located in src/Function in to one .psm1 file saved in the module folder - $Results = [System.Management.Automation.Language.Parser]::ParseFile($function, [ref]$null, [ref]$null) - $Functions = $Results.EndBlock.Extent.Text - $Functions | Add-Content -Path $outPSMFile - - # Converting the function name to fit the .psd1 file for exporting - $function = $function -split "/" -replace ".ps1" | Select-Object -Last 1 - $function = """$($function)""," - [void]($function.trim()) - - # Collect the name of all .ps1 files so it can be added as functions in the psd1 file. - [void]($FunctionPSD.Add($function)) - } -} - -# if $MigrateFunction are not empty remove the last , from the $FunctionPSD ArrayList -if ($null -ne $MigrateFunction) { - # I know that I need to fix this one, but it's the best I can think of for now to remove the last , in the ArrayList - # Bug! If the module only contain one function the , after the name are not removed, need to remove that - $FunctionPSD = $FunctionPSD | ForEach-Object { - if ( $FunctionPSD.IndexOf($_) -eq ($FunctionPSD.count - 1) ) { - $_.replace(",", "") - } - else { - $_ - } - } -} - -# Change the placeholder in the $outPSMFile file -Write-Verbose "Getting the content from file $($outPSMFile)" -$PSMfileContent = Get-Content -Path $outPSMFile - -Write-Verbose "Replacing the placeholders in the $($outPSMFile) file" -$PSMfileContent = $PSMfileContent -replace '{{year}}', $year - -Write-Verbose "Setting the placeholders for $($outPSMFile)" -Set-Content -Path $outPSMFile -Value $PSMfileContent -Encoding utf8BOM -Force - -# Copy the .psd1.source file from the srcPath to the module folder and removing the .source ending -Write-Verbose "Copy the file $($psdTemplate) to $($outPSDFile)" -Copy-Item -Path $psdTemplate -Destination $outPSDFile -Force - -# Getting the content from the .psd1 file -Write-Verbose "Getting the content from file $($outPSDFile)" -$PSDfileContent = Get-Content -Path $outPSDFile - -# Can I do a loop here? I just might :) remember to check if the varible is empty or not -# Changing version, preReleaseTag and function in the .psd1 file -Write-Verbose "Replacing the placeholders in the $($outPSDFile) file" -$PSDfileContent = $PSDfileContent -replace '{{manifestDate}}', $TodaysDate -$PSDfileContent = $PSDfileContent -replace '{{moduleName}}', $ModuleName -$PSDfileContent = $PSDfileContent -replace '{{year}}', $Year -$PSDfileContent = $PSDfileContent -replace '{{version}}', $version -$PSDfileContent = $PSDfileContent -replace '{{preReleaseTag}}', $preReleaseTag - -# If $FunctionPSD are empty, then adding @() instead according to best practices for performance -if ($null -ne $FunctionPSD) { - $PSDfileContent = $PSDfileContent -replace '{{function}}', $FunctionPSD -} -else { - $PSDfileContent = $PSDfileContent -replace '{{function}}', '@()' -} - -Write-Verbose "Setting the placeholders for $($outPSDFile)" -Set-Content -Path $outPSDFile -Value $PSDfileContent -Encoding utf8BOM -Force - - - -# Import the module and save the Get-Help files to the $HelpPath for the module, files get saved in .md format -Write-Verbose "Importing $($ModuleName) to the session..." -Import-Module -Name $($ModuleFolderPath) -MinimumVersion $Version -Force - -Write-Verbose "Writing $($ModuleName) functions to help files in $($HelpPath)..." -$mCommands = Get-Command -Module $ModuleName -foreach ($m in $mCommands) { - if ($null -ne $m) { - Write-Verbose "Creating help file of function $($m.Name)..." - Get-Help -name $m.Name -Full | Out-File -Encoding UTF8BOM -FilePath $(Join-Path -Path $HelpPath -ChildPath "$($m.Name).md") - } -} - -Write-Output "`n== Summery of PSScriptAnalyzer ==" -#$ResultPSDPSM - -#if ($ResultPS1.Severity -contains "Warning" -or $ResultPSM.Severity -contains "Warning") { -# Write-Error "PSAnalyzer severity did contain Warning, please fix this and run the RSModuleBuilder again. You can se the results from PSScriptAnalyzer below." -# Break -#} - -# Add so it check if it has any other flags in the analyzer then just inform about it. - -if ($Publish -eq $true) { - Write-Verbose "Publishing $($ModuleName) version $($version) to PowerShell Gallery" - Publish-Module -Path $ModuleFolderPath -NuGetApiKey $apiKey -Force - Write-Output "---/// $($ModuleName) version $($Version) has now been built and published to PowerShell Gallery! ///---" -} -else { - Write-Output "---/// $($ModuleName) version $($Version) is now prepared for publishing! ///---" -} \ No newline at end of file