是否可以将脚本通过管道传输到 WScript? [英] Is it possible to pipe a script to WScript?

查看:45
本文介绍了是否可以将脚本通过管道传输到 WScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行由我的应用程序使用 Windows 脚本宿主(wscript.exe 或 cscript.exe)生成的简单 VBScript/JScript,而不生成临时脚本文件.

I would like to execute a simple VBScript/JScript generated by my application using the Windows Script Host (wscript.exe or cscript.exe) without generating a temporary script file.

我没有找到从标准输入流中读取脚本的命令行选项,但我确实在 WWW 中找到了几个记录 WScript 错误消息无法从标准输入读取脚本"的地方.

I did not find a command line option to read the script form the standard input stream, but i did find several places in the WWW that document a WScript error message "Can't read script from stdin."

MSDN Windows 脚本宿主参考 - 错误消息

那么是否有一个隐藏的命令行选项可以从标准中读取脚本?

So is there a hidden command line option to read the script from standard in?

目前我的程序生成一个小脚本并将其写入临时文件.然后它创建一个运行 cscript.exe 的子进程,等待它完成然后再次删除临时文件.如果可能的话,我想删除临时文件并将脚本直接传递给脚本宿主.(例如,将其通过管道传输到 cscript.exe 或将其作为命令行参数的一部分传递,类似于 cmd.exe 的/c 命令行选项)

Currently my program generates a small script and writes it to a temporary file. Then it creates a sub process running cscript.exe, waits for it to complete and then deletes the temporary file again. I would like to get rid of the temporary file and pass the script directly to the Script Host, if that's possible. (e.g. piping it to the cscript.exe or passing it as part of the command line arguments similar to the /c command line option of cmd.exe)

推荐答案

很遗憾,简短的回答是否定的.WScript 引擎不提供通过管道或命令参数传递脚本的方法,唯一的选择是使用 StdIn 来解释和处理命令 (在下面对此进行扩展).

Short answer is No unfortunately. The WScript Engine does not provide a way to pass script either piped or via the command arguments, the only option is using StdIn to interpret and process the commands (expand on this below).

我自己也不确定,但想测试一下...

Wasn't sure myself but wanted to test it out...

值得注意的是,下面的测试仍然需要一个脚本来解释输入,这可能不符合要求,但非常通用,我不会将其归类为临时脚本.

Worth noting that the testing below still requires a script to interpret the input which is probably not going to fit the requirement but is so generic I wouldn't class it as a temporary script.

我看到的问题是,您想要传递的代码越复杂,就越难通过管道传递到 cscript.exe 程序.

The issue I can see is the more complex the code you want to pass in the harder it will be to pipe through to the cscript.exe program.

这是一个简单的例子,它只是通过管道将 MsgBox() 调用传递给 cscript.exe.

Here is a simple example that just pipes a MsgBox() call to cscript.exe.

命令行:

echo MsgBox("test piping") | cscript.exe /nologo "test.vbs"

test.vbs

Dim stdin: Set stdin = WScript.StdIn
Dim line: line = stdin.ReadAll

Call ExecuteGlobal(line)

在命令行上处理多行

echo 中插入多行并不容易,如果您使用字符作为分隔符在传入命令后将命令分开,它可能会更好地工作.例如;>

Piping multiple lines from echo isn't going to be easy it may work better if you use a character as a delimiter to break the commands up after being passed in. So for example;

echo MsgBox("test piping")^^^^Msgbox("test Complete") | cscript.exe /nologo "test.vbs"

然后使用

Split(stdin.ReadAll, "^")

分解命令行以执行.

这是一个更复杂的例子;

Here is a more complex example;

命令行:

echo Dim i: i = 1^^^^i = i + 2^^^^MsgBox("i = " ^^^& i) | cscript /nologo "test.vbs"

test.vbs 的内容:

Dim line Dim stdin: Set stdin = WScript.StdIn

For Each line In Split(stdin.ReadAll, "^")   
  'Takes each line from StdIn and executes it.
  Call ExecuteGlobal(line)
Next

使用 ^ 作为分隔符为我自己的背部制作了一根杆,因为这在命令行中具有特殊含义并且需要转义(恰好是 ^),加上管道传输时必须再次转义的事实,因此 ^^^^.

Made a rod for my own back by using ^ as the delimiter as this has special meaning in the command line and needs to be escaped (which just so happens to be ^), plus the fact it has to be escaped again when piped hence the ^^^^.

意识到将行分解并单独执行可能会导致更多问题(例如,Function 定义会失败).这让我重新思考了这个方法,发现它应该更简单.

Realised that breaking the lines down and executing them individually could cause more problems (for example a Function definition would fail). This made me re-think the approach and found it should be even simpler.

Dim stdin: Set stdin = WScript.StdIn
Dim input: input = Replace(stdin.ReadAll, "^", vbCrLf)

Call ExecuteGlobal(input)

这篇关于是否可以将脚本通过管道传输到 WScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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