|
| 1 | +# ---------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright Microsoft Corporation |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +function Get-AzModuleUpdateList { |
| 16 | + [OutputType([System.Collections.ArrayList])] |
| 17 | + [CmdletBinding(DefaultParameterSetName = 'Default', PositionalBinding = $false)] |
| 18 | + param( |
| 19 | + [Parameter(HelpMessage = 'The module name.')] |
| 20 | + [ValidateNotNullOrEmpty()] |
| 21 | + [string[]] |
| 22 | + ${Name}, |
| 23 | + |
| 24 | + [Parameter(HelpMessage = 'The Registered Repostory.')] |
| 25 | + [ValidateNotNullOrEmpty()] |
| 26 | + [string] |
| 27 | + ${Repository} |
| 28 | + ) |
| 29 | + |
| 30 | + process { |
| 31 | + |
| 32 | + $modules = New-Object System.Collections.ArrayList |
| 33 | + |
| 34 | + Write-Debug "Retrieving installed Az modules" |
| 35 | + $installModules = @{} |
| 36 | + try { |
| 37 | + (PowerShellGet\Get-InstalledModule -Name "Az*" -ErrorAction Stop).Where({ |
| 38 | + ($_.Author -eq 'Microsoft Corporation' -or $_.CompanyName -eq 'Microsoft Corporation') -and ($_.Name -match "Az(\.[a-zA-Z0-9]+)?$") |
| 39 | + }) | ForEach-Object { |
| 40 | + $installModules[$_.Name] = @() |
| 41 | + } |
| 42 | + } |
| 43 | + catch { |
| 44 | + Write-Warning $_ |
| 45 | + } |
| 46 | + |
| 47 | + Write-Debug "The Az modules currently installed: $($installModules.Keys)" |
| 48 | + |
| 49 | + if ($installModules.Keys) { |
| 50 | + foreach ($key in $installModules.Keys.Clone()) { |
| 51 | + $installedModules = (PowerShellGet\Get-InstalledModule -Name $key -AllVersions).Where( { -not $_.AdditionalMetadata.IsPrerelease }) |
| 52 | + foreach ($installed in $installedModules) { |
| 53 | + $installModules[$key] += [System.Tuple]::Create($installed.Version, $installed.Repository) |
| 54 | + } |
| 55 | + if($installModules[$key].Count -gt 1) { |
| 56 | + $installModules[$key] = ($installModules[$key] | Sort-Object -Property @{Expression={[Version]$_.Item1}} -Descending) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + $modulesToCheck = @() |
| 62 | + $modulesToCheck += if ($Name) { |
| 63 | + #Fixme:Damien |
| 64 | + $Name.Foreach({ |
| 65 | + if ($installModules.ContainsKey($_) -and $installModules[$_]) { |
| 66 | + [System.Tuple]::Create($_, $installModules[$_][0].Item2) |
| 67 | + } |
| 68 | + }) |
| 69 | + } else { |
| 70 | + $installModules.Keys.ForEach({[System.Tuple]::Create($_, $installModules[$_][0].Item2)}) |
| 71 | + } |
| 72 | + |
| 73 | + $moduleSet = [System.Collections.Generic.HashSet[string]]::new() |
| 74 | + $null = $modulesToCheck | ForEach-Object {$moduleSet.Add($_.Item1)} |
| 75 | + |
| 76 | + $index = 0 |
| 77 | + while($index -lt $modulesToCheck.Count) { |
| 78 | + $moduleName = $modulesToCheck[$index].Item1 |
| 79 | + $repo = if ($Repository) {$Repository} else {$modulesToCheck[$index].Item2} |
| 80 | + if($repo) { |
| 81 | + $module = PowerShellGet\Find-Module -Name $moduleName -Repository $repo |
| 82 | + if ($module) { |
| 83 | + Write-Progress -Activity "Find Module" -CurrentOperation "$($module.Name) with the latest version $($module.Version)" -PercentComplete ($index / $modulesToCheck.Count * 100) |
| 84 | + $null = $modules.Add($module) |
| 85 | + foreach ($dep in $module.Dependencies.Name) { |
| 86 | + if (!$moduleSet.Contains($dep)) { |
| 87 | + $modulesToCheck += [System.Tuple]::Create($dep, $repo) |
| 88 | + $null = $moduleSet.Add($dep) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + else { |
| 94 | + Write-Warning "Failed to find Repository of $moduleName. The module cannot be updated." |
| 95 | + } |
| 96 | + $index += 1 |
| 97 | + } |
| 98 | + Write-Progress -Activity "Find Module" -Completed |
| 99 | + $modules = ($modules | Sort-Object -Property Name -Unique) |
| 100 | + |
| 101 | + Write-Debug "The modules to check for update: $($modules.Name)" |
| 102 | + |
| 103 | + $modulesToUpdate = $modules.Where({ !$installModules.ContainsKey($_.Name) -or !$installModules[$_.Name] -or [Version]($_.Version) -gt [Version]$installModules[$_.Name][0].Item1 }) |
| 104 | + if ("Az" -eq ($modulesToUpdate | Select-Object -First 1).Name) { |
| 105 | + $first, $rest = $modulesToUpdate |
| 106 | + $modulesToUpdate = (@() + $rest + $first) |
| 107 | + } |
| 108 | + |
| 109 | + If (-not $modulesToUpdate) { |
| 110 | + Write-Warning "None of your specifed module needs upgrading." |
| 111 | + } |
| 112 | + else { |
| 113 | + Write-Debug "The modules contain update: $($modulesToUpdate.Name)" |
| 114 | + } |
| 115 | + Write-Output ($modulesToUpdate | ForEach-Object { |
| 116 | + New-Object -Type PSObject -Property @{ |
| 117 | + 'Name' = $_.Name |
| 118 | + 'VersionToUpgrade' = $_.Version |
| 119 | + 'InstalledVersion' = if ($installModules.ContainsKey($_.Name)) {$installModules[$_.Name].Item1} else {@()} |
| 120 | + 'Repository' = $_.Repository |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments