3

I have a COM object with a method that I would like to create and call from Powershell.

I know from the source code I know that the method signature is:

private DataSet MethodName(string connectionString, int userID, string location, int specID)

When I create the object in Powershell:

$obj = New-Object -ComObject My.ComObjectClass
$obj | Get-Member

The signature looks like:

MethodName               Method     _Recordset_Deprecated MethodName (SAFEARRAY(Variant))   

So I create an array and pass it to the method:

$MyArgs = "data source=Server;Initial catalog=kickassDB",1234,"flavortown",56
$obj.MethodName([ref]$MyArgs)

But this doesn't work. I get an error:

Exception calling "MethodName" with "1" argument(s): "Type mismatch"

I think my problem is that my array is full of regular string and integer values and I need to create VARIANT's, but I don't know how to go about doing that in Powershell.

Any ideas?

Bill Hurt
  • 749
  • 1
  • 8
  • 26
  • Using this syntax to create $MyArgs, creates an Array of Variants by default! What if you just ignore the signature and how powershell shows it. What if you just call $Obj.MethodeName("data source=Server;Initial catalog=kickassDB",1234,"flavortown",56) ? – xMRi May 05 '15 at 08:38
  • @xMRi Tried that. Didn't work. – Bill Hurt May 05 '15 at 13:17
  • The exception text in the body of the question is almost all I get. Exception calling "MethodName" with "1" argument(s): "Type mismatch" At U:\testScript.ps1:4 char:15 + $obj.MethodName <<<< ([ref]$myArgs) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation – Bill Hurt May 05 '15 at 14:12

1 Answers1

0

I know this is a very old post, but just updating it since I was running into a similar situation and found this thread. If someone else stumbles onto this, they may find my solution helpful.

I was receiving the following error when attempting to use the write method of the HTMLFile COM object under PowerShell v7. This was not an issue with PowerShell v5. With PS5, the method was expecting a System.Object[] parameter. With PS7, it expects a SAFEARRAY(Variant) parameter.

"Type mismatch. (0x80020005 (DISP_E_TYPEMISMATCH))"

I was able to solve my issue using jedigo's solution here and supplying a byte array instead of a string. So the code which ended up working was similar to:

$H = New-Object -ComObject "HTMLFile"
$S = '<p>Some Text</p>'
$B = [System.Text.Encoding]::Unicode.GetBytes($S)
$H.write($B)