Jump to content

Recommended Posts

Posted

I'm attempting to created a windows task using SCHTASKS, but seem to have my formatting incorrect as a double quote is getting stripped out. How might I correct my code to resolve this dilemma?

#RequireAdmin
#include <AutoItConstants.au3>
Global $TaskName, $sStartTime, $Program, $ProgramName, $Argument

$TaskName = "MyTask"
$sStartTime = "15:00"
$ProgramName = "\\Server\share\myapp.exe"
$Argument = " /verbose"
$Program = '"' & $ProgramName & '"' & $Argument & '"'
MsgBox(0, "Program with Argument", $Program)

Scheduler()

Func Scheduler()
    Local $sCmd, $sStartDate, $sDateTime, $sXtra_Parms, $Result
    Local $QueryTask = Run("SCHTASKS /QUERY /TN " & $TaskName, "", @SW_HIDE, $STDOUT_CHILD)
    Local $line = ""

    If @OSVersion = "WIN_VISTA" Or @OSVersion = "WIN_7" Or @OSVersion = "WIN_8" Or @OSVersion = "WIN_81" Then $sXtra_Parms = " /Z /V1"

    While 1
        $line &= StdoutRead($QueryTask)
        If @error Then ExitLoop
    WEnd

    If $line = "" Then
        MsgBox(1, "Task Command", 'SCHTASKS /CREATE /TN ' & $TaskName & ' /TR ' & '"' & $Program & '"' & ' /ST ' & $sStartTime & ' /SC ONCE /RU SYSTEM' & $sXtra_Parms)
        $sCmd = 'SCHTASKS /CREATE /TN ' & $TaskName & ' /TR ' & '"' & $Program & '"' & ' /ST ' & $sStartTime & ' /SC ONCE /RU SYSTEM' & $sXtra_Parms
        ;$sCmd = 'SCHTASKS /CREATE /TN ' & $TaskName & ' /TR  ' & $Program & '  /ST ' & $sStartTime & ' /SC ONCE /RU SYSTEM' & $sXtra_Parms
    Else
        $sCmd = 'SCHTASKS /CHANGE /TN ' & $TaskName & ' /ST ' & $sStartTime
    EndIf
    $Result = Run($sCmd, "", @SW_HIDE)
    If $Result > 0 Then
        MsgBox(64, "Success!!", "The task was successfully created.")
    Else
        MsgBox(48, "Sorry...", "There was a problem scheduling this installation, please contact the HelpDesk for assistance.")
    EndIf
EndFunc   ;==>Scheduler

The task is created with the above code, except the formatting is wrong so I getting an invalid directory error upon run time.

The above creates this command (Serversharemyapp.exe" /verbose) in the task scheduler.

What I'm actually trying to get is ("Serversharemyapp.exe" /verbose)

The first double quote is being stripped out using my faulty code.

Any suggestions?

Thanks for your time,

-Mike

Posted

Hi l3ill,

I'm not sure what your trying to point out...but yes, the command going into the schtasks app has the quotes in all the right places, it's when the actual task is created that the first quote is stripped out and the command fails upon execution.

I've found a few informative posts from years ago, but none seem to follow this issue to a suitable resolution. I cannot copy and paste the command to other machines as the program and the argument are not fixed.

I'm currently looking at the possibility of generating an XML file on-the-fly that I can run against schtasks /create... (XML schema reference: http://msdn.microsoft.com/en-us/library/windows/desktop/aa383609%28v=vs.85%29.aspx)

but I fear that route may be way over my head.

Any other thoughts??

-Mike

Posted (edited)

$ProgramName = '"\\Server\share\myapp.exe"'
$Argument = ' /verbose '
$Program = $ProgramName & $Argument
MsgBox(0, "Program with Argument (run string)", "'" & $Program & "'")

double wrap it, it works on my machine.  Here is post #2 wrapped up.

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted (edited)

Ok...as it turns out both your versions work using my poor example, sadly the real path to the program, and the program itself, have spaces. Once I removed the spaces, the task was created correctly...but unfortunately the spaces may or may not be present, I need to account for both. :doh:

This is more accurate to my situation...
$ProgramName = '"\\Server\shared directory\my app.exe"'
$Argument = ' /verbose '
$Program = $ProgramName & $Argument
MsgBox(0, "Program with Argument (run string)", "'" & $Program & "'")

Thank you l3ill and boththose for taking a crack at this.

-Mike

Edited by mdwerne
Posted (edited)

...throwing the second set of quotes into the $Program variable cleans it up a bit, here is a reproducer that works with spaces (provided you have 7z)

$ProgramName = '"C:\Program Files\7-Zip\7z.exe"'
$Argument = ' -? '
$Program = '"' & $ProgramName & '"' & $Argument
MsgBox(0, "Program with Argument (run string)", $Program)
run("cmd /k " & $Program)
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...