Custom Action - Input and Log Output - Advanced Installer Community
coroberti
Posts: 18
Joined: Sun Jan 19, 2020 4:11 pm

Custom Action - Input and Log Output

Hi,
After reading docs, I would prefer to get the gurus advise.

1. Custom Action Input - it should get it from the command line as custom Properties obtained from command-line of msiexec.exe.

1.1 Custom Property passed to msiexec.exe command line.
msiexec.exe ... MYPROPERTY="MYVALUE"

Q.1.1 Should I register MYPROPERTY somewhere with "Advanced Installer"?

1.2 Custom Action gets it via command line.
Q.1.2 Is it something like the below in the command line area?
-a -b [MYPROPERTY]

1.3 Conditional run if [MYPROPERTY] value exists.
Q.1.3 Is there an option for a conditional run if MYPROPERTY was indeed passed to msiexec and and has non-empty value?


2. Custom Action Output. Custom Action is a C/C++ exe. It returns 0 on success, and positive values on various errors.

We need to log to msiexec log (when running it with /L*V "path-to-logfile")
- the success status
or
- the error status with explanations.

Q.2 What are the options to either directly write to the log OR after the Custom Action to collect the return status
and thus translate it to the output to log?

Thank you in advance!
Kind regards,
Robert
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Custom Action - Input and Log Output

Hello Robert,

Please allow me to answer your inquires below:
1. Custom Action Input - it should get it from the command line as custom Properties obtained from command-line of msiexec.exe.
Yes, this is possible.
1.1 Custom Property passed to msiexec.exe command line.
msiexec.exe ... MYPROPERTY="MYVALUE"
The syntax of this is indeed correct.
Q.1.1 Should I register MYPROPERTY somewhere with "Advanced Installer"?
If you pass the property through the command line, then there isn't any need for you to do so. If you do not want to pass the property through the command line, then you can indeed define your property in Advanced Installer ("Install Parameters" page --> "Properties" section).
1.2 Custom Action gets it via command line.
Q.1.2 Is it something like the below in the command line area?
-a -b [MYPROPERTY]
Yes, this is possible. Let's have the following exmple:

We have the following application - Notepad/Notepad++

Notepad/Notepad++ can be run from the command line, as it follows:

<path_to_notepad++>\notepad++.exe --> it simply opens notepad++

<path_to_notepad++>notepad++.exe "path_to_some_txt_file" --> it opens a text file in notepad++

Let's say we want to pass the path to the .txt file through a property. If so, we could create a custom action as it follows:
LaunchFile.png
LaunchFile.png (64.76 KiB) Viewed 17403 times

After doing so, we can launch our .MSI file as it follows:
cmd.png
cmd.png (60.6 KiB) Viewed 17403 times

This way, the notepad++.exe will be launched with the argument stored by our property.
1.3 Conditional run if [MYPROPERTY] value exists.
Q.1.3 Is there an option for a conditional run if MYPROPERTY was indeed passed to msiexec and and has non-empty value?
Yes, this is possible. To do so, please go to your custom action and enter the name of your property in the "Condition" field, as it follows:
condition.png
condition.png (70.1 KiB) Viewed 17403 times

This way, the custom action will only be executed if the MY_PROP property exists.

Q.2 What are the options to either directly write to the log OR after the Custom Action to collect the return status
and thus translate it to the output to log?
This could be done in the following way:

