none
COM Interfaces RRS feed

  • Question

  • I have a third party COM server API that I would like to use in PowerShell. I can use it in C#/Visual Studio, the types are recognized. However in PowerShell I cannot make it work. If I use New-Object - ComObject to instantiate the entry point, it is of type System.__ComObject. If I use the Interop (Add-Type -Path <Interop.dll>),  I get an error that there is no appropriate constructor.

    Opening the interop in ILDASM, I see that entry point is defined as an interface. So the error from PowerShell makes sense, however why does it work in the C# Console App. The maker of the API only gives a C# example. Opening the tlb in OLEViewer, the same call is a coclass that implements an interface, the coclass itself has no members.

    I do not understand enough about COM interop or the RCW to see why C#/Visual Studio can interpret the types however Powershell cannot.

    Thursday, November 21, 2019 4:06 PM

All replies

  • Without a type library the properties and methods will not be visible. Either add a TLB or learn to use "Invoke" to access properties and methods.

    Also newer COM objects may not be browsable but you can try to access the properties direct like any object.


    \_(ツ)_/


    • Edited by jrvModerator Thursday, November 21, 2019 4:13 PM
    Thursday, November 21, 2019 4:12 PM
    Moderator
  • I do have the tlb and it is registered in Windows. New-Object -ComObject works in Powershell and in VS I can add the reference selecting from the COM dialog.

    I tried InvokeMember, however I get an error that the method or property does not exist.

    Both Powershell and VS are pointing to the same tlb, yet VS is doing something differently since it can recognize the types.

    Thursday, November 21, 2019 4:28 PM
  • Not all COM objects are "scriptable". They have to be designed specifically for scripting. Contact the vendor for the object to see if they have a version for scripting.

    You might be able to use direct C# methods to activate the COM object.

    Without a copy it I hard to know.  Post in the VS forum to get more info on scriptable COM.


    \_(ツ)_/

    Thursday, November 21, 2019 4:54 PM
    Moderator
  • Try this to be sure your invoke was correct.

    Function Invoke-ComObject{
        Param(
            [Parameter(Mandatory)]
            $ComObject,
            [Switch]$Method, 
            [Parameter(Mandatory)]
            [String]$Property,
            $Value
        )
        
        if($ComObject -IsNot '__ComObject'){
            If(!$ComInvoke){
                $Global:ComInvoke = @{}
            }
            If(!$ComInvoke.$ComObject){
                $ComInvoke.$ComObject = New-Object -ComObject $ComObject
            }
            $ComObject = $ComInvoke.$ComObject
        }
        
        if($Method){
            $Invoke = 'InvokeMethod'
        }elseif($Value){
            $Invoke = 'SetProperty'
        }else{
            $Invoke = 'GetProperty'
        }
        [__ComObject].InvokeMember($Property, $Invoke, $Null, $ComObject, $Value)
    }
    


    \_(ツ)_/

    Thursday, November 21, 2019 4:56 PM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, November 25, 2019 8:43 AM
    Moderator