PowerShell: how to convert a COM object to an .NET interop type? PowerShell: how to convert a COM object to an .NET interop type? powershell powershell

PowerShell: how to convert a COM object to an .NET interop type?


You can try to pass $is in a (unsafe) c# method like an objecttype and try to handle it with a VARdeclared as System.Runtime.InteropServices.ComTypes.IStream

public unsafe static class MyClass{    public static void MyMethod(object Stream)     {       var i = Stream as System.Runtime.InteropServices.ComTypes.IStream;     //do something with i like i.read(...) and i.write(...)    }}

In powershell after the add-type:

[MyClass]::MyMethod($is)


You can't make this work. PowerShell uses a transparent "com adapter" layer that prevents this from working, but enables late binding in script. For the majority of cases this a Good Thing, but not in yours.