Add arguments (variables) when calling a -ComClient from Powershell

0

I am quite new dealing with COM libraries and so on and I have step into a problem that I don't know how to solve.

I have a code in Python using a library that I would like to transform into Powershell.

So I have a COM library I want to use. I have successfully call it using python win32com.client.Dispatch() library. Some of the procedures only need to be called with no arguments and they work quite straight-forward in both, python and powershell. However if the procedure needs some argument (variable) I haven't been able to make it work in Powershell.

For example:

I would like to call some procedures that needs arguments, exactly the procedure I want to use is Compute_CurrentPlan() which has two compulsory arguments:

  • nmsg (long), the number of returned messages
  • msg() (string), array of messages returned from the procedure

According to get members the input arguments are Compute_CurrentPlan Method bool Compute_CurrentPlan (int, SAFEARRAY(string), bool)

I overcome this problem in Python using RC.Compute_CurrentPlan(nmsg=pythoncom.Missing, Msg=pythoncom.Missing), but I have not found a way to overcome this problem in Powershell.

So far I have tried this three command.

PS C:\> $obj.Compute_CurrentPlan()
Cannot find an overload for "Compute_CurrentPlan" and the argument count: "0".
At line:1 char:1
+ $obj.Compute_CurrentPlan()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

I also have tried:

PS C:\> $obj.Compute_CurrentPlan([System.Int32]$nmsg, [System.String]$msg)
Argument: '1' should be a System.Management.Automation.PSReference. Use [ref].
At line:1 char:1
+ $obj.Compute_CurrentPlan([System.Int32]$nmsg, [System.String]$msg)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : NonRefArgumentToRefParameterMsg

And this one:

PS C:\> $obj.Compute_CurrentPlan([ref]$nmsg,[ref]$msg)
Exception calling "Compute_CurrentPlan" with "2" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
At line:1 char:1
+ $obj.Compute_CurrentPlan([ref]$nmsg,[ref]$msg)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Being totally unsuccessful. Any suggestion?

python
powershell
com
asked on Stack Overflow May 2, 2017 by Ricardo • edited May 3, 2017 by Ricardo

1 Answer

0

First you need to find out what methods are exposed by the $obj object try {code} $obj | gm -MemberType Methods {code} This should give you a list of methods and expected parameter types etc.

answered on Stack Overflow May 2, 2017 by Raj

User contributions licensed under CC BY-SA 3.0