powershell - struggling to reference DLL that is not in GAC

1.1k views Asked by At

I'm trying (and failing) to load a DLL (not in GAC) into powershell.

The DLL is part of the Microsoft.Diagnostics.Runtime (ClrMD) Nuget Package See the full documentation for Microsoft.Diagnostics.Runtime.

The reason why i cant get it into GAC is because the DLL wasn't created with a strongname (that's what gacutil says)

So i've tried all of the options below.. but can't make it work.. wondering if anyone has any tricks:

$dllpath = somepath\Microsoft.Diagnostics.Runtime.0.9.170809.03\lib\net40\Microsoft.Diagnostics.Runtime.dll"

#LoadFile (this shouldnt work according to method documentation,.. it's just for inspection)
 [System.Reflection.Assembly]::LoadFile($dllpath)

#LoadFrom
 [System.Reflection.Assembly]::LoadFrom($dllpath)

#LoadwithPartialName .. this is deprecated
 [reflection.assembly]::LoadWithPartialName( "Microsoft.Diagnostics.Runtime")

# add-type
 add-type -path $dllpath

and also

$dllname = "Microsoft.Diagnostics.Runtime, Version=0.8.31.1, Culture=neutral, PublicKeyToken=null"
[System.Reflection.Assembly]::Load($dllname) 

the dllname i got using:

$dllpath = "somepath\Microsoft.Diagnostics.Runtime.0.9.170809.03\lib\net40\Microsoft.Diagnostics.Runtime.dll"
$dllname = [System.Reflection.AssemblyName]::GetAssemblyName($dllpath).Fullname

Anyway.... in all of the cases above i can see that the DLL got loaded into the current appdomain within my session:

[System.AppDomain]::CurrentDomain.GetAssemblies() | where-object -Filterscript {$_.Fullname -like "*Diagnostics*"}

but then when i try to reference it says "type not available"

e.g.

This should work.. also when i type the double-colons after it intellisense should spit out properties/methods But it fails with "type not found"

[Microsoft.Diagnostics.Runtime]

This should definitely work. afaik this is a static method first variable is processID, second variable is timeout (seconds) But it fails with "type not found"

[Microsoft.Diagnostics.Runtime]::DataTarget.AttachToProcess(7984,5000)

It will obviously work with C# but i dont really want to learn that right now as it would delay me while i get my head around it... but maybe the necessary step...

2

There are 2 answers

1
Maximilian Burszley On BEST ANSWER

To expand on my comment, create a file:

MyModule.psd1

@{
    RequiredAssemblies = @('bin\lib.dll','bin\lib2.dll')
 }

Your folder structure should look like so:

C:.
|---MyModule
    |---MyModule.psd1
    |---bin
        |---lib.dll
        |---lib2.dll

Now utilize Import-Module -Name 'C:\MyModule' to load your dlls.

0
albvar On

Try just calling Import-Module pathToDll