Skip to content

Commit b943c96

Browse files
authored
feat: add new function to list outdated packages (#15)
* feat: add new function to list outdated packages * Update with review feedback * Udpate to not write empty files
1 parent 005d80b commit b943c96

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"ipify",
77
"ULNS",
88
"Wifi",
9+
"Winget",
910
"WLAN"
1011
]
1112
}

PSF.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
# NestedModules = @()
7070

7171
# 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.
72-
FunctionsToExport = @('Get-oAuthToken', 'Get-RandomPassword', 'Get-WiFiKnownPasswords', 'Get-PublicIP')
72+
FunctionsToExport = @('Get-oAuthToken', 'Get-RandomPassword', 'Get-WiFiKnownPasswords', 'Get-PublicIP', 'Get-Updates')
7373

7474
# 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.
7575
CmdletsToExport = @()

PSF.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
. $PSScriptRoot\functions\Get-RandomPassword.ps1
55
. $PSScriptRoot\functions\Get-WiFiKnownPasswords.ps1
66
. $PSScriptRoot\functions\Get-PublicIP.ps1
7+
. $PSScriptRoot\functions\Get-Updates.ps1

functions/Get-Updates.ps1

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
function Get-Updates {
2+
<#
3+
.SYNOPSIS
4+
Checks for software/module updates
5+
.DESCRIPTION
6+
Supports checking Chocolatey/WinGet/PowerShell module updates.
7+
.EXAMPLE
8+
C:\PS> .\Get-Updates.ps1
9+
Checks updates for all known installed package managers.
10+
.OUTPUTS
11+
Creates outdated package files in the temp directory ($env:Temp).
12+
Displays the outdated packages on the console.
13+
.NOTES
14+
Version 1.0.0
15+
#>
16+
[CmdletBinding()]
17+
param ()
18+
19+
$ErrorActionPreference = 'Stop'
20+
21+
# Check for supported applications.
22+
$Chocolatey = ($null -ne (Get-Command choco -ErrorAction SilentlyContinue))
23+
$WinGet = ($null -ne (Get-Command winget -ErrorAction SilentlyContinue))
24+
#Define the output file paths
25+
$PathChoco = "$env:temp\choco-outdated.json"
26+
$PathPS = "$env:temp\powershell-outdated.json"
27+
$PathWinGet = "$env:temp\winget-outdated.json"
28+
29+
# Check PowerShell module updates.
30+
Write-Host 'Checking PowerShell module updates...'
31+
if (Test-Path $PathPS -PathType Leaf) {
32+
# Remove the PS update file if it exists.
33+
Remove-Item -Path $PathPS -Force | Out-Null
34+
}
35+
$Modules = Get-Module -ListAvailable | Where-Object { `
36+
$_.RepositorySourceLocation -and `
37+
$_.Name -notlike 'Az.*' -and `
38+
$_.Name -notlike 'AzureRM.*' -and `
39+
$_.Name -notlike 'Microsoft.Graph.*' `
40+
} | Group-Object Name | Select-Object Name, @{n = 'Version'; e = { $_.Group[0].Version } }
41+
$ModuleUpdates = foreach ($Module in $Modules) {
42+
try {
43+
$Available = Find-Module $Module.Name
44+
if ([version]($Available).Version -gt [version]$Module.Version) {
45+
[PSCustomObject]@{
46+
Module = $Module.Name
47+
CurrentVersion = $Module.Version.ToString()
48+
AvailableVersion = $Available.Version
49+
}
50+
}
51+
} catch {
52+
Write-Warning ('Failed to find details for module "{0}": {1}' -f $Module.Name, $_.Exception.Message)
53+
}
54+
}
55+
if ($ModuleUpdates) {
56+
$ModuleUpdates | ConvertTo-Json | Out-File -FilePath $PathPS -Encoding utf8 -Force
57+
}
58+
59+
# If Chocolatey is installed, check its updates.
60+
if ($Chocolatey) {
61+
Write-Host 'Checking Chocolatey package updates...'
62+
if (Test-Path $PathChoco -PathType Leaf) {
63+
# Remove the Choco update file if it exists.
64+
Remove-Item -Path $PathChoco -Force | Out-Null
65+
}
66+
$ChocoOutput = & Choco outdated -r
67+
$ChocoUpdates = @(
68+
foreach ($line in $ChocoOutput) {
69+
if ($line -match '(.*)\|(.*)\|(.*)\|(.*)') {
70+
[PSCustomObject]@{
71+
Package = $Matches[1]
72+
CurrentVersion = $Matches[2]
73+
AvailableVersion = $Matches[3]
74+
}
75+
}
76+
}
77+
)
78+
if ($ChocoUpdates) {
79+
$ChocoUpdates | ConvertTo-Json | Out-File -FilePath $PathChoco -Encoding utf8 -Force
80+
}
81+
}
82+
83+
# If WinGet is installed, check its updates.
84+
if ($WinGet) {
85+
Write-Host 'Checking WinGet package updates...'
86+
if (Test-Path $PathWinGet -PathType Leaf) {
87+
# Remove the WinGet update file if it exists.
88+
Remove-Item -Path $PathWinGet -Force | Out-Null
89+
}
90+
$WinGetOutput = & winget upgrade
91+
$WinGetUpdates = @(
92+
foreach ($line in ($WinGetOutput | Select-Object -Skip 1)) {
93+
if ($line -match '^(.*?) +([\w\d\.\+-_]+) +([\d\.\+-_]*?|Unknown) +([\d\.-]*|Unknown) +winget$') {
94+
[PSCustomObject]@{
95+
PackageId = $Matches[2]
96+
CurrentVersion = $Matches[3]
97+
AvailableVersion = $Matches[4]
98+
}
99+
}
100+
}
101+
)
102+
$WinGetUpdates = $WinGetUpdates | Where-Object { $_.CurrentVersion -ne 'Unknown' } | Sort-Object PackageId
103+
if ($WinGetUpdates) {
104+
$WinGetUpdates | ConvertTo-Json | Out-File -FilePath $PathWinGet -Encoding utf8 -Force
105+
}
106+
}
107+
108+
# If Chocolatey is installed, list the available updates.
109+
if ($Chocolatey) {
110+
if (Test-Path $PathChoco -PathType Leaf) {
111+
$Output = Get-Content $PathChoco | ConvertFrom-Json
112+
if ($Output) {
113+
Write-Host ('{0} Chocolatey package updates are available!' -f ($Output | Measure-Object).Count) -ForegroundColor Green
114+
Write-Host ($Output | Format-Table | Out-String) -ForegroundColor Green
115+
}
116+
} else {
117+
Write-Host 'Chocolatey packages are up to date' -ForegroundColor Green
118+
}
119+
}
120+
121+
# List the PS module available updates.
122+
if (Test-Path $PathPS -PathType Leaf) {
123+
$Output = Get-Content $PathPS | ConvertFrom-Json
124+
if ($Output) {
125+
Write-Host ('{0} PowerShell module updates are available!' -f ($Output | Measure-Object).Count) -ForegroundColor Green
126+
Write-Host ($Output | Format-Table | Out-String) -ForegroundColor Green
127+
}
128+
} else {
129+
Write-Host 'PowerShell modules are up to date' -ForegroundColor Green
130+
}
131+
132+
# If Winget is installed, list the available updates.
133+
if ($WinGet) {
134+
if (Test-Path $PathWinGet -PathType Leaf) {
135+
$Output = Get-Content $PathWinGet | ConvertFrom-Json
136+
if ($Output) {
137+
Write-Host ('{0} Winget package updates are available!' -f ($Output | Measure-Object).Count) -ForegroundColor Green
138+
Write-Host ($Output | Format-Table | Out-String) -ForegroundColor Green
139+
}
140+
} else {
141+
Write-Host 'Winget packages are up to date' -ForegroundColor Green
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)