Use ini file configuration as parameter to running service - Advanced Installer Community
aiuser2142
Posts: 1
Joined: Fri Jan 31, 2020 10:15 am

Use ini file configuration as parameter to running service

Hi,

Is it possible to pass the value set in a configuration file (installed during install) to a Java service? I'm able to pass a fixed string but not able to pull a property / value from an ini file and use that as a part of the argument string, specifically:
--server.port=[value from ini file]

I've seen a few threads asking for a similar thing but they seem to be using the ini file configuration on install whereas I'd like to pass it to the service every time it is run.

I've tried adding an .ini file search using the Search screen and also via the Table Editor but in both cases, the property is not found and when the service runs, the property simply evaluates as empty:

--server.port=

Is it possible to achieve this?

Thank you
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Use ini file configuration as parameter to running service

Hello and welcome to Advanced Installer forums,

Yes, it is possible to achieve what you want.
I've tried adding an .ini file search using the Search screen
This is actually how this can be achieved, with an exception (we will discuss about this a bit later).

Let's recap this so we can better understand this scenario.

Let's consider we have the following .INI file:

Code: Select all

[SectionSample]
MyName = Catalin
In this case, we would like to pass the value of "MyName" (which is "Catalin" - without quotes) as a parameter to our service.

To do so, we can create a search that will get the value from the .INI file. The search could look like this:
Search.png
Search.png (19.14 KiB) Viewed 2183 times

The biggest problem here is the fact that we need to know where the .INI file resides. In my case, I have placed the .INI file in the "C:\" volume, therefore I have used the [WindowsVolume] property, which resolves, at install time, to the volume where the OS is installed (in my case, C:\).

For more information about this, you can have a look over our "Paths of the folders in the "Files and Folders" Page" article.

After doing so, we can go to "Services" page and add our argument, as it follows:
ServiceArgument.png
ServiceArgument.png (26.87 KiB) Viewed 2183 times

After doing so, according to the log file, the service will be started with the required parameters:
InstallServices: Service:
MSI (s) (C4:C0) [11:52:13:639]: Executing op: ActionStart(Name=StartServices,Description=Starting services,Template=Service: [1])
Action 11:52:13: StartServices. Starting services
MSI (s) (C4:C0) [11:52:13:639]: Executing op: ProgressTotal(Total=1,Type=1,ByteEquivalent=1300000)
MSI (s) (C4:C0) [11:52:13:639]: Executing op: ServiceControl(,Name=sample.exe,Action=1,Wait=1,StartupArguments=--someParameter=Catalin)
Let's now talk about the exception we've been previously mentioned. Most probably, you want to deliver the .INI file with your setup file (your .INI file is installed by your setup).

The problem that is encountered here is the fact that the searches are executed right when the setup starts, which means that your .INI file will not be available, therefore your search will be empty.

If you go to "Custom Actions" page, you can see that the "Searches" action group is placed at the top of the installation sequence.

Unfortunately, there is not much that can be done about this. The only solution left is a custom solution. Basically, here, we can have a custom action (e.g. a script / .EXE / .DLL file) which will look for our .INI file, extract the data from it and then further set a property to the extracted value.

Here is how this can be achieved:

- please go to "Files and Folders" page and add your .INI file as a temporary file using the "Add Temporary Files" button from the toolbar

The script that will do the work for us is written in PowerShell (as I am not very familiar with other programming languages).

- now please go to "Custom Actions" page and add a "PowerShell" custom action, with sequence.

- the PowerShell custom action should be scheduled as it follows:

Wizard Dialogs Stage --> after "Paths Resolution" action group

- the PowerShell script could look as it follows:

Code: Select all

# Block for declaring the script parameters.
Param()

# Your code goes here.

# get the value of the TempFolder property and store it in a variable

$tempFolder = AI_GetMsiProperty TempFolder

# as the path to the .INI file is %temp%\NameOfTheIniFile.ini, we join the paths to create the full path to our INI file

$pathToIni = Join-Path -Path $tempFolder -ChildPath "sample.ini"

$sampleIni = Get-Content $pathToIni | Where-Object {$_ -match "="} | ConvertFrom-StringData

# set a property to our required INI value

AI_SetMsiProperty MY_PROP $sampleIni.MyName
Explanation for the following line (which I did not explain in the code because the explanation is a bit longer):

Code: Select all

$sampleIni = Get-Content $pathToIni | Where-Object {$_ -match "="} | ConvertFrom-StringData
Basically, here, we use the Get-Content PowerShell cmdlet, which gets the content of a file.

This scenario here indicates the fact that we can use the ConvertFrom-StringData cmdlet, which converts a string containing one or more key and value pairs to a hash table.

Unfortunately, this cmdlet only accepts the "key=value", meaning that, if our .INI file contains sections (e.g. "[Section]"), this cmdlet will fail, because [Section] does not respect the key=value format. To avoid such scenario, we have piped the content returned by our Get-Content cmdlet to only match objects that contain "=" (as in key=value).

Hope this helps.

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

Return to “Common Problems”