2
votes

Is it possible to run a VBScript from Ruby? I have several scripts to automate some tasks in SAP systems and I'm currently running them from Excel.

I'd like to move this to Ruby, where my other modules are in place. Is this possible, and if so, how can I accomplish this?

EDIT

More background info:

I'm using VBScripts to perform some repetitive daily tasks in certain SAP transactions. I have to carry some data to and from an SAP system. I've integrated the VBScripts to VBA and I run them via Excel where I also parse the data if necessary. I'm passing some variables (users, transactions etc.) to the scripts as well.

Now I need to move this part from the Excel to my Ruby app and trigger these VBScripts there and also collect the returned data and use them in the Ruby app.

@nmiranda: I've gone through a lot of SCN resources (also the one you mentioned), but I can't use ES, because I don't have the appropriate user level for that. The other option is to use the RFC to connect to the system and perform the tasks (https://github.com/mydoghasworms/nwrfc) but I haven't had any success yet.

So I thought I'd try to run the VBScripts from Ruby as described above. However, doing it this way would also require me to pass those variables as arguments to the VBScripts.

Example:

  1. Run ruby script and pass the arguments to VBScript
  2. Run VBScript
  3. Perform tasks in the system
  4. Leave the system
  5. Parse the data received from the system (not always the case)

So far I’ve searched for possibilities to do this but I haven’t found the resources mentioned in the answer below. Also my knowledge on this is very limited, hence I’ve asked here.

Now I’ve tried the suggestion below and it works:)

require 'open3'
  stdin, stdout, stderr = Open3.popen3('cscript my_script.vbs')

I call the script above and this performs the tasks in the already open system.

Would this be the optimal way of doing it? And, is it possible to pass in the arguments to the VBScript?

I'm passing the argument like so:

require 'open3'
  stdin, stdout, stderr = Open3.popen3('cscript my_script.vbs 0001')

And capturing them like so in my VBScript:

WScript.Arguments(0)

Is this the correct way? It seems to be working, I just need confirmation...

regards

seba

1
You want to run a VBScript that calls an SAP function? Isn't it better to make a call of an SAP function module directly from Ruby? Found this link in SCN: scn.sap.com/docs/DOC-18389. Check if it helps.Nelson Miranda
Welcome to Stack Overflow. What have you tried? There are numerous ways to run sub-shells and commands so listing them all would be out-of-scope for SO. Instead, tell us what you've tried and we'll help you fix it.the Tin Man
Please see the edit above.Sebastjan Hribar

1 Answers

2
votes

Based on "5 ways to run commands from ruby" I choose to use popen3:

master.rb:

require 'open3'
stdin, stdout, stderr = Open3.popen3('cscript ../vbs/slave.vbs')
puts stdout.readlines
puts stderr.readlines

slave.vbs:

WScript.StdOut.WriteLine "slave: stdout"
WScript.StdErr.WriteLine "slave: stderr"

output:

ruby master.rb
slave: stdout
slave: stderr

ruby -v
ruby 1.9.2p180 (2011-02-18) [i386-mingw32]