Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Stefan-Schnell
Active Contributor
0 Kudos
2,596
I presented here the possibility to use PowerShell via dotNET Connector (NCo) with SAP.

Here now two functions to simplify the using of PowerShell with NCo.

The first function shows how to load NCo in relation to the bus width of the PowerShell. The function gets the directory of the script, set it as current directory, expects the NCo libraries in subdirectories x64 for the 64-bit version and x86 for the 32-bit version and loads it.
  #-Function Load-NCo---------------------------------------------------
#-
#- The loading of the NCo libraries is in dependence to the shell.
#- The 64-bit version loads the libraries from the path x64, and the
#- 32-bit version from the path x86.
#-
#---------------------------------------------------------------------
Function Load-NCo {

$CurrentDir = Get-Location
[System.IO.Directory]::SetCurrentDirectory($CurrentDir.Path)

$Size = [System.IntPtr]::Size
if ($Size -eq 4) {
$Path = $CurrentDir.Path + "\x86\"
}
elseif ($Size -eq 😎 {
$Path = $CurrentDir.Path + "\x64\"
}

$rc = [Reflection.Assembly]::LoadFile($Path + "sapnco.dll")
$rc = [Reflection.Assembly]::LoadFile($Path + "sapnco_utils.dll")

}

 

The second function shows how to get a destination with a password requester.
  #-Function Get-Destination--------------------------------------------
Function Get-Destination {

#-Verbindungsparamter---------------------------------------------
$cfgParams = New-Object SAP.Middleware.Connector.RfcConfigParameters
$cfgParams.Add("NAME", "TEST")
$cfgParams.Add("ASHOST", "ABAP")
$cfgParams.Add("SYSNR", "00")
$cfgParams.Add("CLIENT", "001")
$cfgParams.Add("USER", "BCUSER")

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

return [SAP.Middleware.Connector.RfcDestinationManager]::GetDestination($cfgParams)

}

 

 



These two functions changes the examples a little bit.
#-Begin-----------------------------------------------------------------

#-Function Load-NCo---------------------------------------------------
Function Load-NCo {

$CurrentDir = Get-Location
[System.IO.Directory]::SetCurrentDirectory($CurrentDir.Path)

$Size = [System.IntPtr]::Size
if ($Size -eq 4) {
$Path = $CurrentDir.Path + "\x86\"
}
elseif ($Size -eq 😎 {
$Path = $CurrentDir.Path + "\x64\"
}

$rc = [Reflection.Assembly]::LoadFile($Path + "sapnco.dll")
$rc = [Reflection.Assembly]::LoadFile($Path + "sapnco_utils.dll")

}

#-Function Get-Destination--------------------------------------------
Function Get-Destination {

#-Connectionparamter----------------------------------------------
$cfgParams = New-Object SAP.Middleware.Connector.RfcConfigParameters
$cfgParams.Add("NAME", "TEST")
$cfgParams.Add("ASHOST", "ABAP")
$cfgParams.Add("SYSNR", "00")
$cfgParams.Add("CLIENT", "001")
$cfgParams.Add("USER", "BCUSER")
$cfgParams.Add("PASSWD", "minisap")

return [SAP.Middleware.Connector.RfcDestinationManager]::GetDestination($cfgParams)

}

#-Function Invoke-SAPFunctionModule-----------------------------------
Function Invoke-SAPFunctionModule {

$destination = Get-Destination

#-Metadata--------------------------------------------------------
[SAP.Middleware.Connector.IRfcFunction]$rfcFunction =
$destination.Repository.CreateFunction("STFC_CONNECTION")

#-Set importparameter---------------------------------------------
$rfcFunction.SetValue("REQUTEXT", "Hello World from PowerShell")

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

#-Show exportparameter--------------------------------------------
Write-Host $rfcFunction.GetValue("ECHOTEXT")
Write-Host $rfcFunction.GetValue("RESPTEXT")

}

#-Main----------------------------------------------------------------
Load-NCo
Invoke-SAPFunctionModule

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

 
Labels in this area