forked from bcdady/MyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind-NewModule.ps1
66 lines (55 loc) · 2.92 KB
/
Find-NewModule.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
#requires -Version 3 -modules PowerShellGet
#===============================================================================
# NAME : Find-NewModule.ps1
# LANGUAGE : Windows PowerShell
# AUTHOR : Bryan Dady
# DATE : 06/06/2016
# COMMENT : Thanks to @jsnover : https://twitter.com/jsnover/status/739869719532969984
# Here's a 1-liner to find all the modules that have been updated this week:
# $n=[datetime]::Now;fimo|?{($n-$_.PublishedDate).Days-le7}|ogv
# $n = [datetime]::Now;fimo|Where-Object{($n-$_.PublishedDate).Days -le 1} | Format-List -Property Name,Description
#===============================================================================
[CmdletBinding()]
param ()
$age = [int]10
$today = [datetime]::Now
Write-Verbose -Message 'Defining function Find-NewModule' -Verbose
function Find-NewModule
{
[cmdletbinding()]
Param()
Write-Verbose -Message 'Started function Find-ModuleUpdate'
Write-Verbose -Message "Retrieving modules from powershellgallery.com, updated in the last $age days"
# retrieve current gallery listing
Try {
$PSGallery = Find-Module -Repository PSGallery -ErrorAction Stop | Where-Object -FilterScript {($today-$_.PublishedDate).Days -le $age}
}
Catch {
Write-Warning -Message 'Fatal error attempting to retrieve modules from powershellgallery.com'
throw 'Failed to retrieve modules info from powershellgallery.com'
}
Write-Verbose -Message "Found $($PSGallery.length) new modules"
Write-Verbose 'Checking for existing, installed modules'
$InstalledModules = Get-InstalledModule; # Get-Module -ListAvailable
Write-Verbose -Message "Got $($InstalledModules.length) installed modules"
foreach ($module in $PSGallery) {
Write-Verbose -Message "Checking for local version of $($module.name)"
# compare matching object from online PSGallery, with list of local modules
$foundLocal = $null
if (-not ($module -in $InstalledModules)) {
$module | Select-Object -Property Name,Version,PublishedDate,CompanyName,Author,Description
$foundLocal = get-module -ListAvailable -Name $module.name | Select-Object -Property Name,Version,ModuleBase -ErrorAction SilentlyContinue
$foundLocal
if ($null -ne $foundLocal) {
Write-Output -InputObject 'Found local module that can be replaced / upgraded from powershellgallery.com. Remove ''legacy'' module?'
Remove-Module -Name $module.name -Confirm -ErrorAction SilentlyContinue
}
# prompt to install this new module?
}
else {
$LocalModule = $InstalledModules | where{ $_.Name -eq $($module.name)}
Write-Verbose -Message "Skipping already installed module $($LocalModule.name) ($($LocalModule.name), $($LocalModule.PublishedDate))"
}
} # end foreach
Write-Verbose -Message 'Find-NewModule complete'
}