Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1.  
  2.  
  3. # スクリプト実行パスを取得する関数
  4. # バージョンによって取得する情報を変更する
  5. function Get-ScriptDir() {
  6. if( $PSVersionTable.PSVersion.Major -ge 3) {
  7. # Data from $PSScriptRoot
  8. $ScriptDir = $PSScriptRoot
  9. }
  10. else {
  11. # Data from $MyInvocation.MyCommand.Path
  12. $ScriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
  13. }
  14. return $ScriptDir
  15. }
  16.  
  17.  
  18. # NetWeaver .Net Connectorのモジュールをロードする関数
  19. function Load-Nco {
  20. $ScriptDir = Get-ScriptDir
  21.  
  22. $Size = [System.IntPtr]::Size
  23. if ($Size -eq 4) {
  24. $Path = $ScriptDir + "\x86\"
  25. }
  26. elseif ($Size -eq 8) {
  27. $Path = $ScriptDir + "\x64\"
  28. }
  29.  
  30. [Reflection.Assembly]::LoadFile($Path + "sapnco.dll") > $Null
  31. [Reflection.Assembly]::LoadFile($Path + "sapnco_utils.dll") > $Null
  32. }
  33.  
  34. # -----
  35. # SAPへの接続先を設定する
  36. # -----
  37. Function Get-Destination {
  38. $cfgParams = New-Object SAP.Middleware.Connector.RfcConfigParameters
  39. $cfgParams.Add("NAME", "TEST")
  40. $cfgParams.Add("ASHOST", "ABAP")
  41. $cfgParams.Add("SYSNR", "00")
  42. $cfgParams.Add("CLIENT", "400")
  43. $cfgParams.Add("USER", "User")
  44.  
  45. $secPasswd = Read-Host -Prompt "Password" -AsSecureString
  46. $ptrPasswd = [Runtime.InteropServices.Marshal]::SecureStringToBStr($secPasswd)
  47. $passwd = [Runtime.InteropServices.Marshal]::PtrToStringBStr($ptrPasswd)
  48. $cfgParams.Add("PASSWD", $passwd)
  49.  
  50. $cfgParams.Add
  51. Return [SAP.Middleware.Connector.RfcDestinationManager]::GetDestination($cfgParams)
  52. }
  53.  
  54. # ----
  55. # SAP汎用モジュールを実行する
  56. # ----
  57. Function Invoke-SAPFunctionModule {
  58. $destination = Get-Destination
  59.  
  60. # Metadata
  61. Try {
  62. [SAP.Middleware.Connector.IRfcFunction]$bapiCreateUser = $destination.Repository.CreateFunction("BAPI_USER_CREATE1")
  63. [SAP.Middleware.Connector.IRfcFunction]$bapiTransactionCommit = $destination.Repository.CreateFunction("BAPI_TRANSACTION_COMMIT")
  64. }
  65. Catch [SAP.Middleware.Connector.RfcBaseException] {
  66. Write-Host "Failed"
  67. Break
  68. }
  69.  
  70. # Set Import Parameters
  71. $bapiCreateUser.SetValue("USERNAME", "MYUSER")
  72. [SAP.Middleware.Connector.IRfcStructure]$imPassword = $bapiCreateUser.GetStructure("PASSWORD")
  73. $imPassword.SetValue("BAPIPWD", "initial")
  74. [SAP.Middleware.Connector.IRfcStructure]$imAddress = $bapiCreateUser.GetStructure("ADDRESS")
  75. $imAddress.SetValue("FIRSTNAME", "My")
  76. $imAddress.SetValue("LASTNAME", "User")
  77. $imAddress.SetValue("FULLNAME", "MyUser")
  78.  
  79. # Open context
  80. [SAP.Middleware.Connector.RfcSessionManager]::BeginContext($destination) > $Null
  81.  
  82. # 汎用モジュール呼び出し
  83. Try {
  84. # ユーザ作成
  85. $bapiCreateUser.Invoke($destination)
  86. [SAP.Middleware.Connector.IRfcTable]$return = $bapiCreateUser.GetTable("RETURN")
  87. ForEach ($line in $return) {
  88. Write-Host $line.GetValue("TYPE") "-" $line.GetValue("MESSAGE")
  89. }
  90. # コミット
  91. $bapiTransactionCommit.Invoke($destination)
  92. }
  93. Finally {
  94. # クローズ
  95. [SAP.Middleware.Connector.RfcSessionManager]::EndContext($destination) > $Null
  96. }
  97. }
  98.  
  99.  
  100. # ----
  101. # メイン関数
  102. # ----
  103. Function Main() {
  104. Load-NCo
  105. Invoke-SAPFunctionModule
  106. }
  107.  
  108.  
  109. # ------------------------------
  110. # Main
  111. # ------------------------------
  112.  
  113. Main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement