Jump to content

Recommended Posts

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

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

Link to post
Share on other sites

$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

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

Link to post
Share on other sites

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
Link to post
Share on other sites

...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

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

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By abberration
      Software Installer
      Version: 2.0
      It's been been a long year, but I finally got some time to rework this project. I re-wrote everything from scratch because the old version was getting too complicated with so many options and sub-menus. This new version is much easier to use and I have been testing for a few days and it seems very stable.
      For those who are new to this software, it helps you install software silently/unattended. This new version tries to determine the silent switch automatically. You can also re-organize the order in which the software installs by dragging & dropping them in the listview. It now supports creating profiles and checks for missing software (and automatically unchecks them, so it does not attempt to install non-existent software). One feature I included was because I have seen several people on Youtube talk about disliking bright screens at night. So, now you can choose from a few color theme (half of them are dark). I dabbled a bit more into GDI+ to draw a few things and show my logo with a transparent background (hint: I'm not good at GDI+).
      Under the Help menu, you will find a User Guide, which goes through most of it's features. I included a new icon if you want to use when you compile the script (in the Assets > Misc folder).
      If you have questions, comments or suggestions, all are welcome. Hope you enjoy!
      Here it is in action:

       
      Software_Installer_2.0.zip
    • By Jemboy
      Recently I was working on a script with icons using GuiCtrkCreatIcon.
      I decided to change the sub folder name of the icons to a more meaning name, however made a typo.

      I tested the .exe on my test computer and it worked flawlessly (because both icon folder where on my test computer) 😁
      But after I installed the script on the intended computers , I got chaos!😵
      Zooming into the problem, I discovered, that because the icons could not be found, the ControlID were returned with a value of 0
      and thus played havoc within the GuiGetMsg() switch/case statement.
      I have been able to reproduce this  (see example)
      #include <GUIConstantsEx.au3> ;============================================================================================================ ; PLEASE, do not save this example in the example folder: C:\Program Files (x86)\AutoIt3\Examples\Helpfile ;============================================================================================================ Example() Func Example() GUICreate(" My GUI Icons", 250, 250) $Icon1 = GUICtrlCreateIcon("shell32.dll", 10, 20, 20) $Icon2 = GUICtrlCreateIcon(@ScriptDir & '\Extras\horse.ani', -1, 20, 40, 32, 32) $Icon3 = GUICtrlCreateIcon("shell32.dll", 7, 20, 75, 32, 32) GUISetState(@SW_SHOW) ;$Icon2 = -1 ; ==> When this line is uncommented the script "works", so -1 could be a potential fix. ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $Icon2 Beep (500,500) EndSwitch WEnd GUIDelete() EndFunc ;==>Example If you save the above script outside the Autoit example folder and run it, it will keep beeping because GuiCtrlCreatIcon did not find horse.ani and return $Icon2=0.
      At the moment GUICtrlCreateIcon () only returns the conntrolID on success and 0 on failure.
      I would like to propose a return of -1 on failure, so a existing and working script won't go awry when the icon can not be found.
       
    • By TheSaint
      Just a little program I whipped up a couple of days ago, to fill a need. It has a few nice features.
      Basically it gives batch list ability, to any program that supports an input path on the command-line (%1 etc).
      It only supports drag & drop, and uses RunWait .... but feel free to adapt to your needs.
      Batch Create & Run v1.8.zip  BUGFIX
      OLDER DOWNLOADS
          
      Older Screenshot
      NOTE - My usage was with a BAT file, so even though I coded for EXE files as well, that element remains untested, but should work.
      EDIT - Of course, if you use a DOS (console) EXE, you might want to add a checkbox to specify that the @Comspec method should be used. Perhaps I will add that ... one day.  DONE
    • 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
    • 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! 
×
×
  • Create New...