Search code examples
powershellenumscom

How to specify custom COM enum as PowerShell method parameter


How can I create a reference for a enum declared inside a COM object to pass to a method that requires it? Specifically, there is a method SetHomeDir on a 3rd-party COM object that takes in an enum as the only parameter. The definition that parameter expects is basically:

typedef enum
{
  abFalse = 0,
  abTrue = 1,
  abInherited = -2
} SFTPAdvBool;

It appears to be defined somewhere in the COM object, but not as an object that I can create with New-Object. I have tried the following and they all give the same MX Error: 7 response:

$obj.SetHomeDir($true)
$obj.SetHomeDir(1)
$obj.SetHomeDir([Object]1)
$obj.SetHomeDir([Int16]1)

Exception calling "SetHomeDir" with "1" argument(s): "MX Error: 7 (00000007)"

Here are the results from trying some other approaches:

PS C:\> New-Object -ComObject SFTPCOMINTERFACELib.SFTPAdvBool
New-Object : Cannot load COM type SFTPCOMINTERFACELib.SFTPAdvBool.

PS C:\> [SFTPAdvBool]::abTrue
Unable to find type [SFTPAdvBool]: 
make sure that the assembly containing this type is loaded.

PS C:\> [SFTPCOMINTERFACELib.SFTPAdvBool]::abTrue
Unable to find type [SFTPCOMINTERFACELib.SFTPAdvBool]: 
make sure that the assembly containing this type is loaded.

The method signature that COM exposes to PowerShell looks like this:

PS C:\> $user.SetHomeDir

MemberType          : Method
OverloadDefinitions : {void SetHomeDir (SFTPAdvBool)}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : void SetHomeDir (SFTPAdvBool)
Name                : SetHomeDir
IsInstance          : True

Note: This is running under PowerShell 2.0 on Windows Server 2008 R2, but I would consider upgrading the Windows Management Framework to a newer version if necessary.

Update: Here is a screenshot from the Visual Studio object explorer, in case that offers up any clues.

COM Object in Visual Studio Object Explorer


Solution

  • We ended up using a compiled C# interop wrapper around the COM object. I was able to specify an int as the parameter and just used a case statement to pass the correct value from the enum. As far as I can tell there isn't a way to do this directly from Powershell and requires wrapping the COM object in managed code.

    We have opened a dialog with Globalscape and hopefully this will be something they address in a future release.