dimanche 1 novembre 2015

Call a Native Assembly from PowerShell Using Reflection

I have a unique requirement to write a PowerShell script that works on PowerShell v1. I am trying to call the DhcpRequestParams function to determine the DHCP options presented from a DHCP server. Because I have to make this work on PowerShell v1, I cannot use C# code and add it with Add-Type.

I am not a .NET programmer, but I get by with a basic understanding. With that said, my research indicates that I have to use reflection and pInvoke this function from the DHCPCSvc.dll file.

I think my code is close to making this work, but I am getting an error: Exception calling "LoadFile" with "1" argument(s): "The module was expected to contain an assembly manifest.

(Exception from HRESULT: 0x80131018)"
At line:1 char:1
+ [Reflection.Assembly]::LoadFile("C:\Windows\System32\dhcpcsvc.dll")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : BadImageFormatException

Here is a link to my code on github: http://ift.tt/20mrv62

What am I missing here? I have been working on this all weekend and have hit a wall.





Aucun commentaire:

Enregistrer un commentaire