add: method to avoid assembly already loaded error

This commit is contained in:
DrIOS
2025-01-13 16:03:30 -06:00
parent f409e8a5f1
commit fbf40fa98e
3 changed files with 14 additions and 4 deletions

View File

@@ -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

View File

@@ -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"

View File

@@ -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$_"
}
}
}
}