Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Assembly.Load() then Import-Module as DynamicModule
# Load Assembly without dll Lock
function LoadAssembly ([System.Reflection.AssemblyName[]]$Arr, [string]$Directory)
{
$loadeAssemblies = [System.AppDomain]::CurrentDomain.GetAssemblies();
$names = New-Object System.Collections.Generic.List[string];
foreach ($assem in $loadeAssemblies)
{
$names.Add($assem.FullName);
}
foreach ($anam in $Arr)
{
if (! ($names.Contains($anam.FullName)))
{
try
{
[byte[]]$binary = [System.IO.File]::ReadAllBytes((Join-Path $Directory $anam.Name) + ".dll");
$loadeAssembly = [System.Reflection.Assembly]::Load($binary);
$referencedAssemblies = $loadeAssemblies.GetReferencedAssemblies();
LoadAssembly($referencedAssemblies, $Directory);
}
catch
{
}
}
}
}
# Load Assembly as byte[] and Import Module as DynamicModule
function ImportModuleToCurrentAppDomain ([string]$Path)
{
[byte[]]$primary = [System.IO.File]::ReadAllBytes($Path);
$assembly = [System.Reflection.Assembly]::Load($primary);
$arr = $assembly.GetReferencedAssemblies();
LoadAssembly -Arr $arr -Directory (Split-Path $Path -Parent);
return (Import-Module -Assembly $assembly -PassThru);
}
# Find Module Path
$moduleName = "TestModule";
$dllName = "$moduleName.dll";
$parentPath = [System.IO.Directory]::GetParent($MyInvocation.MyCommand.Path);
$myDllPath = Join-Path $parentPath $dllName;
if (Test-Path $myDllPath)
{
# get from current path. Normally always go this path
$path = $myDllPath;
}
else
{
Write-Warning "Search Module"
# Stop Explorer at first hit
$moduleRoot = (Get-Module -ListAvailable -Name $moduleName | select -First 1).ModuleBase;
$path = Join-Path $moduleRoot $dllName;
}
# Import as DyanmicModule
$module = ImportModuleToCurrentAppDomain -Path $path
@guitarrapc

This comment has been minimized.

Copy link
Owner Author

guitarrapc commented Dec 25, 2015

animation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.