plugins/moduleupgrade/src/Upgrade-InstalledModule.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#Requires -Version 5
Function Global:Upgrade-InstalledModule {
    <#
    .SYNOPSIS
        A small wrapper for PowerShellGet to upgrade installed modules.
    .DESCRIPTION
        A small wrapper for PowerShellGet to upgrade installed modules.
    .PARAMETER ModuleName
        Show modules which would get upgraded.
    .PARAMETER Silent
        Do not show progress bar.
    .PARAMETER Force
        Force an upgrade without any confirmation prompts.
    .EXAMPLE
        PS> Upgrade-InstalledModule -Force
 
        Updates modules installed via PowerShellGet. Shows a progress bar.
    .NOTES
        Author: Zachary Loeber
        Site: http://www.the-little-things.net/
 
 
        Version History
        1.0.0 - Initial release
    #>


    [CmdletBinding( SupportsShouldProcess=$True, ConfirmImpact='Medium' )]
    Param (
        [Parameter(HelpMessage = 'Name of a module to check and remove old versions of.')]
        [string]$ModuleName = '*',
        [Parameter(HelpMessage = 'Force upgrade modules without confirmation.')]
        [Switch]$Force,
        [Parameter(HelpMessage = 'Do not write progress.')]
        [Switch]$Silent
    )
    Begin {
        try {
            Import-Module PowerShellGet
        }
        catch {
            Write-Warning 'Unable to load PowerShellGet. This script only works with PowerShell 5 and greater.'
            return
        }

        $YesToAll = $false
        $NoToAll = $false
    }

    Process {
        $Count = 0

        if (-not $Silent) {
            Write-Progress -Activity "Retrieving installed modules" -PercentComplete 1 -Status "Processing"
        }
        $InstalledModules = @(Get-InstalledModule $ModuleName)
        $TotalMods = $InstalledModules.Count
        ForEach ($Mod in (Get-InstalledModule $ModuleName)) {
            $count++
            if (-not $Silent) {
                $PercentComplete = [math]::Round((100*($count/$TotalMods)),0)
                Write-Progress -Activity "Processing Module $($Mod.Name)" -PercentComplete $PercentComplete -Status "Checking Module For Updates"
            }
            $OnlineModule = Find-Module $Mod.Name
            if ($OnlineModule.Version -gt $Mod.Version) {
                if ($pscmdlet.ShouldProcess("Upgraded module $($Mod.Name) from $($Mod.Version.ToString()) to $($OnlineModule.Version.ToString())", 
                "Upgrade module $($Mod.Name) from $($Mod.Version.ToString()) to $($OnlineModule.Version.ToString())?",
                "Upgrading module $($Mod.Name)")) {
                    if($Force -Or $PSCmdlet.ShouldContinue("Are you REALLY sure you want to upgrade '$($Mod.Name)'?",
                    "Upgrading module '$($Mod.Name)'",
                    [ref]$YesToAll,
                    [ref]$NotoAll)) {
                        if (-not $Silent) {
                            Write-Progress -Activity "Upgrading Module $($Mod.Name)" -PercentComplete $PercentComplete -Status "Upgrading Module"
                        }
                        Update-Module $Mod.Name -Force -Confirm:$false
                    }
                }
            }
        }
    }
}