Skip to content

Instantly share code, notes, and snippets.

@rakheshster
Created July 14, 2023 18:31
Show Gist options
  • Save rakheshster/90ec04c4165dfbb8307b393157381cdb to your computer and use it in GitHub Desktop.
Save rakheshster/90ec04c4165dfbb8307b393157381cdb to your computer and use it in GitHub Desktop.
function InstallUpdate-Module {
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=$true)]
[string[]]$Modules,
[Parameter(Mandatory=$false)]
[switch]$AllowPrerelease = $false,
[Parameter(Mandatory=$false)]
[switch]$Force = $false
)
# I use this function as part of my profile and don't want it to run every time I launch a new session.
# So the function stores the current execution date to a file and compare it on the next run.
# If the current date isn't ahead, the function doesn't run.
# Use the -Force switch to override this.
$timestampFile = "$($env:TMPDIR)installupdate-module.txt"
$presentTimestamp = Get-Date (Get-Date).ToUniversalTime() -Format "yyyy-MM-dd"
if (Test-Path -PathType Leaf -Path $timestampFile) {
[datetime]$previousTimestamp = Get-Content $timestampFile
if ([datetime]$presentTimestamp -le $previousTimestamp -and !$Force) {
Write-Host -ForegroundColor Yellow "Not checking for updated modules. Use -Force if needed."
return
}
}
# Only update the timestamp file in case of no errors.
$noErrors = $true
foreach ($module in $modules) {
Write-Progress "Checking module $module"
try {
$repoModule = $null
if ($AllowPrerelease) {
$repoModule = Find-Module -Name $module -ErrorAction Stop -AllowPrerelease
} else {
$repoModule = Find-Module -Name $module -ErrorAction Stop
}
} catch {
Write-Warning $_.Exception.Message
$noErrors = $false
continue
}
if ($repoModule) {
$repoModuleVersion = $repoModule.Version
$repoModuleRepository = $repoModule.Repository
Write-Host ("Latest version of the {0} module in the PowerShell Gallery is {1}" -f $module, $repoModuleVersion)
}
try {
$installedModule = $null
$installedModule = Get-InstalledModule -Name $module -ErrorAction Stop
} catch {
if ($_.Exception.Message -match "No match was found") {
# Isn't installed
$installedModule = $null
} else {
Write-Warning $_.Exception.Message
$noErrors = $false
continue
}
}
if ($null -ne $installedModule) {
# Module is installed, update if needed
$installedModuleVersion = $installedModule.Version
$installedModuleRepository = $installedModule.Repository
$installedModuleLocation = $installedModule.InstalledLocation
if ($installedModuleVersion -ne $repoModuleVersion) {
Write-Host -ForegroundColor Yellow ("Installed version of module {0} is {1}, needs updating to version {2}" -f $module, $installedModuleVersion, $repoModuleVersion)
Write-Progress "Updating module $module"
try {
Update-Module -Name $module -ErrorAction Stop
try {
$installedModule2 = $null
$installedModule2 = Get-InstalledModule -Name $module -ErrorAction Stop
} catch {}
$installedModuleVersion2 = $installedModule2.Version
if ($null -ne $installedModule2) {
Write-Host -ForegroundColor Green ("Updated module {0} to version {1}" -f $module, $installedModuleVersion2)
} else {
Write-Warning ("Couldn't find the updated module {0} locally; it was previously installed at {1}" -f $module, $installedModuleLocation)
$noErrors = $false
}
} catch {
Write-Warning $_.Exception.Message
$noErrors = $false
}
} else {
Write-Host -ForegroundColor Green ("Installed version of the {0} module is {1} at {2}" -f $module, $installedModuleVersion, $installedModuleLocation)
if ($installedModuleRepository -ne $repoModuleRepository) {
Write-Warning ("Installed version of the {0} module is from a different repo: {1}" -f $module, $installedModuleRepository)
}
}
} else {
# Module is not installed, so install it
Write-Progress "Installing module $module"
try {
if ($AllowPrerelease) {
Install-Module -Name $module -ErrorAction Stop -AllowPrerelease
} else {
Install-Module -Name $module -ErrorAction Stop
}
} catch {
Write-Warning $_.Exception.Message
}
try {
$installedModule2 = $null
$installedModule2 = Get-InstalledModule -Name $module -ErrorAction Stop
} catch {}
$installedModuleVersion2 = $installedModule2.Version
if ($null -ne $installedModule2) {
Write-Host ("Updated module {0} to version {1}" -f $module, $installedModuleVersion2)
} else {
Write-Warning ("Couldn't find the newly installed module {0}" -f $module)
$noErrors = $false
}
}
}
if ($noErrors) {
try {
$presentTimestamp | Out-File -FilePath $timestampFile -Force -ErrorAction Stop
} catch {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment