views:

28

answers:

2

Hi, I have COM object with few constructors, i'm trying to create new instance and passing parameters to the constructor using powershell 2.0, but i'm getting this error: Parameter set cannot be resolved using the specified named parameters.

This is the code:

$paramsInfo = New-Object -ComObject 'MyObject.ObjectA' 
$comObj = New-Object -ComObject 'MyObject.ObjectB'  -ArgumentList  $paramsInfo

This code doesn't work!!! Any help?

A: 

For a COM object, try using the -Property hashtable instead of -ArgumentList.

$comObj = new-object -com 'MyObject.ObjectB' -Property @{propToSet=$paramsInfo}

Ref: http://technet.microsoft.com/en-us/library/dd315334.aspx

David Longnecker
A: 

COM has no concept of a constructor. COM objects are created with the CoCreateInstance() API function, it has no way to pass any kind of arguments to the class factory. While many languages support making their classes visible to COM clients (like .NET's [ComVisible] attribute), only their parameterless constructor will ever be called.

Hans Passant