Skip to content

Instantly share code, notes, and snippets.

@sidewinder94
Last active July 17, 2025 14:05
Show Gist options
  • Select an option

  • Save sidewinder94/a815adc6d8e5e0e1c9d9c93add1a7cd1 to your computer and use it in GitHub Desktop.

Select an option

Save sidewinder94/a815adc6d8e5e0e1c9d9c93add1a7cd1 to your computer and use it in GitHub Desktop.
function Import-DotNetAssembly {
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory=$true)]
[string]$AssemblyName
)
try {
# Rechercher d'abord le chemin des runtimes
Write-Verbose "Recherche des runtimes installés..."
$runtimesOutput = dotnet --list-runtimes
$latestRuntime = $runtimesOutput | Where-Object { $_ -like "Microsoft.AspNetCore.App*" } | Select-Object -Last 1
if ($latestRuntime) {
Write-Verbose "Runtime ASP.NET Core trouvé : $latestRuntime"
$parts = $latestRuntime -split '\s+\[|\]'
$runtimePath = $parts[1]
$sharedRuntimePath = Join-Path (Split-Path $runtimePath -Parent) "Microsoft.AspNetCore.App"
if (Test-Path $sharedRuntimePath) {
$latestRuntimeVersion = Get-ChildItem $sharedRuntimePath | Sort-Object Name -Descending | Select-Object -First 1
$runtimeAssemblyPath = Join-Path $latestRuntimeVersion.FullName "$AssemblyName.dll"
if (Test-Path $runtimeAssemblyPath) {
Add-Type -Path $runtimeAssemblyPath
Write-Verbose "Assembly chargé depuis runtime : $runtimeAssemblyPath"
return $true
}
}
}
# Si non trouvé dans les runtimes, chercher dans les SDKs
Write-Verbose "Recherche dans les SDKs installés..."
$sdksOutput = dotnet --list-sdks
$latestSdk = $sdksOutput | Select-Object -Last 1
if ($latestSdk) {
Write-Verbose "SDK ASP.NET Core trouvé : $latestSdk"
$parts = $latestSdk -split '\s+\[|\]'
$sdkPath = $parts[1]
$sharedPath = Join-Path (Split-Path $sdkPath -Parent) "shared/Microsoft.AspNetCore.App"
if (Test-Path $sharedPath) {
$latestVersion = Get-ChildItem $sharedPath | Sort-Object Name -Descending | Select-Object -First 1
$assemblyPath = Join-Path $latestVersion.FullName "$AssemblyName.dll"
if (Test-Path $assemblyPath) {
Add-Type -Path $assemblyPath
Write-Verbose "Assembly chargé depuis SDK : $assemblyPath"
return $true
}
}
}
throw "Assembly non trouvé : $AssemblyName.dll"
}
catch {
Write-Error "Impossible de charger l'assembly '$AssemblyName': $_"
Write-Error "Veuillez vérifier que .NET Core et les runtimes requis sont installés."
return $false
}
}
# Exporter la fonction
Export-ModuleMember -Function Import-DotNetAssembly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment