Get-PackageUpdates.psm1

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
82
83
84
85
[cmdletbinding()]
Param()

<#
Print update info using $env:ChocoUpdatesCount and $env:PSUpdatesCount and the following logic to your $profile:

Import-Module Get-PackageUpdates
Write-PackageUpdates

Open Explorer under shell:startup and create a shortcut that includes the following line:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -Command "Save-PackageUpdates"
#>


function Write-PackageUpdate {
    param(
        [int]
        $count,
        [string]
        $environment
    )

    if ($count -gt 0) {
        Write-Host "$count $environment packages can be updated"
    }
}

function Write-PackageUpdates {
    if (Test-Path "~\.updateinfo") {
        $updateInfo = Get-Content '~\.updateInfo' | ConvertFrom-Json
        if ($updateInfo) {
            Write-PackageUpdate -count $updateInfo.ChocoUpdatesCount -environment 'Chocolatey'
            Write-PackageUpdate -count $updateInfo.PSUpdatesCount -environment 'PowerShell'
        }
    }    
}

function Get-PackageUpdates {
    # Check for Chocolatey updates
    $choco_count = 0
    $ps_count = 0
    $choco_outdated = choco outdated
    if ("$choco_outdated" -match '([0-9]*) package\(s\)') {
        $choco_count = $Matches[1]
    }

    # Check for PSGallery updates
    $groupedModules = Get-Module -ListAvailable | Where-Object {$_.repositorysourcelocation} | Group-Object Name
    # group and keep highest version
    $gallery = @()
    foreach ($moduleGroup in $groupedModules) {
        $moduleToKeep = $moduleGroup.Group[0]
        foreach ($module in $moduleGroup.Group) {
            if ($module.version -gt $moduleToKeep.version) {
                $moduleToKeep = $module
            } 
        }
        Write-Host "$($moduleToKeep.Name) $($moduleToKeep.Version)"
        $gallery += $moduleToKeep 
    }    
    foreach ($module in $gallery) {
        #find the current version in the gallery
        Try {
            $online = Find-Module -Name $module.name -Repository PSGallery -ErrorAction Stop
            #compare versions
            if ($online.version -gt $module.version) {
                Write-Host "$($module.name): Online - $($online.version), local - $($module.version)"
                $ps_count++
            } 
        }
        Catch {
            # Who cares, not found
        }        
    } #foreach

    return New-Object -TypeName PSObject -Property @{ 
        ChocoUpdatesCount = $choco_count 
        PSUpdatesCount = $ps_count 
    }
}

function Save-PackageUpdates {
    (Get-PackageUpdates) | ConvertTo-Json | Out-File '~\.updateInfo'
}