- instead of using the predefined "Launch File" custom action, you could use another custom action (e.g. PowerShell, VBScript, C#, etc.) which will launch your .EXE file, check its error code and then write the results to the log file.

For instance, the return code of an executable can be checked through the %errorlevel% environment variable. Here is how you can check the return code of notepad++ using a batch file:

1. create a text file and paste the following content to it:

Code: Select all

"C:\Program Files (x86)\Notepad++\notepad++.exe"
ECHO %errorlevel%
PAUSE
2. replace the path with the path on your machine

3. save it with the .bat extension

4. run it

This should bring back the return code of the process.

Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
coroberti
Posts: 18
Joined: Sun Jan 19, 2020 4:11 pm

Re: Custom Action - Input and Log Output

Dear Catalin,
Thank you for the detailed reply that fully covers the INPUT section.
So, INPUT question is closed.

Regarding the OUTPUT from Custom Action,
the question was how to pass messages to the msiexec.exe logfile,
and NOT to a separate file. Thus, IT people have a single logfile to monitor.

There's some API like MsiProcessMessage, but it could be that the matters were simplified
by Advanced Installer?

One option could be by analyzing the return code in a script, as you suggested, and sending messages
via some API to MSI log-system.

Alternatively, perhaps via a DLL directly from C/C++ custom action.

Your comments and/or suggestions would be very much appreciated.

Thanks in advance,
Kind regards,
Robert
coroberti
Posts: 18
Joined: Sun Jan 19, 2020 4:11 pm

Re: Custom Action - Input and Log Output

To continue my previous question regarding OUTPUT or errors
from custom action, there's an article devoted to the topic:

https://www.advancedinstaller.com/user- ... ction.html

The questions related to the article are:

- Is this function supposed to be attached to "Launch attached file" or that to be corrected to "Launch file"?
- Is there any parallel function to use directly from C/C++?
- If the only option is to output to msiexec log is from vbscript,
that comes so the place to run my Custom C/C++ Action and analyze return values,
how to pass Custom Properties values to the script (via some environment vars?) ?

If there's a way to write directly from C/C++ Custom Action to msiexec log file?

Yet another suggestion I see is to use the "Write-Output" cmdlet into your PowerShell custom action code.
viewtopic.php?t=31577

Since my installation is for Windows-7 and up, PowerShell script could be also fine.
If there's an example of using it with appropriate formatting, it could be appreciated.
Somehow, Custom Properties values should be passed to the PowerShell is as well (environment vars to set?)

If there's a reference for an appropriate format of writing to msiexec logfile, please let me know.

Thanks in advance!
Kind regards,
Robert
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Custom Action - Input and Log Output

Hello Robert,
Dear Catalin,
Thank you for the detailed reply that fully covers the INPUT section.
So, INPUT question is closed.
You are always welcome. I am really glad I was able to help.
Regarding the OUTPUT from Custom Action,
the question was how to pass messages to the msiexec.exe logfile,
and NOT to a separate file. Thus, IT people have a single logfile to monitor.
It seems that somehow I have forgotten to give you a link to our article which explains how to write to the log file. Please accept my apologies for that.
Alternatively, perhaps via a DLL directly from C/C++ custom action.
To be fully honest with you, as I am not very familiar with C and C++, I can not say for sure if there's any method to write to a log file directly from your .EXE.

However, there is the method described above:

- have a script file (VBS, PowerShell) which launches your file

- after doing so, it gets its exit code

- write the exit code in the log file

For instance, a PowerShell script that launches a process and checks its exit code would look like this:

Code: Select all

$process = Start-Process -FilePath "C:\Users\Catalin\Desktop\sample.exe" -PassThru -Wait
$process.ExitCode
if($process.ExitCode -ne 0){
Write-Output "The custom action failed with the following error code: $($process.ExitCode)"
}
where "sample.exe" is a simple executable file created in C#.

When adding this as a "PowerShell inline script" custom action in an Advanced Installer project, the resulted MSI's log file will look like this (in what regards the custom action):
ExitCodeLog.png
ExitCodeLog.png (62.35 KiB) Viewed 17392 times
Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
coroberti
Posts: 18
Joined: Sun Jan 19, 2020 4:11 pm

Re: Custom Action - Input and Log Output

Dear Catalin,
Thank you very much for your guidelines.

You are indeed convincing to use the script for that.

The lost element of the puzzle is how to get MSI-passed custom properties in the script.

Let's say, msiexec.exe gets a custom property via its command line: MYPROPERTY="MyPropertyValue"

And I need to get the value of MYPROPERTY and use it in the command line
of process running in the script.

Thank you in advance!
Kind regards,
Robert
coroberti
Posts: 18
Joined: Sun Jan 19, 2020 4:11 pm

Re: Custom Action - Input and Log Output

Well, and I need to pass the custom action Application Folder [APPDATA]
to the script as well.

Thanks!
Kind regards,
Robert
coroberti
Posts: 18
Joined: Sun Jan 19, 2020 4:11 pm

Re: Custom Action - Input and Log Output

After some investigation, my custom action could be only deferred.

It needs to get APPDATA property and from 2 up to 4 custom properties from msiexec command line.

This link explains how to pass two specific MSI properties to a deferred custom action:
https://www.advancedinstaller.com/user- ... ell-script

Your help would be appreciated.

Thanks in advance,
Robert
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Custom Action - Input and Log Output

Hello Robert,

There are two ways of getting property values in your PowerShell script. These ways differ based on what your custom action's execution time is (either "Immediately" or "When the system is being modified (deferred)").

I could notice that your custom action is a deferred one, but I will explain both ways just in case you will ever need this in the upcoming future.

1. For immediate custom actions - for this type of custom action, we can use the "AI_GetMsiProperty" custom made cmdlet, which has the following syntax:

Code: Select all

AI_GetMsiProperty YOUR_PROPERTY
and save it in a PowerShell variable, as it follows:

Code: Select all

$variable = AI_GetMsiProperty YOUR_PROPERTY
Let's have a look on the following sample I created so I can explain it better to you:

- first of all, we have the PowerShell inline script as explained above:
PowerShellParameters.png
PowerShellParameters.png (147.14 KiB) Viewed 17385 times

- beside that, we have our MSI package which we launch as it follows:
cmd.png
cmd.png (15.27 KiB) Viewed 17385 times

- upon installation, when the custom action executes, it will display the value of our property in a message box, as it follows:
firstMessagebox.png
firstMessagebox.png (62.08 KiB) Viewed 17385 times

Hope things are clear in what regards getting Windows Installer properties in a PowerShell script (immediate type).

2. For deferred custom actions - in this case, things are a little bit different. By default, it is not possible to use the value of properties during the deferred stage (this is the default behavior of Windows Installer) - all of their values being basically deleted.

However, an exception to this rule is the CustomActionData property

This is the only property that keeps its value(s) during the deferred stage.

Baiscally, here, what we need to do is:

- set the CustomActionData property to the value of our property(ies) that we need to use and then parse its content to get the values (since the values are delimited by a separator)

Luckily, we do not have to do that in case of a PowerShell custom action, thanks to the improvements that were brought upon it). In order to use properties during the deferred stage, we need to use the "Parameters" field, like this:
Deferred.png
Deferred.png (164.72 KiB) Viewed 17385 times

Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
coroberti
Posts: 18
Joined: Sun Jan 19, 2020 4:11 pm

Re: Custom Action - Input and Log Output

Wow, excellent explanation.

Re: Deferred case.

Since in my case I need to pass APPDATA property and from 2 up to 4 MSI properties,
could it be the case extended to:

Param($firstProperty,$secondProperty,$thirdProperty,$forthProperty ...) ?

or there is a room only for two params?

Thanks,
Robert
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Custom Action - Input and Log Output

Hello Robert,

You can use as many properties as you need there.

I have used two just for the sake of the example.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
coroberti
Posts: 18
Joined: Sun Jan 19, 2020 4:11 pm

Re: Custom Action - Input and Log Output

Dear Catalin,
Thank you very much!

Kind regards,
Robert
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Custom Action - Input and Log Output

You are always welcome, Robert.

I am really glad I could help.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
coroberti
Posts: 18
Joined: Sun Jan 19, 2020 4:11 pm

Re: Custom Action - Input and Log Output

Dear Catalin,
It woks now as inline script.

To prevent output of the inline script to log,
I'm planning to move it to a non-inline, file-based Powershell Script,
and sign the file for a PowerShell Action.

Thank you very much for your guidelines and examples.

Kind regards,
Robert
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Custom Action - Input and Log Output

You are always welcome, Robert!

Glad I could help.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”