Sign in to follow this  
Followers 0
mdwerne

SCHTASKS /CREATE with an argument?

6 posts in this topic

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

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

#4 ·  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

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

Share this post


Link to post
Share on other sites

#5 ·  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

Share this post


Link to post
Share on other sites

#6 ·  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

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

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0

  • Similar Content

    • Skysnake
      By Skysnake
      From From here, bottom of the post
      I am not arguing the logic of this, merely would like to point out that if there is such a rule, it is not documented...  Are there other such rules?
      Skysnake
    • kawliga751
      By kawliga751
      I am attempting to run an Autoit.exe with schtasks every weekday @ 9:05 am.
      I have the below
      C:\>SchTasks /Create /TN "P10Run" /TR "O:\AUTOIT\P40 Run.au3" /SC WEEKLY /D MON, TUE,WED,THU,FRI /ST 09:05:00 SUCCESS: The scheduled task "P10Run" has successfully been created. I also did a "test" run :
      C:\>Schtasks /Run /TN "P10Run SUCCESS: Attempted to run the scheduled task "P10Run". But when I run my query to verify success my "Last Result" seems to indicate that the script did not run:
      C:\>SCHTASKS /QUERY /FO LIST /V /TN "P10RUN" Folder: \ HostName: xxxxxxxxxxxxxxxx TaskName: \P10RUN Next Run Time: N/A Status: Could not start Logon Mode: Interactive only Last Run Time: 6/14/2017 9:08:56 AM Last Result: -2147024894 Author: xxxxxxx Task To Run: O:\AUTOIT\P40 Run.au3 Start In: N/A Comment: N/A Scheduled Task State: Enabled Idle Time: Disabled Power Management: Stop On Battery Mode, No Start On Batterie s Run As User: xxxxxxxxx\xxxxxxxx Delete Task If Not Rescheduled: Enabled Stop Task If Runs X Hours and X Mins: 72:00:00 Schedule: Scheduling data is not available in this f ormat. Schedule Type: Weekly Start Time: 9:05:00 AM Start Date: 6/14/2017 End Date: N/A Days: MON, TUE, WED, THU, FRI Months: Every 1 week(s) Repeat: Every: Disabled Repeat: Until: Time: Disabled Repeat: Until: Duration: Disabled Repeat: Stop If Still Running: Disabled C:\> Is there something missing in my command? Also is there a better way to make sure that the script does in fact sun successfully?
      Thanks In Advance! 
    • WoodGrain
      By WoodGrain
      Hi guys,
      Could someone please tell me what I'm doing wrong with this code?
      I'm trying to take user input and then run a function with the same name, but it's not calling the function.
      So in the below after pressing Shift+F8 I would enter the text (without quotes) "testFunc".
      As you can see I've also tried this with IF statements, I'm new to using Switch/Case.
      HotKeySet("+{F8}", RunManually) Func RunManually() ;Use a case statement with 1 hot key and an InputBox to manually run functions $funcName = InputBox("Which Func to Run?", "Enter the name of the function to run") MsgBox(0, "Entered value", $funcName) ;If $funcName = testFunc Then testFunc() ;If $funcName = test2Func Then test2Func() Switch $funcName Case testFunc MsgBox(0, "Calling", "Calling Function") testFunc() MsgBox(0, "Called", "Function call finished") Case test2Func MsgBox(0, "Calling", "Calling Function") test2Func() MsgBox(0, "Called", "Function call finished") EndSwitch EndFunc While 1 ;testFunc() Sleep(1000) WEnd Func testFunc() MsgBox(0, "func running", "Seems to work!") EndFunc Func test2Func() MsgBox(0, "func 2 running", "2 Seems to work!") EndFunc Thanks!
    • ur
      By ur
      I have never used switch in AutoIT earlier.
       
      But today, when I started to implement it in my code, I am getting errors.
      So I tried to execute default example, but still getting the same error.

      Any suggestions please...
    • CT83
      By CT83
      Dir_Load("Target1", "GS1_TarToday", $GS1_TarToday) ;here Target1 is a dir. and GS1_TarToday is a filename. $GS1_TarToday is the edit box in which i want to display info set by dir_load function Func Dir_Load($cS_Group,$cS_Name,$cVData,$cSavedPath="Configurations\Default") Local $cCurrent_Path=String($cSavedPath&"\"&$cS_Group&"\"&$cS_Name&".ct83") Local $cFileData If Not _FileReadToArray($cCurrent_Path,$cFileData, 0) Then MsgBox($MB_SYSTEMMODAL, "", "There was an error Reading the "& $cS_Group&"\"&$cS_Name &".ct83 File ! Please Try again! ") EndIf Local $cFileData1 = _ArrayToString($cFileData) GUICtrlSetData($cVData,$cFileData1) Endfunc The above code is meant to set the data of the edit box to a file GS1_TarToday.ct83 in Target1 folder ( $cFileData1 ) but this code simply does nothing. No errors are returned. still the data of the edit box remains as it is,