【问题标题】:VBS to trigger another vbs via cmd in cscriptVBS 通过 cscript 中的 cmd 触发另一个 vbs
【发布时间】:2016-10-13 14:16:50
【问题描述】:

我有一个 VBScript,它触发 HP ALM 中的测试以使用 UFT 运行 - 这个 vbs 只能通过在cmdcscript 中触发它来工作。

我想知道如何避免先去cmd 触发它,而只是创建另一个vbs 来触发文件在cmdcscript 中运行。或者你有更好的解决方案。

下面的代码不起作用。

Set oShell = WScript.CreateObject ("WScript.Shell")

oShell.run "cmd.exe"   "" c:\Windows\SysWOW64\cscript.exe C:\Temp\Unattended.vbs""

Set oShell = Nothing

【问题讨论】:

    标签: vbscript hp-uft wsh


    【解决方案1】:

    以下是几个如何运行 VBS 的示例。如果您的系统默认运行 cscript,则最后一个选项应该可以工作。

    Set objShell = CreateObject("WScript.Shell")
    
    'run with wscript
    objShell.Run("""C:\Windows\System32\wscript.exe"" ""C:\Test\MyScript.vbs""")
    
    wscript.sleep 5000 'waits 5 seconds before running the next script. This is used for display purposes, not because you have to
    
    'run with cscript
    objShell.Run("""C:\Windows\System32\cscript.exe"" ""C:\Test\My Script.vbs""")
    
    wscript.sleep 5000 'waits 5 seconds before running the next script. This is used for display purposes, not because you have to
    
    'run with the default program for vbs files (usually cscript)
    objShell.Run("""C:\Test\My Script.vbs""")
    

    【讨论】:

    • 请去掉括号。它们在这里不是无效的,而是don't do what most people would expect。此外,默认解释器是 wscript.exe,除非您将其更改为 cscript.exe
    • @AnsgarWiechers 真的吗?,它不是一个 Sub...为什么不直接做 result = objShell.Run("""C:\Windows\System32\cscript.exe"" ""C:\Test\My Script.vbs""") 并完成它(记得先Dim result
    • @Lankymart 那将是另一个(可以说是更好的)选择。如果你真的检查返回的退出代码,那就是。
    • @AnsgarWiechers 的事情是因为它不是 Sub 它在技术上不是无效的,因为 Run() 方法是 Function,所以括号很好,不会导致脚本失败。当你像这样调用Sub mySub(arg1)(arg1) 并不是你想象的那样。
    • @Lankymart 否。在这方面,函数和子函数是相似的。 myFunc(arg1)mySub(arg1) 都将作为独立语句工作。不是因为它是传递参数的正确语法,而是因为括号强制 arg1 按值传递。 myFunc(arg1, arg2)mySub(arg1, arg2) 都将失败,因为该语法仅在像这样使用时才有效:ret = myFunc(arg1, arg2)Call mySub(arg1, arg2) 等。否则它必须是 myFunc arg1, arg2mySub arg1, arg2(不带括号)两个子和功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多