Schedule task to run hidden in Inno Setup - inno-setup

I have an exe file that I've created using pyinstaller. I am using Inno Setup to create a Windows installer for this executable.
Here's a snippet from my compiler script :
Filename: "schtasks"; \
Parameters: "/Create /F /SC MINUTE /MO 2 /TN ""Discovery"" /TR ""'{app}\Discovery.exe'"""; \
Flags: runhidden runminimized
I'm using schtasks to schedule the execution of my exe file (Discovery.exe). The scheduling works perfectly fine but a command line window still appears when the file runs. This leads me to believe that there's something weird happening with runminimized and runhidden
Discovery.exe is in fact a command line application created using pyinstaller.
How do I ensure that no command line window shows up when this file is supposed to run?
Final working [Run] statement on Inno Setup based on the answer by #Bill_Stewart:
[Run]
Filename: "schtasks"; \
Parameters: "/Create /F /SC MINUTE /MO 5 /TN ""Discovery"" /TR ""'wscript.exe' '{app}\RunHidden.js' '{app}\Discovery.exe' "" "; \
Flags: runhidden runminimized;
Note the usage of quotations due to spaces in my file paths.

The problem is that the runhidden flag in Inno Setup's [Run] section is running the schtasks command, and the schtasks command is scheduling your program (Discovery.exe) to run. When the package installs, the schtasks command runs hidden as requested, but this doesn't mean that the scheduled task you are creating will run Discovery.exe as a hidden process.
If you are running the scheduled task as the current user, the Task Scheduler doesn't have a "run this task hidden" setting. However, you can work around this by creating a short WSH script:
// RunHidden.js
// Runs a program in a hidden window
// Run this script using wscript.exe
var Args = WScript.Arguments;
if ( (Args.Unnamed.Count == 0) || (Args.Named.Item(0) == "/?") ) {
WScript.Echo('Usage: wscript RunHidden.js "command"');
WScript.Quit(0);
}
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var Command = Args.Unnamed.Item(0);
if ( ! FSO.FileExists(Command) ) {
WScript.Echo("File not found - " + Command);
WScript.Quit(2); // ERROR_FILE_NOT_FOUND
}
var WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run('"' + Command + '"',0);
You can distribute the above script with your installation. Schedule wscript.exe as the program to run, the above script and Discovery.exe as parameters, and the scheduled task should run the command without a window.

Related

Return output filename generated by Inno Setup preprocessor to a batch script

I'm setting up a batch file that will compile my application, the compile the installer using Inno Setup.
My setup filename is determined using some ISPP command in Inno Setup (creating the filename from the build version, among other things).
The last step is to upload the setup to my FTP, but for this I need a way to retrieve the installer filename generated by Inno Setup.
Is there a way to do that?
You can write a preprocessor variable value to a file.
One way is to execute an external program to do the writing using Exec function:
#define FileName "foobar"
#expr Exec( \
"cmd.exe", "/c echo " + FileName + "> """ + SourcePath + "\filename.txt""", , , \
SW_HIDE)
You can then read the file in your batch file. Or you can have the preprocessor generate a full FTP upload script and just execute it from the batch file.
Another way is to create an INI file using WriteIni function:
#define FileName "foobar"
#expr WriteIni(SourcePath + "\filename.ini", "Section", "FileName", FileName)
Though personally, I'd generate the file name in the batch file (or replace the batch file with a better language), and pass it to Inno Setup, rather then the other way around.

task scheduler is displaying an error "The specified query is invalid"

I am running Windows 10.
I'm trying to run a backup job (C:\WINDOWS\system32\robocopy.exe) using Task Scheduler. But I was getting issues. To figure out what was going on, I turned on the history log. Unfortunately since doing that, every time I click the History tab I get the following error:
Dialog Title: "Query Error"
Dialog Message: "One or more logs in the query have errors."
The table in the dialog: "Microsoft-Windows-Task-Scheduler/Operational | The specified query is invalid"
"The events displayed are partial results."
Nothing appears in the History pane, so I cannot debug either problem. Does anyone know what is going on?
I am having the exact same error as in
https://www.experts-exchange.com/questions/27676176/Query-Error-In-Microsoft-Task-Scheduler.html
The answer is at
http://www.minasi.com/forum/topic.asp?TOPIC_ID=27906
The answer site (www.minasi.com) is apparently moved and did not retain the answer so cannot try the fix that solved his problem.
Please check if you have single quotes (apostrophes) in the task name. It is known that the apostrophe in the task name breaks task history. To remove them you can try following batch file (it exports task to the xml file, imports it with the new name and removes the old task):
#set "tn=my PC's task"
#if not "%tn%"=="%tn:'=%" echo renaming "%tn%" ==^> "%tn:'=%" && ^
schtasks.exe /query /tn "%tn%" /xml>"%temp%\%tn:\=_%.xml" && ^
schtasks.exe /create /tn "%tn:'=%" /xml "%temp%\%tn:\=_%.xml" && ^
schtasks.exe /delete /tn "%tn%" /f && del /q /f "%temp%\%tn:\=_%.xml"

