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

Question:

As described in my question Create ISO image using PowerShell: how to save IStream to file?, in PowerShell I create an IStream object as follows:

This object is of (PowerShell) type System.__ComObject. And somehow PowerShell knows that it is an IStream:

However, a cast to this type fails:

How do I make this conversion work, without using C# code?

Update: Apparently this conversion cannot be made to work, as x0n’s answer says.

Now, my goal is to pass this IStream COM object to some C# code (part of the same PowerShell script using Add-Type), where it would become a .NET object of type System.Runtime.InteropServices.ComTypes.IStream. Is that possible? If not, what alternatives do I have?

Answer:

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


In powershell after the add-type:

Source:

PowerShell: how to convert a COM object to an .NET interop type? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply