Skip to Content

Hello community,

in a normal case you can use the COM libraries, like the SAP ActiveX, very easily in PowerShell scripting language.

$SAPFunc = New-Object -ComObject “SAP.Functions”

This should be sufficient to use the SAP ActiveX libraries. But in this case you don’t have access to the members of the library.

001.JPG

At this point I use the way I described here, I use the helper functions. In this case you can use the ActiveX libraries, but you are missing the advantages of the Integrated Script Environment (ISE) of PowerShell, e.g. code completion.

To compensate this disadvantage I create with the type library importer (tlbimp) of the Microsoft .NET SDK an interoperability library.

[Reflection.Assembly]::LoadFile(“C:\Dummy\Interop.wdtfuncs.dll”)
$SAPFunc = New-Object Interop.wdtfuncs.SAPFunctionsClass

Now it works all as expected.

002.JPG

So far so good, but the next problem is in front of the door.

In a normal case you use the Connection property in the next step.

$Connection = $SAPFunc.Connection

It works, you get the object, but not all members.

003.JPG

Now I do the same steps with wdtlog.ocx and want to cast the object variable on different ways into the connection class type, but without success.

[Interop.wdtlog.ConnectionClass]$Connection = $SAPFunc.Connection
$Connection = [Interop.wdtlog.ConnectionClass]$SAPFunc.Connection
$Connection -As [Interop.wdtlog.ConnectionClass]
$Connection -Is [Interop.wdtlog.ConnectionClass]

As far as I can see it is not possible to cast a System.__ComObject to another in PowerShell.

Maybe someone has a solution for this problem.

Another insight: It is not in any case possible to use reference variables with COM libraries. If you work with an interoperability library it is as well not possible.

From these points it seems reasonable to use NCo, which I described here and here.
2015/09.20 – Addendum: The casting method that I described here doesn’t work also. It crashes PowerShell with the errors System.AccessViolationException and System.RuntimeType.ForwardCallToInvokeMember at the point where the first property is assigned..

#-Begin-----------------------------------------------------------------

  #-Sub Main------------------------------------------------------------
    Function Main() {

      $Path = $PSScriptRoot
      [Reflection.Assembly]::LoadFile($Path + "\wdtlog.dll")
      [Reflection.Assembly]::LoadFile($Path + "\wdtfuncs.dll")
      [Reflection.Assembly]::LoadFile($Path + "\wdtaocx.dll")

      $SAPFunc = New-Object wdtfuncs.SAPFunctionsClass
      If ($SAPFunc -eq $Null) {
        Break
      }

      $SAPFunc.AboutBox()

      $Connection = $SAPFunc.Connection
      If ($Connection -eq $Null) {
        Break
      }

      [wdtlog.Connection]$Connection =
        [System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($Connection,
        [wdtlog.ConnectionClass])

      $Connection.Client = "001"
      $Connection.User = "BCUSER"
      $Connection.Password = "minisap"
      $Connection.Language = "EN"
      $Connection.System = "NSP"
      $Connection.HostName = "ABAP702"
      $Connection.SystemNumber = 0

      $SAPConnection = $Connection.Logon(0, -1)
      If ($SAPConnection -ne 0) {
        Write-Host "Check connection in SAP system with transactin SMGW"
      }

    }

  #-Main----------------------------------------------------------------
    Main

#-End-------------------------------------------------------------------

 

 

Cheers
Stefan

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply