Last active
January 27, 2026 21:21
-
-
Save dancing-groot/b0521f8152b2aabc6876db7ea1cc922e to your computer and use it in GitHub Desktop.
Update-RequireModules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Update-RequireModules | |
| { | |
| <# | |
| .SYNOPSIS | |
| Checks repository pre-requisites and install/update the modules needed for the script using PSResouceGet | |
| .LINK | |
| https://gist.github.com/dancing-groot/b0521f8152b2aabc6876db7ea1cc922e | |
| .NOTES | |
| Version: 2026.01.27 | |
| Author: @dancing-groot | |
| Notes: Improved error catching and debug information | |
| #> | |
| [cmdletBinding()] | |
| param | |
| ( | |
| [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelinebyPropertyName)] | |
| [string[]]$Module | |
| ) | |
| begin | |
| { | |
| if ($PSBoundParameters['Debug']) { $DebugPreference = 'Continue' } | |
| if ($PSBoundParameters['Verbose']) { $VerbosePreference = 'Continue' } | |
| ## Update PowerShellGet Pre-Requisites | |
| # Force TLS 1.2 | |
| [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 | |
| # NuGet Package Provider | |
| try | |
| { | |
| Get-PackageProvider -Name NuGet -Debug:$false -ErrorAction Stop | Out-Null | |
| } | |
| catch | |
| { | |
| try | |
| { | |
| Install-PackageProvider -Name NuGet -Force -WarningAction SilentlyContinue -ErrorAction Stop | |
| } | |
| catch | |
| { | |
| Write-Host "Could not install Package Provider NuGet" -ForegroundColor Red | |
| } | |
| } | |
| # PowerShellGet Module | |
| try | |
| { | |
| Get-Module -Name PowerShellGet -Debug:$false -ErrorAction Stop | Out-Null | |
| } | |
| catch | |
| { | |
| try | |
| { | |
| Install-Module PowerShellGet -AllowClobber -Scope AllUsers -Force -Confirm:$false -WarningAction SilentlyContinue -ErrorAction Stop | |
| } | |
| catch | |
| { | |
| Install-Module PowerShellGet -AllowClobber -Scope CurrentUser -Force -Confirm:$false -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | |
| } | |
| } | |
| # Set PSGallery Repository to v2 if needed | |
| try | |
| { | |
| Get-PSResourceRepository -Name PSGallery -Debug:$false -ErrorAction Stop | Out-Null | |
| } | |
| catch | |
| { | |
| Write-ADTLogEntry "Installing the module 'PSResourceGet'" | |
| try | |
| { | |
| Install-Module -Name Microsoft.PowerShell.PSResourceGet -Scope AllUsers -Force -ErrorAction Stop | |
| } | |
| catch | |
| { | |
| Install-Module -Name Microsoft.PowerShell.PSResourceGet -Scope CurrentUser -Force | |
| } | |
| Write-Verbose "Setting v2 attribute for the repository 'PSGallery'" | |
| Set-PSResourceRepository -Name PSGallery -ApiVersion v2 | |
| } | |
| # Trust PowerShell Gallery | |
| if (Get-PSResourceRepository -Debug:$false | Where-Object { $_.Name -eq "PSGallery" -and $_.Trusted -eq $false }) | |
| { | |
| Write-Verbose "Trusting the Repository 'PSGallery'" | |
| Set-PSResourceRepository -Name "PSGallery" -Trusted | |
| } | |
| try | |
| { | |
| Write-Debug "Package Provider: NuGet $((Get-PackageProvider -Name NuGet -Debug:$false -ErrorAction Stop).Version)" | |
| } | |
| catch | |
| { | |
| Write-Debug "Could not retrieve version for Package Proviuder NuGet" | |
| } | |
| Write-Debug "Module: PowerShellGet $((Get-Module -Name PowerShellGet -Debug:$false).Version)" | |
| Write-Debug "Module: PSResourceGet $((Get-Module -Name Microsoft.PowerShell.PSResourceGet -Debug:$false).Version)" | |
| Write-Debug "Resource Repository: PSGallery $((Get-PSResourceRepository -Name PSGallery -Debug:$false).ApiVersion)" | |
| } | |
| process | |
| { | |
| if (-not (Get-PSResource -Name $Module -Debug:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue)) | |
| { | |
| try | |
| { | |
| Install-PSResource -Name $Module -Debug:$false -Scope AllUsers -ErrorAction Stop | |
| } | |
| catch | |
| { | |
| Install-PSResource -Name $Module -Debug:$false -Scope CurrentUser | |
| } | |
| Write-Verbose "Module '$Module' Installed" | |
| } | |
| else | |
| { | |
| try | |
| { | |
| Update-PSResource $Module -Scope AllUsers -Debug:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -ErrorAction Stop | |
| } | |
| catch | |
| { | |
| Update-PSResource $Module -Scope CurrentUser -Debug:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | |
| } | |
| Write-Verbose "Module '$Module' Updated" | |
| } | |
| Write-Debug "Module: $Module $((Get-Module -Name $Module -Debug:$false).Version)" | |
| } | |
| } # Update-RequireModules | |
| "Az.Accounts", "Az.KeyVault" | Update-RequireModules |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment