|
| 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