从 powershell 调用 ruby​​ 脚本 [英] Call ruby script from powershell

查看:68
本文介绍了从 powershell 调用 ruby​​ 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:带有此变量的 powershell 脚本:

i have following situation: a powershell script with this variables:

$RUBY = "C:\installing\ruby\bin\ruby.exe"
$ARGS = "C:\files\sript.rb $Arg1 $Arg2 $Arg3"

在批处理文件中我可以写

in a batch file i can write

%RUBY% %ARGS% 

它有效.

但是我如何在 powershell 中执行此操作?

but how can i do this in powershell?

ruby 安装路径可变很重要.

It is important that the ruby installing path is variable.

我用 Start-Process 尝试过,但只启动了 ruby​​.exe,没有脚本.

I tried it with Start-Process but only ruby.exe was started, without script.

有什么提示吗?

谢谢.

推荐答案

首先,$args 是一个 自动变量 由 PowerShell 管理,因此我会避免尝试使用相同的名称声明您自己的变量.

First, $args is an automatic variable managed by PowerShell, so I would avoid trying to declare your own variable with the same name.

要从 PowerShell 调用存储在变量中的命令,您可以使用 调用运算符 <代码>&:

To invoke a command stored in a variable from PowerShell, you can use the call operator &:

& $ruby 'script.rb'

如果您需要构建参数列表,我建议您创建一个数组,而不是将它们全部混合成一个字符串:

If you need to build up the list of arguments, I would recommend creating an array, instead of mashing them all together into a single string:

$rubyArgs = @('C:\files\script.rb', $Arg1, $Arg2, $Arg3)
& $ruby $rubyArgs

如果您需要传递更复杂的参数,您可能会发现这个答案很有用.

If you need to pass more complicated arguments, you may find this answer useful.

这篇关于从 powershell 调用 ruby​​ 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