Skip to content

Instantly share code, notes, and snippets.

@dancing-groot
Last active December 9, 2025 18:54
Show Gist options
  • Select an option

  • Save dancing-groot/b0521f8152b2aabc6876db7ea1cc922e to your computer and use it in GitHub Desktop.

Select an option

Save dancing-groot/b0521f8152b2aabc6876db7ea1cc922e to your computer and use it in GitHub Desktop.
Update-RequireModules
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: 2025.12.09
Author: @dancing-groot
Notes: Tweaked for PSADT
#>
[cmdletBinding()]
param
(
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelinebyPropertyName)]
[string[]]$Module
)
begin
{
if ($PSBoundParameters['Debug']) { $DebugPreference = 'Continue' }
if ($PSBoundParameters['Verbose']) { $VerbosePreference = 'Continue' }
# Update PowerShellGet Pre-Requisites
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
if ($null -eq (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) {Install-PackageProvider -Name NuGet -Force -WarningAction SilentlyContinue}
if ($null -eq (Get-Module -Name PowerShellGet)) {Install-Module PowerShellGet -AllowClobber -Scope AllUsers -Force -Confirm:$false -WarningAction SilentlyContinue}
# Set PSGallery Repository to v2 if needed
try
{
Get-PSResourceRepository -Name PSGallery | Out-Null
}
catch
{
Write-ADTLogEntry "Installing the module 'PSResourceGet'"
Install-Module -Name Microsoft.PowerShell.PSResourceGet -Scope AllUsers -Force
Write-Verbose "Setting v2 attribute for the repository 'PSGallery'"
Set-PSResourceRepository -Name PSGallery -ApiVersion v2
}
# Trust PowerShell Gallery
if (Get-PSResourceRepository | Where-Object { $_.Name -eq "PSGallery" -and $_.Trusted -eq $false })
{
Write-Verbose "Trusting the Repository 'PSGallery'"
Set-PSResourceRepository -Name "PSGallery" -Trusted
}
}
process
{
if (-not (Get-PSResource -Name $Module -ErrorAction SilentlyContinue -WarningAction SilentlyContinue))
{
Install-PSResource -Name $Module -Scope AllUsers
Write-Verbose "Module '$Module' Installed"
}
else
{
Update-PSResource $Module -Scope AllUsers -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
Write-Verbose "Module '$Module' Updated"
}
}
} # 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