Running VBS script with Ruby

不打扰是莪最后的温柔 提交于 2019-12-11 03:23:48

问题


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:


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]


来源:https://stackoverflow.com/questions/31619268/running-vbs-script-with-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!