2

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...

g0pher
  • 59
  • 8
  • One thing I've had luck with is using a module manifest to load DLLs instead of reflection or `Add-Type`. I don't know what it does differently, but it does. Use `New-ModuleManifest` and change the `RequiredAssemblies = @()` field. The paths you enter are relative to where the `.psd1` file is. – Maximilian Burszley Dec 08 '17 at 15:23
  • ended up that i was making an error with referencing the assemblies.... powershell requires ```[Microsoft.Diagnostics.Runtime.DataTarget]::AttachToProcess(7984,5000)``` rather than the original ```[Microsoft.Diagnostics.Runtime]::DataTarget.AttachToProcess(7984,5000)``` – g0pher Dec 13 '17 at 07:16

2 Answers2

2

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.

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
0

Try just calling Import-Module pathToDll

albvar
  • 76
  • 10