Skip to content

Commit 3bbb7ec

Browse files
committed
New script (also in PSPreworkout). Get-ModulesWithUpdate
1 parent 8d674b4 commit 3bbb7ec

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

General/Get-ModulesWithUpdate.ps1

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
function Get-ModulesWithUpdate {
2+
<#
3+
.SYNOPSIS
4+
Get a list of installed PowerShell modules that have updates available in the PowerShell Gallery.
5+
6+
.DESCRIPTION
7+
This function retrieves a list of installed PowerShell modules and checks for updates available in the source repository.
8+
9+
.PARAMETER Name
10+
The module name or list of module names to check for updates. Wildcards are allowed, and all modules are checked by default.
11+
12+
.EXAMPLE
13+
Get-ModulesWithUpdate
14+
This command retrieves all installed PowerShell modules and checks for updates available in the PowerShell Gallery.
15+
16+
.NOTES
17+
To Do: Add support for checking other repositories.
18+
19+
#>
20+
[CmdletBinding()]
21+
[OutputType('PSPreworkout.ModuleInfo')]
22+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'Making it pretty.')]
23+
param(
24+
# List of modules to check for updates. This parameter is accepts wildcards and checks all modules by default.
25+
[Parameter(
26+
Position = 0,
27+
ValueFromPipeline,
28+
ValueFromPipelineByPropertyName,
29+
HelpMessage = 'Enter a module name or names. Wildcards are allowed.'
30+
)]
31+
[ValidateNotNullOrEmpty()]
32+
[SupportsWildcards()]
33+
[System.Collections.Generic.List[string]] $Name = @('*')
34+
)
35+
36+
begin {
37+
# Initialize a list to hold modules with updates.
38+
[System.Collections.Generic.List[System.Object]] $ModulesWithUpdates = @()
39+
} # end begin block
40+
41+
process {
42+
# Get installed modules.
43+
Write-Host -ForegroundColor Cyan "Getting installed modules ($($Name -join ','))..."
44+
try {
45+
[System.Collections.Generic.List[System.Object]] $Modules = Get-InstalledModule -Name $Name
46+
} catch {
47+
throw $_
48+
}
49+
50+
# End the script if no modules were found.
51+
if (-not $Modules -or $Modules.Count -eq 0) {
52+
Write-Warning 'No matching modules were found.'
53+
return
54+
} else {
55+
Write-Host "Found $($Modules.Count) installed modules.`n"
56+
}
57+
58+
Write-Host 'Checking the repository for newer versions of the modules...' -ForegroundColor Cyan
59+
foreach ($Module in $Modules) {
60+
61+
Write-Verbose "$($Module.Name) $($Module.Version)"
62+
63+
# Use $true for the AllowPrerelease argument if the module version string contains 'beta', 'prerelease', 'preview', or 'rc'.
64+
$PreRelease = ( $Module.Version -match 'beta|prerelease|preview|rc' )
65+
66+
try {
67+
# Get the latest online version. Only allow pre-release versions if a pre-release version is already installed.
68+
$OnlineModule = Find-Module -Name $Module.Name -AllowPrerelease:$PreRelease
69+
# The Get-PSResource cmdlet provides Repository name and can be optimized to check other repositories if needed.
70+
# If a newer version is available, create a custom object with PSPreworkout.ModuleInfo type.
71+
# Treat the installed version as an array in case multiple versions are installed.
72+
if ( ($OnlineModule.Version -as [version]) -gt @(($Module.Version))[0] ) {
73+
Write-Verbose "$($Module.Name) $($Module.Version) --> $($OnlineModule.Version) 🆕"
74+
75+
# Create a custom object with PSPreworkout.ModuleInfo type
76+
$ModuleInfo = [PSCustomObject]@{
77+
PSTypeName = 'PSPreworkout.ModuleInfo'
78+
Name = $Module.Name
79+
Version = $Module.Version
80+
Repository = $Module.Repository
81+
Description = $Module.Description
82+
Author = $Module.Author
83+
CompanyName = $Module.CompanyName
84+
Copyright = $Module.Copyright
85+
PublishedDate = $Module.PublishedDate
86+
InstalledDate = $Module.InstalledDate
87+
UpdateAvailable = $true
88+
OnlineVersion = $OnlineModule.Version
89+
ReleaseNotes = $OnlineModule.ReleaseNotes
90+
}
91+
92+
# Add the module to the list of modules with updates.
93+
$ModulesWithUpdates.Add($ModuleInfo)
94+
}
95+
} catch {
96+
# Show a warning if the module is not found in the online repository.
97+
Write-Warning "Module $($Module.Name) was not found in the online repository. $_"
98+
}
99+
}
100+
101+
if (-not $ModulesWithUpdates -or $ModulesWithUpdates.Count -eq 0) {
102+
Write-Information 'No module updates found in the online repository.'
103+
return
104+
} else {
105+
# Return the list of modules with updates to the host or the pipeline.
106+
Write-Host "Found $($ModulesWithUpdates.Count) modules with updates available." -ForegroundColor Yellow
107+
$ModulesWithUpdates
108+
}
109+
} # end process block
110+
}
111+

0 commit comments

Comments
 (0)