8585 }
8686 else {
8787 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+ }
89103 }
90104
91105 # Making sure that TLS 1.2 is used.
111125
112126 # Start looping trough every module that are stored in the string Module
113127 foreach ($m in $Module.Split ()) {
114-
115128 Write-Verbose " Checks if $ ( $m ) are installed"
116129 if ($m -in $InstalledModules.Name ) {
117130
118- # Get all of the installed versions of the module
131+ # Getting the latest installed version of the module
119132 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
121135
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
125139
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 ) {
128142 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 ) ..."
131145 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 "
133147 }
134148 catch {
135149 Write-Error " $ ( $PSItem.Exception ) "
136150 continue
137151 }
152+ }
138153
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 ) {
141157 Uninstall-RSModule - Module $m
142158 }
143159 }
190206 }
191207 }
192208 Write-Output " `n ---/// Script Finished! ///---"
193- }
209+ }
210+
211+ Update-RSModule - UninstallOldVersion - Scope CurrentUser
0 commit comments