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

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