mdwerne Posted June 10, 2014 Posted June 10, 2014 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? expandcollapse popup#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
l3ill Posted June 10, 2014 Posted June 10, 2014 (edited) $ProgramName = "\\Server\share\myapp.exe" $Argument = " /verbose" $Program = '"' & $ProgramName & '"' & $Argument MsgBox(0, "Program with Argument", $Program) Edited June 10, 2014 by l3ill My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example
mdwerne Posted June 10, 2014 Author Posted June 10, 2014 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
iamtheky Posted June 10, 2014 Posted June 10, 2014 (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 June 10, 2014 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mdwerne Posted June 10, 2014 Author Posted June 10, 2014 (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. 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 June 10, 2014 by mdwerne
iamtheky Posted June 10, 2014 Posted June 10, 2014 (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 June 10, 2014 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now