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

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

问题描述

无论用户尝试什么,强制 VBS 使用 cscript 而不是 wscript 运行的 stackoverflow 批准(因此是正确的)方法是什么?

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

我很欣赏这可能很容易修改以处理参数,但意识到这可能不是解决问题的最佳方法.

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.

它唯一不做的就是测试 StdIn(例如,如果有人通过命令行将某些内容通过管道传输到脚本,但忘记使用cscript script.vbs")——但如果它被执行了通过 WScript.exe,WScript.StdIn 的方法都返回 Invalid Handle 错误,因此无论如何都无法对其进行测试.

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天全站免登陆