Catalin
Posts: 6511
Joined: Wed Jun 13, 2018 7:49 am

InstallTypeDlg - Automatic selection accordingly with user's rights.

Mon Jun 10, 2019 12:49 pm

Hello guys,

License type required: Enterprise or above.

Use case: A package having its "Installation Type" of "Per-machine if user is administrator, per-user otherwise" ("Install Parameters" page --> "Installation Type" field).

By default, this option is intended to work with the "InstallTypeDlg", which can be added to your project by going to "Dialogs" page.

The "InstlalTypeDlg" looks like this:
InstallTypeDlg.PNG
InstallTypeDlg.PNG (27.56KiB)Viewed 178258 times
As you can see, it lets the user decide whether he wants to install into a per-user location, or a per-machine location.

By default, the per-machine installation type is chosen, as in the following screenshot:
Per-Machine.PNG
Per-Machine.PNG (23.46KiB)Viewed 178258 times
even if the user which deployed the setup is not an administrator.

The best behavior would be if, somehow, the setup could detect if it (the setup) was launched as an administrator and, based on that, automatically select the best option (either per-user if the setup was not launched as an administrator or per-machine if the setup was launched as an administrator)

Unfortunately, as for this moment, Advanced Installer does not offer predefined support for such a task.

However, this can be achieved fairly easily through a custom action (e.g. PowerShell script, VBScript, C# dll, etc.).

The property which helps the setup decide which option to be used is called "AI_InstallPerUser". This property can have two values:

- if AI_InstallPerUser = 1 ==> per-user is by default used ("Only for me")

- if AI_InstallPerUser = 0 ==> per-machine is by default used ("Everybody (all users")

With that being said, our custom action should be setting the "AI_InstallPerUser" property based on whether the setup was run elevated or not.

In order to determine if a process is run elevated, we can use a custom action. For instance, in this sample, I will be using a PowerShell script to determine if the process is run elevated or not. However, you are not limited to this and you can use any other language that you are more familiar with.

Here is a little script which does what I have said above, setting a variable to either "True" (if the process is run as admin) or "False" (if otherwise). Based on the result of this, we will then set a Windows Installer property which we will use to condition the "SetProperty" custom action that will set the "AI_InstallPerUser" property accordingly:

Code: Select all

Add-Type -AssemblyName PresentationFramework

$runAsAdmin = ([Security.Principal.WindowsPrincipal] `
  [Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if($runAsAdmin -eq $true){

# Set the MY_PROP to "0". This property will furher be used in deciding to which value we set
# the "AI_InstallPerUser" property.
 
AI_SetMsiProperty MY_PROP 0


}
elseif($runAsAdmin -eq $false){

# Set the MY_PROP to "1". This property will furher be used in deciding to which value we set
# the "AI_InstallPerUser" property.

AI_SetMsiProperty MY_PROP 1
}
Now that we have created our script, it is time to implement it in Advanced Installer. To do so, you can proceed as it follows:

- go to "Custom Actions" page

- add a "PowerShellScriptInline" custom action with sequence by pressing the "Add custom action with sequence" button which is placed to the right side of the custom action's name.

- copy paste the above code in your custom action.

- schedule it accordingly. The dialogs are displayed during the "User Selection" action group => we should schedule it before that. With that being said, simply drag and drop the custom action before the "User Selection" action group in the "Wizard Dialogs Stage".

Now that we have created the custom action, it's time to set the "AI_InstallPerUser" property based on the custom action's result. To do so, please proceed as it follows:

- go to "Dialogs" page

- select the "InstallTypeDlg" dialog

- delete the default "Init Event"

- create the following two init events:

Event: Set Installer property value
Property: AI_InstallPerUser
Argument: 1
Condition: MY_PROP="1"


Event: Set Installer property value
Property: AI_InstallPerUser
Argument: 0
Condition: MY_PROP="0"


Build and save the project. You can test this by running the setup with both elevated privileges and without them and the selection should be done automatically.

Also, attached below, you can find a sample project which I have created for your reference:
Sample project.aip
(16.35KiB)Downloaded 3088 times
Hope this helps.

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

Return to “Sample Projects”