Execute a perl script with command line argument in inno setup run setup

In our application we need to change the property of the jboss standalone-full.xml file during installation.
For that i have written a perl script which will accept xml file and value need to be modified as command line argument. I want to run this script during installation.
I have added below line in {Run] section of .iss(inno setup) file.
Filename: "{cmd}"; WorkingDir: "{app}\Perl64\"; Parameters: "/c ""bin\perl {app}\bin\makeHttpDisableScript.pl -file=""{app}\jboss\standalone\configuration\standalone-full.xml"" -httpEnabled=""false"""; StatusMsg: "Running perl script..."; Flags: runhidden
But it is not working.

Inno Setup, spaces and double quote in [Run]

I'm trying to schedule a task on Windows, and unfortunately, it doesn't work!
The task is created but not correctly.
When I look into task's parameters, it says:
PROGRAM: C:\Program
ARGUMENTS: Files(x86)\AppName\executable.exe
This is how I proceed:
[Run]
Filename: "schtasks.exe"; Parameters: "/create /tn TaskName /sc MINUTE /mo 10 /tr ""{app}\{#MyAppExeName}""";
And I don't know why it doesn't work, doubles quotes should fix the "spaces problem"
Any ideas?
This problem is known and is described in this knowledge base article. You will need to enclose the file name between combination of backslash and quotation marks. That article describes:
Cause:
This problem occurs if the path of the scheduled task contains a
space. For example, this problem occurs if you create a schedule for
the following task by using Schtasks.exe:
"c:\foldername containing spaces\task.bat"
Workaround:
To work around this problem, enclose the path portion of the task (not
including arguments or switches) between backslash (\) and quotation
marks (") character combinations, for example \". Enclose the complete
path of the task (including arguments or switches) between quotation
marks as usual when you create a path or command that contains spaces.
For example, the following example task does not run when you schedule
it:
schtasks /create /tn "my task" /tr "c:\foldername containing spaces\script.bat arguments" /sc once /sd 07/29/2003 /st 10:01
However, when you enclose the path of the task between the backslash
and quotation marks character combinations as in the following
example, the scheduled task runs successfully:
schtasks /create /tn "my task" /tr "\"c:\foldername name containing spaces\script.bat\" arguments" /sc once /sd 07/29/2003 /st 10:01
So in your case it could be:
[Run]
Filename: "schtasks.exe"; Parameters: "/create /tn TaskName /sc MINUTE /mo 10 /tr ""\""{app}\{#MyAppExeName}\""";
Late to the party, but as mentioned in this SO answer, you could try to simply add single quotes around your /TR parameter, which would result in
Parameters: "/create /tn TaskName /sc MINUTE /mo 10 /tr '"{app}\{#MyAppExeName}"'";
That might be a bit easier than juggling backslashes.

Redirecting a WSH script without logo

I wrote a jscript script intended to create a CSV file. In theory I should use it like this:
myscript>foo.csv
But the CSV file gets corrupted by "Microsoft (R) Windows Script Host Version 5.8 etc." logo.
I know I can write:
cscript //nologo myscript>foo.csv
but it is a loss of productivity and readability.
Do you have a better solution?
cscript /?
Usage: CScript scriptname.extension [option...] [arguments...]
Options:
//B Batch mode: Suppresses script errors and prompts from displaying
//D Enable Active Debugging
//E:engine Use engine for executing script
//H:CScript Changes the default script host to CScript.exe
//H:WScript Changes the default script host to WScript.exe (default)
//I Interactive mode (default, opposite of //B)
//Job:xxxx Execute a WSF job
//Logo Display logo (default)
//Nologo Prevent logo display: No banner will be shown at execution time
//S Save current command line options for this user
//T:nn Time out in seconds: Maximum time a script is permitted to run
//X Execute script in debugger
//U Use Unicode for redirected I/O from the console
So use
cscript //Nologo //S
Command line options are saved.

Resources