Force a VBS to run using cscript instead of wscript

前端 未结 5 1539
予麋鹿
予麋鹿 2020-12-29 07:42

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 G

相关标签:
5条回答
  • 2020-12-29 08:01

    One approach might be to give it another extension instead of .vbs. Say .cvbs for example. Associate .cvbs with cscript.exe not wscript.exe, that way executing or double clicking a .cvbs file will never invoke the wscript.exe.

    0 讨论(0)
  • 2020-12-29 08:05

    Here is my code snippet i use for some of my scripts. It handles Arguments as well. All you have to do is replace the {EnterWorC} with either a "w" or "c" WITH quotes

    Dim WorC, Command, Arguments, I
    
    WorC={EnterWOrC}  'Make sure you replace "{EnterWOrC}" with a "w" or a "c" and BE SURE TO PUT QUOTES AROUND THE LETTER.
    
    WorC=LCase (WorC)
    If lcase (WorC)="w" Or lcase (WorC)="c" Then
    
    If LCase (Right (WScript.FullName,11))<> WorC & "script.exe" Then
        command=WScript.ScriptFullName
        Arguments=""
        For I=0 To UBound (WScript.Arguments)
            Arguments=Arguments & Chr (34) & WScript.Arguments(I) & Chr (34) & Space (1)
        Next
        CreateObject("Wscript.Shell").Run WorC & "script.exe " & Chr (34) & command & Chr (34) & Space (1) & Arguments, 1
        WScript.Quit
    End If
        WorC=Empty
        Command=Empty
        I=Empty
        Arguments=Empty
    End If
    

    Here you will have to replace the 2nd line (2nd NON-blank line)

    WorC={EnterWOrC}  'Make sure you replace "{EnterWOrC}" with a "w" or a "c" and BE SURE TO PUT QUOTES AROUND THE LETTER.
    

    For Wscript: WorC="W"

    For CScript: WorC="C"

    It is NOT case Sensitive.

    0 讨论(0)
  • 2020-12-29 08:15

    Two small additions to forceCScriptExecution let me see its Window after termination and handle its return code.

    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
            **ret =** CreateObject( "WScript.Shell" ).Run **("cmd /k** cscript //nologo """ & WScript.ScriptFullName & """ " & Str**,1,true)**
            WScript.Quit **ret**
        End If
    End Sub
    

    Notes: "cmd /k" let the windows stay after execution. Parameter "1" activates the window. Parameter "true" waits for termination, so variable "ret" can return the error code.

    0 讨论(0)
  • 2020-12-29 08:16

    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
    

    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.

    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.

    0 讨论(0)
  • 2020-12-29 08:16

    Here's a similar one in JScript for making .js files run in CScript:

    (function(ws) {
      if (ws.fullName.slice(-12).toLowerCase() !== '\\cscript.exe') {
        var cmd = 'cscript.exe //nologo "' + ws.scriptFullName + '"';
        var args = ws.arguments;
        for (var i = 0, len = args.length; i < len; i++) {
          var arg = args(i);
          cmd += ' ' + (~arg.indexOf(' ') ? '"' + arg + '"' : arg);
        }
        new ActiveXObject('WScript.Shell').run(cmd);
        ws.quit();
      }
    })(WScript);
    
    WScript.echo('We are now in CScript. Press Enter to Quit...');
    WScript.stdIn.readLine();
    

    https://gist.github.com/4482361

    0 讨论(0)
提交回复
热议问题