Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [cmdletbinding()]
- param(
- [Switch]
- $UninstallOldVersions
- )
- # Load required modules
- $requiredModules = @(
- 'PowerShellGet'
- )
- $modCheckFail = $false
- foreach ($modName in $requiredModules) {
- if (!(Get-Module -Name $modName)) {
- if (!(Get-Module -Name $modName -ListAvailable)) {
- Write-Warning "'$modName' module required. Please install prior to running"
- $modCheckFail = $true
- }
- else {
- Import-Module -Name $modName
- }
- }
- }
- if ($modCheckFail) { Return }
- # Validate required commands (failure may indicate too old a version of required modules)
- $requiredCommands = @(
- 'Find-Module',
- 'Get-InstalledModule',
- 'Install-Module',
- 'Uninstall-Module'
- )
- $cmdCheckFail = $false
- foreach ($cmdName in $requiredCommands) {
- if (!(Get-Command -Name $cmdName -ErrorAction Ignore)) {
- Write-Warning "Required command '$cmdName' not found after loading required modules"
- $cmdCheckFail = $true
- }
- }
- if ($cmdCheckFail) {
- Write-Warning "Check you have the latest versions of all required modules"
- foreach ($modName in $requiredModules) {
- Write-Warning "$modName"
- }
- Return
- }
- # Dependent functions
- function Test-VersionString {
- [cmdletbinding()]
- param(
- [parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [String]
- $VersionString
- )
- if ($VersionString -match '^[\d]+\.[\d]+\.[\d]+$') { $true }
- if ($VersionString -match '^[\d]+\.[\d]+\.[\d]+\.[\d]+$') { $true }
- else { $false }
- }
- function Test-ModuleVersionPresent {
- [cmdletbinding()]
- param(
- [parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [String]
- $ModuleName,
- [parameter(Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [String]
- $Version
- )
- $list = [System.Collections.Generic.List[String]]::new()
- $moduleList = Get-Module -Name $ModuleName -ListAvailable
- foreach ($module in $moduleList) {
- $list.Add($module.Version.ToString())
- }
- if ($Version -in $list) { $true }
- else { $false }
- }
- # MAIN
- $installedModules = Get-InstalledModule
- $x = 1
- foreach ($installedModule in $installedModules) {
- Write-Progress -Activity 'Processing modules' -Status "($x/$($installedModules.Count)) - '$($installedModule.Name)'" -PercentComplete ($x/$installedModules.Count*100)
- $latestModule = Find-Module -Name $installedModule.Name -Repository $installedModule.Repository
- if (!$latestModule) {
- Write-Warning "Unable to find latest module '$($installedModule.Name)' at repository '$($installedModule.Repository)'. Skipping module."
- Continue
- }
- if (!(Test-VersionString -VersionString $installedModule.Version) -or !(Test-VersionString -VersionString $latestModule.Version)) {
- Write-Warning "Unhandled version string syntax in source or latest module. Version must 4 segments of digits only. Skipping module."
- Write-Warning "Source: '$($installedModule.Name)' - '$($installedModule.Version)' -- Latest: '$($latestModule.Name)' - '$($latestModule.Version)'"
- Continue
- }
- else {
- if ([System.Version]$installedModule.Version -lt [System.Version]$latestModule.Version) {
- Write-Output "'$($installedModule.Name)' - Higher version '$($latestModule.Version)' found (local version '$($installedModule.Version)'). Installing."
- Install-Module -Name $installedModule.Name -Repository $installedModule.Repository -RequiredVersion $latestModule.Version -Scope CurrentUser -Force
- if (!(Test-ModuleVersionPresent -ModuleName $installedModule.Name -Version $latestModule.Version)) {
- Write-Warning "'$($installedModule.Name)' - Latest version not found after installation attempt."
- if ($UninstallOldVersions) { Write-Warning "Uninstall of old versions will not proceed" }
- Continue
- }
- else {
- if ($UninstallOldVersions) {
- Write-Output "'$($installedModule.Name)' - Uninstalling old versions"
- $installedModuleMatches = Get-Module -Name $installedModule.Name -ListAvailable
- foreach ($moduleMatch in $installedModuleMatches) {
- if ($moduleMatch.Version.ToString() -eq $latestModule.Version) { Continue }
- else {
- Uninstall-Module -Name $moduleMatch.Name -RequiredVersion $moduleMatch.Version -Force
- }
- }
- }
- }
- }
- elseif ([System.Version]$installedModule.Version -eq [System.Version]$latestModule.Version) {
- Write-Output "'$($installedModule.Name)' - Same version '$($latestModule.Version)' found (local version '$($installedModule.Version)'). Skipping install."
- if ($UninstallOldVersions) {
- Write-Output "'$($installedModule.Name)' - Uninstalling old versions"
- $installedModuleMatches = Get-Module -Name $installedModule.Name -ListAvailable
- foreach ($moduleMatch in $installedModuleMatches) {
- if ($moduleMatch.Version.ToString() -eq $latestModule.Version) { Continue }
- else {
- Uninstall-Module -Name $moduleMatch.Name -RequiredVersion $moduleMatch.Version -Force
- }
- }
- }
- }
- elseif ([System.Version]$installedModule.Version -gt [System.Version]$latestModule.Version) {
- Write-Warning "'$($installedModule.Name)' - Local version '$($installedModule.Version)' found as higher (repository version '$($latestModule.Version)'). No action taken due to anomaly."
- Continue
- }
- }
- $x++
- }
Advertisement
Add Comment
Please, Sign In to add comment