cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted

Unable to Capture results properly from CMD line parameter executions by LaunchApp

Hello Friends,

I am facing problem in fetching the return result properly from LaunchAppAndWait where i am trying to execute the CMD command.

function CreateGroup()
STRING szGroup,szTempFile, szTempFolderPath;
STRING szProgram,szCmdLine, szResult, szComment;
NUMBER nResult;

begin
szProgram =SystemFolder^"cmd.XXX"; //Need to remove executable format as forum not allowing the post to submit. IGNORE IT!!
szTempFile = "ErrorLogTest.txt";
szTempFolderPath = TempFolder ^ szTempFile;
szGroup = "TestGroup";
szComment = "TestGroup";

szCmdLine = "%/C \"NET LOCALGROUP \""+szGroup+"\" "+"\/comment\:\""+szComment+"\" \/add >"+szTempFolderPath+" 2>&1\"";
MessageBox(szCmdLine,INFORMATION);

//Launch the program with the parameters
if (LaunchAppAndWait(szProgram, szCmdLine, LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN) < 0) then
MessageBox("Failed to execute the program", INFORMATION);
else
nResult = LAAW_PARAMETERS.nLaunchResult;
szResult = FormatMessage(nResult);
//if the launched applications returns an error, it will be logged
if (nResult != 0) then
SprintfMsiLog("The returned error is:\"" + szResult + "\"");
else
SprintfMsiLog("The returned information is:\"" + szResult + "\"");
endif;
MessageBox("Program Executed:"+szResult, INFORMATION);
endif;
end;


When I execute the code, the ErrorLogTest.txt file is getting created with all the outputs/errors properly. BUT

The szResult in my code is returning "The specified file path cannot be found" and nResult is -2. Definitely its not correct return status.
This happens in the case when the group is already present and it writes this message in the log file "The specified local group already exists".

In case when the group doesnt exist, it creates the group and szResult shows "Operation completed successfully" and the same gets written in the log file too.

My observation shows that because of the double quotes due to /C, Installer is somehow thinking the entire command as some kind of file path. You can verify this by printing the szCommand in a MessageBox.

Is there any problem with my code? I want to get correct result in szResult in both error and success cases. How to get it. I know i can get it from the logfile that i am printing but i want to get directly from szResult without having to read the file repeatedly.

Any help guys...please. Thanks
Labels (1)
0 Kudos
9 Replies
rguggisberg
Active participant

Re: Unable to Capture results properly from CMD line parameter executions by LaunchApp

Hi,

I just took a look at one of my projects and happen to have similar code. Here is mine for reference. It is a bit different than yours in that I only write to file if command succeeds and I only write to file if output contains what I am looking for.:

szCmdLine = "/C net localgroup administrators | find /I \"" + szAccount + "\" && echo(>" + szTempFileSpec;


Here is your code. Need to be careful with placement of +.

szCmdLine = "%/C \"NET LOCALGROUP \""+szGroup+"\" "+"\/comment\:\""+szComment+"\" \/add >"+szTempFolderPath+" 2>&1\"";


Try this (untested):

szCmdLine = "/C NET LOCALGROUP + \" + szGroup + "\ \"/comment:\" + szComment + "\ "\/add >\" + szTempFolderPath + \" 2>&1\"";
0 Kudos

Re: Unable to Capture results properly from CMD line parameter executions by LaunchApp

rguggisberg wrote:
Hi,

I just took a look at one of my projects and happen to have similar code. Here is mine for reference. It is a bit different than yours in that I only write to file if command succeeds and I only write to file if output contains what I am looking for.:

szCmdLine = "/C net localgroup administrators | find /I \"" + szAccount + "\" && echo(>" + szTempFileSpec;


Here is your code. Need to be careful with placement of +.

szCmdLine = "%/C \"NET LOCALGROUP \""+szGroup+"\" "+"\/comment\:\""+szComment+"\" \/add >"+szTempFolderPath+" 2>&1\"";


Try this (untested):

szCmdLine = "/C NET LOCALGROUP + \" + szGroup + "\ \"/comment:\" + szComment + "\ "\/add >\" + szTempFolderPath + \" 2>&1\"";


Hi,

Its not working man, whatever you asked me to try. In fact it threw lot of compile errors for improper use of "" or /. I corrected them and then tried to run and it threw error: "Invalid function"
Any more suggestions?
Thanks
0 Kudos
rguggisberg
Active participant

Re: Unable to Capture results properly from CMD line parameter executions by LaunchApp

I will test and post when I get time.
0 Kudos

