|
1 | | -function Test-IsElevated { |
2 | | - return (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) |
3 | | -} |
4 | | - |
5 | 1 | function Update-Modules { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Update all installed modules |
| 5 | + .EXAMPLE |
| 6 | + Update-Modules -Scope CurrentUser |
| 7 | + .PARAMETER AllowPrerelease |
| 8 | + Updating module to latest prerelease version. |
| 9 | + .PARAMETER WhatIf |
| 10 | + Run function without actually executing it. |
| 11 | + .PARAMETER Name |
| 12 | + Name of the module you want to update. |
| 13 | + .PARAMETER Scope |
| 14 | + The scope apply when updating modules. |
| 15 | + #> |
6 | 16 | [CmdletBinding()] |
7 | 17 | param ( |
8 | | - [Alias('p')][switch]$AllowPrerelease, |
9 | | - [Alias('n', '-dry-run')][switch]$WhatIf, |
| 18 | + [switch]$AllowPrerelease, |
| 19 | + |
| 20 | + [switch]$WhatIf, |
| 21 | + |
10 | 22 | [string]$Name = "*", |
11 | | - [ValidateSet('AllUsers', 'CurrentUser')][string]$Scope = 'CurrentUser' |
| 23 | + |
| 24 | + [ValidateSet('AllUsers', 'CurrentUser')] |
| 25 | + [string]$Scope = "CurrentUser" |
12 | 26 | ) |
13 | 27 |
|
14 | | - # Test elevated permissions for scope allusers |
15 | 28 | if ($Scope -eq "AllUsers") { |
16 | | - if ((Test-IsElevated) -eq $False) { |
| 29 | + if ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) -eq $False) { |
17 | 30 | Write-Warning "Function $($MyInvocation.MyCommand) needs admin privileges to perform actions." |
18 | | - Break |
| 31 | + break |
19 | 32 | } |
20 | 33 | } |
21 | 34 |
|
22 | | - # Status of allow prerelease switch |
23 | | - if ($AllowPrerelease) { |
24 | | - '' |
25 | | - Write-Host "Updating modules to latest " -ForegroundColor "Blue" -NoNewline |
26 | | - Write-Host "prerelease " -ForegroundColor "Yellow" -NoNewline |
27 | | - Write-Host "versions..." -ForegroundColor "Blue" |
28 | | - Write-Host "-------------------------------------------------" -ForegroundColor "Blue" |
29 | | - } else { |
30 | | - '' |
31 | | - Write-Host "Updating modules to latest " -ForegroundColor "Blue" -NoNewline |
32 | | - Write-Host "stable " -ForegroundColor "Yellow" -NoNewline |
33 | | - Write-Host "versions..." -ForegroundColor "Blue" |
34 | | - Write-Host "---------------------------------------------" -ForegroundColor "Blue" |
35 | | - } |
| 35 | + $CurrentModules = Get-InstalledModule -Name $Name -ErrorAction SilentlyContinue | Select-Object -Property Name, Version | Sort-Object Name |
36 | 36 |
|
37 | | - # Get all installed modules |
38 | | - if ((Test-Path "$Env:DOTFILES\modules.lock.json") -and ($Name -eq "*")) { |
39 | | - $CurrentModules = Get-Content "$Env:DOTFILES\modules.lock.json" | ConvertFrom-Json | Select-Object -Property Name, Version | Sort-Object Name |
40 | | - } else { |
41 | | - $CurrentModules = Get-InstalledModule -Name $Name -ErrorAction SilentlyContinue | Select-Object -Property Name, Version | Sort-Object Name |
42 | | - } |
| 37 | + if (!$CurrentModules) { Write-Host "No modules found." -ForegroundColor Red; return } |
| 38 | + else { |
| 39 | + $i = 1 |
| 40 | + $moduleCount = $CurrentModules.Count |
| 41 | + ''; Write-Host "$moduleCount " -ForegroundColor Yellow -NoNewline; Write-Host "module(s) installed." -ForegroundColor Green; '' |
43 | 42 |
|
44 | | - # If no modules installed. |
45 | | - if (-not $CurrentModules) { |
46 | | - '' |
47 | | - Write-Host "No modules found." -ForegroundColor "Red" |
48 | | - Return |
49 | | - } else { |
50 | | - '' |
51 | | - $ModulesCount = $CurrentModules.Count |
52 | | - Write-Host "$ModulesCount " -ForegroundColor "White" -NoNewLine |
53 | | - Write-Host "module(s) installed." -ForegroundColor "Green" |
54 | | - } |
| 43 | + if ($AllowPrerelease) { Write-Host "Trying to update modules to latest " -ForegroundColor Blue -NoNewline; Write-Host "prerelease " -NoNewline -ForegroundColor Magenta; Write-Host "version" -ForegroundColor Blue } |
| 44 | + else { Write-Host "Trying to update modules to latest " -ForegroundColor Blue -NoNewline; Write-Host "stable " -NoNewline -ForegroundColor Magenta; Write-Host "version" -ForegroundColor Blue } |
55 | 45 |
|
56 | | - foreach ($Module in $CurrentModules) { |
57 | | - '' |
58 | | - Write-Host "Checking for updated version of module: " -ForegroundColor "Gray" -NoNewLine |
59 | | - Write-Host "$($Module.Name)" -ForegroundColor "White" |
60 | | - $OnlineVersion = (Find-Module -Name $Module.Name -AllVersions | Sort-Object PublishedDate -Descending)[0].Version |
61 | | - $CurrentVersion = (Get-InstalledModule -Name $Module.Name).Version |
62 | | - if ($CurrentVersion -lt $OnlineVersion) { |
63 | | - try { |
64 | | - Update-Module -Name $Module.Name -AllowPrerelease:$AllowPrerelease -Scope:$Scope -Force:$True -ErrorAction Stop -WhatIf:$WhatIf.IsPresent |
65 | | - $CurrentVersion = $CurrentModules | Where-Object Name -EQ $Module.Name |
66 | | - if ($CurrentVersion.Version -notlike $Module.Version) { |
67 | | - Write-Host "Updated module $($Module.Name) from version $($CurrentVersion.Version) to $($Module.Version)" -ForegroundColor "Green" |
68 | | - } |
69 | | - foreach ($Version in $CurrentVersion) { |
70 | | - if ($Version.Version -ne $CurrentVersion[0].Version) { |
71 | | - Uninstall-Module -Name $Module.Name -RequiredVersion $Version.Version -Force:$True -ErrorAction Stop -WhatIf:$WhatIf.IsPresent |
72 | | - } |
| 46 | + foreach ($module in $CurrentModules) { |
| 47 | + $OnlineVersion = (Find-Module -Name $($module.Name) -AllVersions | Sort-Object PublishedDate -Descending)[0].Version |
| 48 | + $CurrentVersion = (Get-InstalledModule -Name $($module.Name)).Version |
| 49 | + |
| 50 | + if ($CurrentVersion -ne $OnlineVersion) { |
| 51 | + try { |
| 52 | + Update-Module -Name $($module.Name) -AllowPrerelease:$AllowPrerelease -Scope:$Scope -Force:$True -WhatIf:$WhatIf.IsPresent |
| 53 | + } catch { |
| 54 | + Write-Error "Error occurred while updating module $($module.Name): $_" |
73 | 55 | } |
74 | | - } catch { |
75 | | - Write-Error "Error occurred while updating module $($Module.Name): $_" |
76 | 56 | } |
77 | | - } else { |
78 | | - Write-Host "Already up-to-date!" -ForegroundColor "Blue" |
| 57 | + |
| 58 | + [int]$percentCompleted = ($i / $moduleCount) * 100 |
| 59 | + Write-Progress -Activity "Updating Module $($module.Name)" -Status "$percentCompleted% Completed - $($module.Name) v$OnlineVersion" -PercentComplete $percentCompleted |
| 60 | + $i++ |
79 | 61 | } |
| 62 | + if ($?) { Write-Host "Everything is up-to-date!" -ForegroundColor Green } |
80 | 63 | } |
81 | | - '' |
82 | 64 | } |
0 commit comments