Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

How To Use NCo With PowerShell 5 Classes Feature

I presented here the possibility to use NCo with Microsofts scripting language PowerShell. Here now examples how to use NCo with PowerShell 5 and the new classes language feature. You can find more information about classes in PowerShell here.

1st example – Ping:

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

  <#
    .Synopsis
      Calls PING of an SAP system.
    .Description
      Calls the function module RFC_PING and the build-in function
      Ping of the .NET connector.
  #>

  #-Function Get-Destination--------------------------------------------
    Function Get-Destination($cfgParams) {
      If (("SAP.Middleware.Connector.RfcDestinationManager" -as [type]) -ne $null){
        Return [SAP.Middleware.Connector.RfcDestinationManager]::GetDestination($cfgParams)
      }
    }

  #-Class NCo-----------------------------------------------------------
    Class NCo {

      #-Constructor-----------------------------------------------------
        NCo() {

          [String]$ScriptDir = $PSScriptRoot

          If ([Environment]::Is64BitProcess) {
            [String]$Path = $ScriptDir + "\x64\"
          }
          Else {
            [String]$Path = $ScriptDir + "\x86\"
          }

          [String]$File = $Path + "sapnco.dll"
          Add-Type -Path $File
          [String]$File = $Path + "sapnco_utils.dll"
          Add-Type -Path $File

        }

      #-Method GetDestination-------------------------------------------
        Hidden [Object]GetDestination() {

          #-Verbindungsparamter-----------------------------------------
            $cfgParams = `
              New-Object SAP.Middleware.Connector.RfcConfigParameters
            $cfgParams.Add($cfgParams::Name, "TEST")
            $cfgParams.Add($cfgParams::AppServerHost, "ABAP")
            $cfgParams.Add($cfgParams::SystemNumber, "00")
            $cfgParams.Add($cfgParams::Client, "001")
            $cfgParams.Add($cfgParams::User, "BCUSER")

            $SecPasswd = Read-Host -Prompt "Passwort" -AsSecureString
            $ptrPasswd = `
              [Runtime.InteropServices.Marshal]::SecureStringToBStr($SecPasswd)
            $Passwd = `
              [Runtime.InteropServices.Marshal]::PtrToStringBStr($ptrPasswd)
            $cfgParams.Add($cfgParams::Password, $Passwd)
 
          Return Get-Destination($cfgParams)

        }

      #-Method ExecutePing----------------------------------------------
        ExecutePing() {

          $destination = $This.GetDestination()

          #-Metadata----------------------------------------------------
            $rfcFunction = `
              $destination.Repository.CreateFunction("RFC_PING")

          #-Variant 1: Call function module-----------------------------
            Try {
              $rfcFunction.Invoke($destination)
              Write-Host "Ping successful"
            }
            Catch {
              Write-Host "Exception" $_.Exception.Message "occured"
            }

          #-Variant 2: Call build-in function---------------------------
            Try {
              $destination.Ping()
              Write-Host "Ping successful"
            }
            Catch {
              Write-Host "Exception" $_.Exception.Message "occured"
            }

        }

    }

  #-Sub Main------------------------------------------------------------
    Function Main () {
      $NCo = [NCo]::new()
      $NCo.ExecutePing()
    }

  #-Main----------------------------------------------------------------
    If ($PSVersionTable.PSVersion.Major -ge 5) {
      Main
    }

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

2nd example – RFC_SYSTEM_INFO:

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

  <#
    .Synopsis
      Calls RFC_SYSTEM_INFO of an SAP system.
    .Description
      Calls the function module RFC_SYSTEM_INFO and writes the result
      on the screen.
  #>

  #-Function Get-Destination--------------------------------------------
    Function Get-Destination($cfgParams) {
      If (("SAP.Middleware.Connector.RfcDestinationManager" -as [type]) -ne $null){
        Return [SAP.Middleware.Connector.RfcDestinationManager]::GetDestination($cfgParams)
      }
    }

  #-Class NCo-----------------------------------------------------------
    Class NCo {

      #-Constructor-----------------------------------------------------
        NCo() {

          [String]$ScriptDir = $PSScriptRoot

          If ([Environment]::Is64BitProcess) {
            [String]$Path = $ScriptDir + "\x64\"
          }
          Else {
            [String]$Path = $ScriptDir + "\x86\"
          }

          [String]$File = $Path + "sapnco.dll"
          Add-Type -Path $File
          [String]$File = $Path + "sapnco_utils.dll"
          Add-Type -Path $File

        }

      #-Method GetDestination-------------------------------------------
        Hidden [Object]GetDestination() {

          #-Verbindungsparamter-----------------------------------------
            $cfgParams = `
              New-Object SAP.Middleware.Connector.RfcConfigParameters
            $cfgParams.Add($cfgParams::Name, "TEST")
            $cfgParams.Add($cfgParams::AppServerHost, "ABAP")
            $cfgParams.Add($cfgParams::SystemNumber, "00")
            $cfgParams.Add($cfgParams::Client, "001")
            $cfgParams.Add($cfgParams::User, "BCUSER")

            $SecPasswd = Read-Host -Prompt "Passwort" -AsSecureString
            $ptrPasswd = `
              [Runtime.InteropServices.Marshal]::SecureStringToBStr($SecPasswd)
            $Passwd = `
              [Runtime.InteropServices.Marshal]::PtrToStringBStr($ptrPasswd)
            $cfgParams.Add($cfgParams::Password, $Passwd)

          Return Get-Destination($cfgParams)

        }

      #-Method GetSystemInfo--------------------------------------------
        GetSystemInfo() {

          $destination = $This.GetDestination()

          #-Metadata----------------------------------------------------
            $rfcFunction = `
              $destination.Repository.CreateFunction("RFC_SYSTEM_INFO")

          #-Call function module----------------------------------------
            Try {
              $rfcFunction.Invoke($destination)

              $Export = $rfcFunction.GetStructure("RFCSI_EXPORT")

              #-Get information-----------------------------------------
                Write-Host $Export.GetValue("RFCHOST")
                Write-Host $Export.GetValue("RFCSYSID")
                Write-Host $Export.GetValue("RFCDBHOST")
                Write-Host $Export.GetValue("RFCDBSYS")

            }
            Catch {
              Write-Host "Exception" $_.Exception.Message "occured"
            }

        }

    }

  #-Sub Main------------------------------------------------------------
    Function Main () {
      $NCo = [NCo]::new()
      $NCo.GetSystemInfo()
    }

  #-Main----------------------------------------------------------------
    If ($PSVersionTable.PSVersion.Major -ge 5) {
      Main
    }

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

 

 

RFC_SYSTEM_INFO.JPG

Hint: To bypassing unresolved errors of not available types inside the class I use a tiny trick, I externalize the code in a function – e.g. in this case Get-Destination.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.