Created
February 10, 2016 00:29
-
-
Save ritalin/7804d03d1d9b04f6e8df to your computer and use it in GitHub Desktop.
.NET Assembly loader for Powershell. Based on https://gist.github.com/guitarrapc/72bffd1e1c12bec100f9#file-testmodule-psm1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function LoadAssembly { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
[string[]]$asmNames, | |
[string]$baseDir = $PSScriptRoot | |
) | |
begin { | |
$domain = [System.AppDomain]::CurrentDomain | |
[Reflection.Assembly[]]$asms = $domain.GetAssemblies() | |
$cache = New-Object "System.Collections.Generic.HashSet[string]" (,[string[]]$asms.GetName().Name) | |
$path = $domain.getType().Assembly.Location | |
$searchPath = $baseDir,([IO.FileInfo]$path).Directory.FullName | |
} | |
process { | |
foreach ($asmName in $asmNames) { | |
LoadAssemblyInternal $asmName $searchPath $cache | |
} | |
} | |
} | |
function LoadAssemblyInternal([string[]]$asmNames, [string[]]$searchPath, $cache) { | |
foreach ($asmName in $asmNames) { | |
if (-not $cache.Contains($asmName)) { | |
$path = $searchPath | ls -filter "$asmName.dll" | select -first 1 | |
if ($path -ne $null) { | |
Write-Verbose "Try Loading: $path" | |
[byte[]]$binary = [System.IO.File]::ReadAllBytes($path.FullName) | |
$asm = [System.Reflection.Assembly]::Load($binary); | |
$asm | |
Write-Verbose "Loaded: $($asm.FullName)" | |
$cache.Add($asm.GetName().Name) | Out-Null | |
LoadAssemblyInternal $asm.GetReferencedAssemblies().Name $searchPath $cache | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment