将脚本作为字符串传递给 cscript/wscript
Pass script to cscript/wscript as string
我想知道是否可以将脚本作为字符串传递给 cscript/wscript,类似于 PowerShell 的 -Command
选项。或者,还有另一种方法可以从 cmd 执行 VBS 脚本吗?
cscript/wscript的文档没有列出这样的选项,但由于我对Windows脚本不是很熟悉,我想知道我是否遗漏了什么。
谢谢!
许多(脚本)语言都有 R(ead)-E(valuate)-P(rint)-L(oop) - 工具 and/or 处理命令行上给出的字符串。所以使用 Powershell 你可以这样做:
powershell -command (2+3)*10
50
Perl:
perl -e "print 'hello';"
hello
对于 VBScript,您必须自己编写;也许开始 here or here(ff).
一个非常简单的(概念验证)脚本,仅处理来自命令行的代码:
Option Explicit
Dim goWAN : Set goWAN = WScript.Arguments.Named
Dim goWAU : Set goWAU = WScript.Arguments.UnNamed
If goWAN.Count <> 1 Or goWAU.Count <> 1 Or Not (goWAN.Exists("x") Or goWAN.Exists("e")) Then
WScript.Echo "usage: ", WScript.ScriptName, "/x|/e ""code to execute/evaluate"" (use ' for "")"
Else
Dim sCode : sCode = Replace(goWAU(0), "'", """")
If goWAN.Exists("x") Then
Execute sCode
Else
WScript.Echo Eval(sCode)
End If
End If
输出:
cscript 29416456.vbs
usage: 29416456.vbs /x|/e "code to execute/evaluate" (use ' for ")
cscript 29416456.vbs /e "(2+3)*10"
50
cscript 29416456.vbs /x "WScript.Echo 'Now: ' & Now"
Now: 4/3/2015 10:53:49 PM
一种方法是通过 echo > 从 cmd 写入一个 vbs 文件,然后执行它,然后删除它。
示例:
echo WScript.Echo WScript.CreateObject("WScript.Shell").SpecialFolders(WScript.Arguments(0)) > test.vbs
cscript test.vbs "Desktop" //nologo
del test.vbs
我想知道是否可以将脚本作为字符串传递给 cscript/wscript,类似于 PowerShell 的 -Command
选项。或者,还有另一种方法可以从 cmd 执行 VBS 脚本吗?
cscript/wscript的文档没有列出这样的选项,但由于我对Windows脚本不是很熟悉,我想知道我是否遗漏了什么。
谢谢!
许多(脚本)语言都有 R(ead)-E(valuate)-P(rint)-L(oop) - 工具 and/or 处理命令行上给出的字符串。所以使用 Powershell 你可以这样做:
powershell -command (2+3)*10
50
Perl:
perl -e "print 'hello';"
hello
对于 VBScript,您必须自己编写;也许开始 here or here(ff).
一个非常简单的(概念验证)脚本,仅处理来自命令行的代码:
Option Explicit
Dim goWAN : Set goWAN = WScript.Arguments.Named
Dim goWAU : Set goWAU = WScript.Arguments.UnNamed
If goWAN.Count <> 1 Or goWAU.Count <> 1 Or Not (goWAN.Exists("x") Or goWAN.Exists("e")) Then
WScript.Echo "usage: ", WScript.ScriptName, "/x|/e ""code to execute/evaluate"" (use ' for "")"
Else
Dim sCode : sCode = Replace(goWAU(0), "'", """")
If goWAN.Exists("x") Then
Execute sCode
Else
WScript.Echo Eval(sCode)
End If
End If
输出:
cscript 29416456.vbs
usage: 29416456.vbs /x|/e "code to execute/evaluate" (use ' for ")
cscript 29416456.vbs /e "(2+3)*10"
50
cscript 29416456.vbs /x "WScript.Echo 'Now: ' & Now"
Now: 4/3/2015 10:53:49 PM
一种方法是通过 echo > 从 cmd 写入一个 vbs 文件,然后执行它,然后删除它。
示例:
echo WScript.Echo WScript.CreateObject("WScript.Shell").SpecialFolders(WScript.Arguments(0)) > test.vbs
cscript test.vbs "Desktop" //nologo
del test.vbs