From fbf40fa98e85bf8730aa6475b7640c1d2d84adc7 Mon Sep 17 00:00:00 2001 From: DrIOS <58635327+DrIOSX@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:03:30 -0600 Subject: [PATCH] add: method to avoid assembly already loaded error --- CHANGELOG.md | 1 + helpers/Build-Help.ps1 | 2 +- source/Private/Assert-ModuleAvailability.ps1 | 15 ++++++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bbfb2d..35acd63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on and uses the types of changes according to [Keep a Change - Added additional error handling to connect function to identify problematic steps when they occur. - Added new method of verifying spo tenant for Connect-SPOService branch of connect function. +- Added method to avoid "assembly already loaded" error in PNP Powershell function on first run, subsequent runs in the same session will still throw the error. ## [0.1.26] - 2024-08-04 diff --git a/helpers/Build-Help.ps1 b/helpers/Build-Help.ps1 index 034b503..c35f681 100644 --- a/helpers/Build-Help.ps1 +++ b/helpers/Build-Help.ps1 @@ -5,7 +5,7 @@ Import-Module .\output\module\M365FoundationsCISReport\*\*.psd1 <# - $ver = "v0.1.26" + $ver = "v0.1.27" git checkout main git pull origin main git tag -a $ver -m "Release version $ver refactor Update" diff --git a/source/Private/Assert-ModuleAvailability.ps1 b/source/Private/Assert-ModuleAvailability.ps1 index 807594f..09c193f 100644 --- a/source/Private/Assert-ModuleAvailability.ps1 +++ b/source/Private/Assert-ModuleAvailability.ps1 @@ -20,6 +20,17 @@ function Assert-ModuleAvailability { else { Write-Verbose "$ModuleName module is already at required version or newer." } + if ($ModuleName -eq "Microsoft.Graph") { + # "Preloading Microsoft.Graph assembly to prevent type-loading issues..." + Write-Verbose "Preloading Microsoft.Graph assembly to prevent type-loading issues..." + try { + # Run a harmless cmdlet to preload the assembly + Get-MgGroup -Top 1 -ErrorAction SilentlyContinue | Out-Null + } + catch { + Write-Verbose "Could not preload Microsoft.Graph assembly. Error: $_" + } + } if ($SubModules.Count -gt 0) { foreach ($subModule in $SubModules) { Write-Verbose "Importing submodule $ModuleName.$subModule..." @@ -30,11 +41,9 @@ function Assert-ModuleAvailability { Write-Verbose "Importing module $ModuleName..." Import-Module -Name $ModuleName -RequiredVersion $RequiredVersion -ErrorAction Stop -WarningAction SilentlyContinue | Out-Null } - } catch { throw "Assert-ModuleAvailability:`n$_" } } - -} +} \ No newline at end of file