Re: Unable to Capture results properly from CMD line parameter executions by LaunchApp

rguggisberg wrote:
I will test and post when I get time.


Thanks Bro Smiley Happy
0 Kudos
rguggisberg
Active participant

Re: Unable to Capture results properly from CMD line parameter executions by LaunchApp

Hi dinesh.redhawk,

These command lines are tricky and I always start simple and add to it. I should have tested before posting... but that takes time Smiley Sad

You probably know that you can compile your InstallScript code without doing a full build (which can take quite a bit of time). Just right click on your Setup.rul (or similar) and click 'Compile'. This only takes a few seconds. Build when all compile errors are resolved. The following command line works for me:

szCmdLine = "/C NET LOCALGROUP " + szGroup + "\ /comment:\"" + szComment + "\" /add >\"" + szTempFolderPath + "\" 2>&1";
0 Kudos

Re: Unable to Capture results properly from CMD line parameter executions by LaunchApp

rguggisberg wrote:
Hi dinesh.redhawk,

These command lines are tricky and I always start simple and add to it. I should have tested before posting... but that takes time Smiley Sad

You probably know that you can compile your InstallScript code without doing a full build (which can take quite a bit of time). Just right click on your Setup.rul (or similar) and click 'Compile'. This only takes a few seconds. Build when all compile errors are resolved. The following command line works for me:

szCmdLine = "/C NET LOCALGROUP " + szGroup + "\ /comment:\"" + szComment + "\" /add >\"" + szTempFolderPath + "\" 2>&1";


Ok, i tried this and its giving me the same result. Now lets jump to the 2nd part of my question i.e. the result of szResult. [Please refer to my first post, code is there]

The szResult in my code is returning "The specified file path cannot be found" and nResult is -2. Definitely its not correct return status.
This happens in the case when the group is already present and it writes this message in the log file "The specified local group already exists".

In case when the group doesn't exist, it creates the group and szResult shows "Operation completed successfully" and the same gets written in the log file too.

My observation says that because of the double quotes due to /C, Installer is somehow thinking the entire command as some kind of file path. You can verify this by printing the szCommand in a MessageBox.

Whats your thoughts on this?

Thanks
0 Kudos
rguggisberg
Active participant

Re: Unable to Capture results properly from CMD line parameter executions by LaunchApp

Keep in mind that nResult only contains the result (Success/Fail) of the LaunchAppAndWait. It definitely does NOT contain the result of your NET LOCALGROUP command. You need to parse the file that you piped the results to to get that.
0 Kudos

Re: Unable to Capture results properly from CMD line parameter executions by LaunchApp

rguggisberg wrote:
Keep in mind that nResult only contains the result (Success/Fail) of the LaunchAppAndWait. It definitely does NOT contain the result of your NET LOCALGROUP command. You need to parse the file that you piped the results to to get that.


My research says another thing - If you pass LAAW_PARAMETERS as parameters to LaunchAppandWait, then nResult will contain the results of the launched application i.e. in my case NET LOCALGROUP.
Below is the explanation from Flexera Help:

"
If the application cannot be launched, the nLaunchResult member contains the result of calling GetLastError after the CreateProcess call. If LaunchApp, LaunchAppAndWait, or LaunchApplication is successful and the LAAW_OPTION_WAIT option was specified, the nLaunchResult member contains the return code of the launched application."
0 Kudos
rguggisberg
Active participant

Re: Unable to Capture results properly from CMD line parameter executions by LaunchApp

dinesh_redhawk wrote:
Ok, i tried this and its giving me the same result. Now lets jump to the 2nd part of my question i.e. the result of szResult. [Please refer to my first post, code is there]

The szResult in my code is returning "The specified file path cannot be found" and nResult is -2. Definitely its not correct return status.
This happens in the case when the group is already present and it writes this message in the log file "The specified local group already exists".

In case when the group doesn't exist, it creates the group and szResult shows "Operation completed successfully" and the same gets written in the log file too.

My observation says that because of the double quotes due to /C, Installer is somehow thinking the entire command as some kind of file path. You can verify this by printing the szCommand in a MessageBox.

Whats your thoughts on this?

Thanks


Not sure what "Same result" is. Does it compile successfully? I used the command line I posted and it works as expected. MsgBox shows:
szCmdLine=/C NET LOCALGROUP UserName /comment:"My Comment" /add >"MyPath\MyFileName.txt" 2>&1

As far as the nResult thing.... You may be right, but based on past experience I am not convinced. I have not seen it work that way.... maybe it should. Do you have any other options specified besides LAAW_OPTION_WAIT ?
0 Kudos