强制使用VBS而不是CSCRIPT的的WScript运行 [英] Force a VBS to run using cscript instead of wscript

查看:567
本文介绍了强制使用VBS而不是CSCRIPT的的WScript运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是计算器批准(因此正确的)方法来强制使用VBS而不是CSCRIPT的的WScript运行 - 无论什么样的用户尝试

What is the stackoverflow approved (and hence correct) method to force a VBS to run using cscript instead of wscript - irrespective of what the user tries?

一个快速谷歌搜索显示了大量的例子,但他们中的一些根本就没有工作,所以我很渴望知道那些做往往不处理,它可能已经带参数运行的事实,什么是最好的方法是

A quick Google search shows plenty of examples, but some of them simply don't work and those which do often don't handle the fact that it may have been run with arguments so I'm keen to know what the best way is.

下面是一个例子不处理参数:

Here is one example which doesn't handle arguments:

sExecutable = LCase(Mid(Wscript.FullName, InstrRev(Wscript.FullName,"\")+1))
If sExecutable <> "cscript.exe" Then
  Set oShell = CreateObject("wscript.shell")
  oShell.Run "cscript.exe """ & Wscript.ScriptFullName & """"
  Wscript.Quit
End If

我AP preciate,这也许可以很容易地修改,以处理参数,但要意识到这可能不是解决这个问题的最好办法。

I appreciate that this could probably be easily modified to handle arguments, but realise that this may not be the best way to approach the problem.

背景:我无论是从DOS批处理文件或一个计划任务写这可以通过双击或(最有可能)运行脚本。它可以包含一个或多个任选的命令行参数。

Background: I'm writing a script which can run by double clicking or (most likely) from either a DOS batch file or as a scheduled task. It can contain one or more optional command line arguments.

推荐答案

我的主啊,纯粹的垃圾。这让我哭看到这么眉头编码(没有冒犯任何人,大声笑)。说真的,不过,这是我的2便士:

My Lord, what unadulterated rubbish. It makes me cry to see such cruddy coding (no offense to anybody, lol). Seriously, though, here's my 2 pence:

Sub forceCScriptExecution
    Dim Arg, Str
    If Not LCase( Right( WScript.FullName, 12 ) ) = "\cscript.exe" Then
        For Each Arg In WScript.Arguments
            If InStr( Arg, " " ) Then Arg = """" & Arg & """"
            Str = Str & " " & Arg
        Next
        CreateObject( "WScript.Shell" ).Run _
            "cscript //nologo """ & _
            WScript.ScriptFullName & _
            """ " & Str
        WScript.Quit
    End If
End Sub
forceCScriptExecution

它处理参数,并检查在上述参数空间 - 所以,在传递到包含空格的原始脚本实例文件名的情况下,也不会得到记号化传递给CSCRIPT.EXE当

It handles arguments, AND checks for spaces in said arguments -- so that in the case of a filename passed to the original script instance that contained spaces, it wouldn't get "tokenized" when passed to cscript.exe.

只有它不会做的事情是为测试标准输入(例如,如果有人管道通过命令行的东西剧本,却忘了使用的cscript script.vbs的情况下) - 但如果它被执行死刑通过WScript.exe的,WScript.StdIn的方法都返回无效的处理错误,所以没有办法测试反正。

Only thing it doesn't do is test for StdIn (e.g., in the case where someone piped something to the script via the command line, but forgot to use "cscript script.vbs") -- but if it was executed by WScript.exe, WScript.StdIn's methods all return Invalid Handle errors, so there's no way to test that anyway.

随时让我知道如果有一种方法,以破这一点;我愿意在必要时加以改进。

Feel free to let me know if there's a way to "break" this; I'm willing to improve it if necessary.

这篇关于强制使用VBS而不是CSCRIPT的的WScript运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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