|
1 | 1 | Function Update-RSModule { |
2 | | - <# |
| 2 | + <# |
3 | 3 | .SYNOPSIS |
4 | 4 | This module let you maintain your installed modules in a easy way. |
5 | 5 |
|
|
13 | 13 | .PARAMETER Scope |
14 | 14 | Need to specify scope of the installation/update for the module, either AllUsers or CurrentUser. Default is CurrentUser. |
15 | 15 | If this parameter is empty it will use CurrentUser |
16 | | - The parameter -Scope don't effect the uninstall-module function this is because of limitation from Microsoft. |
| 16 | + The parameter -Scope don't effect the uninstall-module function this is because of limitation from Microsoft. |
17 | 17 | - Scope effect Install/update module function. |
18 | 18 |
|
19 | 19 | .PARAMETER ImportModule |
|
42 | 42 | Update-RSModule -Module "PowerCLI, ImportExcel" -UninstallOldVersion -ImportModule |
43 | 43 | # This will update the modules PowerCLI and ImportExcel and delete all of the old versions that are installed of PowerCLI and ImportExcel and then import the modules. |
44 | 44 |
|
45 | | - .RELATED LINKS |
| 45 | + .LINK |
46 | 46 | https://github.com/rstolpe/MaintainModule/blob/main/README.md |
47 | 47 |
|
48 | 48 | .NOTES |
|
58 | 58 | Param( |
59 | 59 | [Parameter(Mandatory = $false, HelpMessage = "Enter module or modules (separated with ,) that you want to update, if you don't enter any all of the modules will be updated")] |
60 | 60 | [string]$Module, |
61 | | - [ValidateSet("CurrentUser", "AllUsers")] |
62 | | - [Parameter(Mandatory = $true, HelpMessage = "Enter CurrentUser or AllUsers depending on what scope you want to change your modules")] |
| 61 | + [ValidateSet("CurrentUser", "AllUsers")] |
| 62 | + [Parameter(Mandatory = $false, HelpMessage = "Enter CurrentUser or AllUsers depending on what scope you want to change your modules")] |
63 | 63 | [string]$Scope = "CurrentUser", |
64 | 64 | [Parameter(Mandatory = $false, HelpMessage = "Import modules that has been entered in the module parameter at the end of this function")] |
65 | 65 | [switch]$ImportModule = $false, |
|
85 | 85 | } |
86 | 86 | else { |
87 | 87 | Write-Verbose "User has added modules to the Module parameter, splitting them" |
88 | | - $Module = $Module.Split(",").Trim() |
| 88 | + $OldModule = $Module.Split(",").Trim() |
| 89 | + |
| 90 | + [System.Collections.ArrayList]$Module = @() |
| 91 | + if ($InstallMissing -eq $false) { |
| 92 | + Write-Verbose "Looking so the modules exists in the system..." |
| 93 | + foreach ($m in $OldModule) { |
| 94 | + if ($m -in $InstalledModules.name) { |
| 95 | + Write-Verbose "$($m) did exists in the system..." |
| 96 | + [void]($Module.Add($m)) |
| 97 | + } |
| 98 | + else { |
| 99 | + Write-Warning "$($m) did not exists in the system, skipping this module..." |
| 100 | + } |
| 101 | + } |
| 102 | + } |
89 | 103 | } |
90 | 104 |
|
91 | 105 | # Making sure that TLS 1.2 is used. |
|
111 | 125 |
|
112 | 126 | # Start looping trough every module that are stored in the string Module |
113 | 127 | foreach ($m in $Module.Split()) { |
114 | | - |
115 | 128 | Write-Verbose "Checks if $($m) are installed" |
116 | 129 | if ($m -in $InstalledModules.Name) { |
117 | 130 |
|
118 | | - # Get all of the installed versions of the module |
| 131 | + # Getting the latest installed version of the module |
119 | 132 | Write-Verbose "Collecting all installed version of $($m)..." |
120 | | - $GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object PublishedDate -Descending |
| 133 | + $GetAllInstalledVersions = Get-InstalledModule -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object Version |
| 134 | + [version]$LatestInstalledVersion = $($GetAllInstalledVersions | Select-Object Version -First 1).version |
121 | 135 |
|
122 | | - # Collects the latest version of module |
123 | | - Write-Verbose "Looking up the latest version of $($m)..." |
124 | | - $CollectLatestVersion = Find-Module -Name $m | Sort-Object Version -Descending | Select-Object Version -First 1 |
| 136 | + # Collects the latest version of module from the source where the module was installed from |
| 137 | + Write-Output "Looking up the latest version of $($m)..." |
| 138 | + [version]$CollectLatestVersion = $(Find-Module -Name $m -AllVersions | Sort-Object { $_.Version -as [version] } -Descending | Select-Object Version -First 1).version |
125 | 139 |
|
126 | | - # Looking if the version of the module are the latest version |
127 | | - if ($GetAllInstalledVersions.Version -lt $CollectLatestVersion.Version) { |
| 140 | + # Looking if the version of the module are the latest version, it it's not the latest it will install the latest version. |
| 141 | + if ($LatestInstalledVersion -lt $CollectLatestVersion) { |
128 | 142 | try { |
129 | | - Write-Output "Found a newer version of $($m), version $($CollectLatestVersion.Version)" |
130 | | - Write-Output "Updating $($m) to version $($CollectLatestVersion.Version)..." |
| 143 | + Write-Output "Found a newer version of $($m), version $($CollectLatestVersion)" |
| 144 | + Write-Output "Updating $($m) from $($LatestInstalledVersion) to version $($CollectLatestVersion)..." |
131 | 145 | Update-Module -Name $($m) -Scope $Scope -Force |
132 | | - Write-Output "$($m) has been updated to version $($CollectLatestVersion.Version)!" |
| 146 | + Write-Output "$($m) has now been updated to version $($CollectLatestVersion)!`n" |
133 | 147 | } |
134 | 148 | catch { |
135 | 149 | Write-Error "$($PSItem.Exception)" |
136 | 150 | continue |
137 | 151 | } |
| 152 | + } |
138 | 153 |
|
139 | | - # If switch -UninstallOldVersion has been used then the old versions will be uninstalled from the module |
140 | | - if ($UninstallOldVersion -eq $true) { |
| 154 | + # If switch -UninstallOldVersion has been used then the old versions will be uninstalled from the module |
| 155 | + if ($UninstallOldVersion -eq $true) { |
| 156 | + if ($GetAllInstalledVersions.Count -gt 1) { |
141 | 157 | Uninstall-RSModule -Module $m |
142 | 158 | } |
143 | 159 | } |
|
168 | 184 | # Collect all of the imported modules. |
169 | 185 | Write-Verbose "Collecting all of the installed modules..." |
170 | 186 | $ImportedModules = Get-Module | Select-Object Name, Version |
171 | | - |
| 187 | + |
172 | 188 | # Import module if it's not imported |
173 | 189 | Write-Verbose "Starting to import the modules..." |
174 | 190 | foreach ($m in $Module.Split()) { |
|
0 commit comments