How to specify a custom COM-enum as a parameter of a PowerShell method

How to create a link for an enumeration declared inside a COM object, pass a method that requires it? In particular, there is a method SetHomeDiron a third-party COM object that accepts an enumeration as the only parameter. The definition that the parameter expects is basically:

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

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

$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)"

The following are search results for 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 provides for 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

. PowerShell 2.0 Windows Server 2008 R2, Windows Management Framework .

: Visual Studio, - .

COM Object in Visual Studio Object Explorer

+4
4

# COM. int case, . , Powershell COM- .

Globalscape , , , .

+3

, , 32- 64- .

:

32- C:\Windows\SysWOW64\WindowsPowerShell\ , and.

0

com, :

Powershell 5, ( Powershell - - 5):

Enum SFTPAdvBool
{
        abFalse = 0
        abTrue = 1
        abInherited = -2
}

:

$obj.SetHomeDir([SFTPAdvBool]::abTrue)

for anything older than PS 5 you can try:

$code = @"

namespace SFTPCOMINTERFACELib {

    public enum SFTPAdvBool {

        abFalse = 0,
        abTrue = 1,
        abInherited = -2

    }

}
"@

Add-Type -TypeDefinition $code -Language CSharpVersion3

And the call:

$obj.SetHomeDir([SFTPCOMINTERFACELib.SFTPAdvBool]::abTrue)

This will basically create a C # enumeration and add it as a type in Powershell.

0
source

All Articles