0

I'm trying to use PowerShell to control a Bluethooth dongle which has API document and dll released.

There is an MasterEmulator class under namespace "Nordicsemi" available, I tested it with C#, I can new an instance with MasterEmulator constructor like below, all other functions work fine too.

MasterEmulator masterEmulator = new MasterEmulator();

However I try to do the same with PowerShell with below script.

[System.Reflection.Assembly]::LoadFile($fullpath)
$MasterEmulatorInstance = New-Object Nordicsemi.MasterEmulator

And I get below error:

+ $MasterEmulatorInstance = New-Object Nordicsemi.MasterEmulator
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [New-Object],MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

The dll seems to be loaded correctly, I just can't get the constructor to work, I've checked other related posts but New-Object seems to be the only way to access a class from a .Net dll. Not sure what I'm doing wrong here, any hint would be appreciated, thanks.

Update: Thanks for the replies, I tried more methods of loading assembly like below.

[System.Reflection.Assembly]::LoadFrom($fullpath) | Out-Null

and also without Out-Null cmdlet

[System.Reflection.Assembly]::LoadFrom($fullpath)

the output shows

GAC    Version        Location                                                                                                                                                       
---    -------        --------                                                                                                                                                       
False  v4.0.30319     D:\Test\MasterEmulator.dll 

and still gives me the same error, I also tried below methods.

[reflection.assembly]::loadwithpartialname("MasterEmulator.dll") | Out-Null
Add-Type -Path $fullpath

and still no luck, all of these methods give me the exact same error when I call the constructor.

New-Object : 以 "0" 引數呼叫 ".ctor" 時發生例外狀況: "無法載入檔案或組件     'IronPython, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1' 或其相依性的其中之一。 系統找不到指定的檔案。"
位於 D:\Test\test.ps1:3 字元:27
+ $MasterEmulatorInstance = New-Object Nordicsemi.MasterEmulator
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [New-Object],MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

sorry about the Chinese in the error message since the system I'm running is in Chinese, any other ideas?

Allen
  • 63
  • 2
  • 8

1 Answers1

0

Try using LoadFrom. Here is how I load MSTranslitTools.DLL which does transliteration.

function load_transliterate(){
    $dll = Resolve-Path ".\tools\transliterate\MSTranslitTools.DLL"
    [System.Reflection.Assembly]::LoadFrom($dll) | out-null

    $specfile = Resolve-Path "Serbian Cyrillic to Latin.tms"      
    $tspec = [System.NaturalLanguage.Tools.TransliteratorSpecification]::FromSpecificationFile($specfile)

    $script:trans = [System.NaturalLanguage.Tools.Transliterator]::FromSpecification($tspec)

    Write-Verbose "Transliteration: $($trans.Input) -> $($trans.Output)"
}

Read about the difference here. See also this SO question.

Community
  • 1
  • 1
majkinetor
  • 8,730
  • 9
  • 54
  • 72