none
VB Script error - when try to execute a command on windows shell RRS feed

  • Question

  • Hi ,

    I had to create a scheduled task on a specific date - I tried using schtasks It worked well!.

    SCHTASKS /create /tn "Mytask" /tr "cscript "C:\Remove.vbs"" /sc ONCE /sd 08/02/2014 /st 14:25 /RL HIGHEST /RU SYSTEM

    But when I try to embed this in VBscript it throws me below error.

    Set oShell = WScript.CreateObject ("WScript.Shell")
    oShell.run "cmd.exe /C SCHTASKS /create /tn "Mytask1" /tr "cscript "C:\Remove.vbs"" /sc ONCE /sd 12/04/2015 /st 12:00 /RL HIGHEST /RU SYSTEM

    samp.vbs(6, 72) Microsoft VBScript compilation error: Expected end of statement

    Can you please help.


    Sai Krishna

    Saturday, August 2, 2014 12:40 PM

Answers

  • ' This script must be run elevated 
    'We Do Not need To use CMD With SCHTasks.  It Is a stand alone EXE.
    
    ' start with the command line as a minimum that works and double up on the required quotes.
    
    strCmd="SCHTASKS /create /tn Mytask1 /tr ""cscript C:\Remove.vbs"" /sc ONCE /sd 12/04/2015 /st 12:00 /RL HIGHEST /RU SYSTEM"
    result = oShell.run(strCmd, 1, True)
    ' check the result to see if the command was successful
    If result = 0 Then
         MsgBox "SUCCESS!"
    Else
         MsgBox "Task creation failed"
    End If



    ¯\_(ツ)_/¯




    • Edited by jrv Saturday, August 2, 2014 2:13 PM
    • Marked as answer by saikrishna_n Saturday, August 2, 2014 5:45 PM
    Saturday, August 2, 2014 2:11 PM

All replies

  • Your quotes are mismatched.


    ¯\_(ツ)_/¯

    Saturday, August 2, 2014 1:29 PM
  • You have several problems with your double quotes. Try this version:

    Set oShell = WScript.CreateObject ("WScript.Shell")
    oShell.run "cmd.exe /C SCHTASKS /create /tn ""Mytask1"""  _
        & " /tr cscript ""C:\Remove.vbs"" /sc ONCE /sd 12/04/2015"  _
        & " /st 12:00 /RL HIGHEST /RU SYSTEM", 1, true

    I also note that you are trying to keep script files in the root of C:. UAC under Vista and later versions of Windows will not allow this.

    Saturday, August 2, 2014 1:30 PM
  • ' This script must be run elevated 
    'We Do Not need To use CMD With SCHTasks.  It Is a stand alone EXE.
    
    ' start with the command line as a minimum that works and double up on the required quotes.
    
    strCmd="SCHTASKS /create /tn Mytask1 /tr ""cscript C:\Remove.vbs"" /sc ONCE /sd 12/04/2015 /st 12:00 /RL HIGHEST /RU SYSTEM"
    result = oShell.run(strCmd, 1, True)
    ' check the result to see if the command was successful
    If result = 0 Then
         MsgBox "SUCCESS!"
    Else
         MsgBox "Task creation failed"
    End If



    ¯\_(ツ)_/¯




    • Edited by jrv Saturday, August 2, 2014 2:13 PM
    • Marked as answer by saikrishna_n Saturday, August 2, 2014 5:45 PM
    Saturday, August 2, 2014 2:11 PM
  • Thanks Jrv.. It Worked :)


    Sai Krishna

    Saturday, August 2, 2014 5:46 PM
  • You are welcome.  Quotes can be a pain.  Doubling them in VBScript escapes the quote.  Be sure to not try to over-quote things.


    ¯\_(ツ)_/¯

    Saturday, August 2, 2014 6:10 PM