Abhinay
Posts: 36
Joined: Wed Feb 27, 2019 9:33 am

How do i throw an error message for the backend powershell script

How do i throw an error message for the backend powershell script written for the combo box entered value and link it.
Scenario is - if user enters wrong value, i need to tell him that he has entered the wrong value and he needs to try again before the installation starts.
Backend power shell script validation is there for which i need to display the error message in the UI - How do i do that?

# Block for declaring the script parameters.
Param()
# Your code goes here.
# the path to your .json file
$jsonfile = 'C:\ProgramData\MyApp\Config\appsettings.json'

# convert the JSON data to a PowerShell object
$json = Get-Content $jsonfile | Out-String | ConvertFrom-Json

#fetch the SiteName from combobox

$SiteName = AI_GetMsiProperty COMBOBOX_1_PROP

if($SiteNamer.length -gt 12)
{
$SiteNameValidation =$false

}

elseif( $SiteNamer.Length -lt 7)
{

$SiteNameValidation =$false

}

elseif( $SiteName -match '[^a-zA-Z0-9]')
{
$SiteNameValidation =$false
}

else
{

$SiteNameValidation =$true
}


#fetch the EquipmentPhysicalIdentifier from combobox

$EquipmentPhysicalIdentifier = AI_GetMsiProperty COMBOBOX_2_PROP


if( $EquipmentPhysicalIdentifier -match '[^a-zA-Z0-9]')
{
throw "$WeirSerialNumber contains a special character"
}


if (-NOT ( $EquipmentPhysicalIdentifier.StartsWith("Weir.Minerals.")))
{
$EqIdValidation =$false
}
else
{
$EqIdValidation = $true
}

# adds the new key-value pair
$json.TransientStoreApiSettings | Add-Member -Type NoteProperty -Name 'siteName' -Value $SiteName
$json.TransientStoreApiSettings | Add-Member -Type NoteProperty -Name 'EquipmentPhysicalIdentifier' -Value $EquipmentPhysicalIdentifier
#$json.TransientStoreApiSettings | Add-Member -Type NoteProperty -Name 'merlinFilePath' -Value $propValue2
# convers the object back to JSON
$json | ConvertTo-Json | Set-Content $jsonfile
Catalin
Posts: 6592
Joined: Wed Jun 13, 2018 7:49 am

Re: How do i throw an error message for the backend powershell script

Hello Abhinay,

Here is how I would do the validation:

First of all, the fact that you do not want the user to continue with the installation until he enters a valid information in your edit box can be achieved by having the "Next" button of your dialog disabled. This can be easily done by going to "Dialogs" page, selecting the "Next" button and, in the "Properties" pane, set the "Enabled" attribute to "False".

Additionally, we will enable this button only when a condition is met. When the condition is met from within our script, we will set a property to a specific value and then we will use the value of this property as a condition to enable the button. This can be done by selecting the "Next" button --> "Control Conditions" tab --> create a new control condition as it follows:

Condition: MY_PROP = "true"
Action: Enable


The above property must be set from within our script when a valid condition is met.

Let's have the following example, so you can better understand how the validation can be done:

Let's say we have an edit box in which the user must input some string. The string length must be greater than 6 and lower than 12 in order to proceed with the installation.

Considering the "Next" button is already disabled, here is what we will need:

- a custom action (in our case, a PowerShell script) which will do the validation. This custom action must be without sequence, so we can trigger it from a dialog control (e.g. a push button).
- a button which will trigger the custom action.

The PowerShell script can look something as it follows:

Code: Select all

Add-Type -AssemblyName PresentationFramework
# we get the value of the EditBox (where the user inputs the string) control and store it in the $var variable.
$var = AI_GetMsiProperty EDIT_1_PROP

if($var.Length -gt 12){
[System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
}
elseif($var.Length -lt 6){
[System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
}
else{
# we set the MY_PROP property to true, so we can enable the "Next" button.
AI_SetMsiProperty MY_PROP "true"
#$prop = AI_GetMsiProperty MY_PROP
#[system.windows.messagebox]::show("$prop")
# the above two lines were for debug purposes. You can ignore them.
}
This script can be added to your project as a "PowerShelScriptInline" custom action without sequence. In order to add a custom action without sequence, all you have to do is to press the "Add custom action without sequence" button which is placed to the right side of the custom action's name.

Now it is time to add the button which will trigger the custom action. To do so, please go to "Dialogs" page, select the dialog where your validation must be done and add a "Push Button" control to it (this can be done by pressing the "Controls Toolbox" button from the toolbar). You can name this button "Validate", so the user knows what it is for. This can be done by pressing the button -> on the right pane, under "Display" section, set the "Text" attribute to "Validate" value.

On this button, we will have two Published Events. In order to add a published event, select the button, go to "Published Events" tab and press the "New":

1. First event:

Event: Execute custom action
Argument: PowerShellScriptInline
Condition: AI_INSTALL

2. Second event:

Event: Refresh the current dialog
Argument: 1
Condition: AI_INSTALL AND (MY_PROP = "true")

Also, for your reference, I have attached to this thread a sample project which I think you may find useful:
Abhinay - Sample Project for PS Validation.aip
(15.39 KiB) Downloaded 220 times
Hope this helps.

All the best,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Abhinay
Posts: 36
Joined: Wed Feb 27, 2019 9:33 am

Re: How do i throw an error message for the backend powershell script

I have two combobox Site Name and EquipmentPhysicalIdentifier. As you suggested i have created a validate button to validate these fields.
The powershell script to validate the fields is as follows:

Code: Select all

# Block for declaring the script parameters.
   Param()
   # Your code goes here.
   # the path to your .json file
  

   #fetch the SiteName from combobox

   $SiteName = AI_GetMsiProperty COMBOBOX_1_PROP

   if($SiteName.length -gt 12)
   {
					[System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
           $SiteNameValidation =$false

   }

   elseif( $SiteName.Length -lt 7)
   {
           [System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
           $SiteNameValidation =$false

   }

   elseif( $SiteName -match '[^a-zA-Z0-9]')
   {
           [System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
           $SiteNameValidation =$false
   }

   else
   {
           AI_SetMsiProperty COMBOBOX_1_PROP "true"
           $SiteNameValidation =$true
   }


   #fetch the  EquipmentPhysicalIdentifier from combobox

   $EquipmentPhysicalIdentifier = AI_GetMsiProperty COMBOBOX_2_PROP


   if( $EquipmentPhysicalIdentifier -match '[^a-zA-Z0-9]')
   {
    [System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
   }


  elseif (-NOT ( $EquipmentPhysicalIdentifier.StartsWith("Weir.Minerals.")))
   {
     [System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
     $EqIdValidation =$false
   }
   else
   {
     AI_SetMsiProperty COMBOBOX_2_PROP "true"
     $EqIdValidation = $true
   }

Here in the Script if the fields are valid i am setting the COMBOBOX_2_PROP and COMBOBOX_1_PROP to true.
i have added publish events to valid button as follows:

Code: Select all

Execute custom Action          ValidateUser    AI_INSTALL 
Refresh the current dialog     1               AI_INSTALL AND (COMBOBOX_1_PROP = "true") AND (COMBOBOX_2_PROP = "true")
Also i had disabled the Next button. The control conditions for this button is:

Code: Select all

(COMBOBOX_1_PROP = "true") AND (COMBOBOX_2_PROP = "true")            Enable.


After running the installer , entering the field values and upon clicking the validate button, instead of enabling the next button as expected, its going to final window with finish button displaying the following message :

Code: Select all

Transient store API ended prematurely because of an error. Your system has not been modified. 
Can you tell me where i am doing wrong?
Catalin
Posts: 6592
Joined: Wed Jun 13, 2018 7:49 am

Re: How do i throw an error message for the backend powershell script

Hello Abhinay,

That most probably is happening because your PowerShell script fails. One thing that I could notice is the fact that you are not adding the required assembly in order to use the "Show" method of the Windows Forms assemblies.

With that being said, could you please add the following line to your script:

Code: Select all

AddType -AssemblyName PresentationFramework
Also, one more thing that I noticed is the fact that you are setting the "COMBOBOX_1_PROP" to "true" if your condition is valid. Could you please change this to another property (e.g. "CONDITION_PROP"). That is in my opinion a better approach, it can make the script easier to read and also the project will be easier to understand at a later point in time. This is not mandatory though.

Hope this helps.

All the best,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Abhinay
Posts: 36
Joined: Wed Feb 27, 2019 9:33 am

Re: How do i throw an error message for the backend powershell script

We have modified the script

Code: Select all

# Block for declaring the script parameters.
AddType -AssemblyName PresentationFramework

Param()
   # Your code goes here.
   # the path to your .json file
  

   #fetch the SiteName from combobox

   $SiteName = AI_GetMsiProperty COMBOBOX_2_PROP

   if($SiteName.length -gt 12)
   {
					[System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
           $SiteNameValidation =$false

   }

   elseif( $SiteName.Length -lt 7)
   {
           [System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
           $SiteNameValidation =$false

   }

   elseif( $SiteName -match '[^a-zA-Z0-9]')
   {
           [System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
           $SiteNameValidation =$false
   }

   else
   {
           AI_SetMsiProperty CONDITION_PROP "true"
           $prop = AI_GetMsiProperty CONDITION_PROP
           [system.windows.messagebox]::show("$prop")
           $SiteNameValidation =$true
   }


   #fetch the  EquipmentPhysicalIdentifier from combobox

   $EquipmentPhysicalIdentifier = AI_GetMsiProperty COMBOBOX_1_PROP


  


  if (-NOT ( $EquipmentPhysicalIdentifier.StartsWith("Weir.Minerals.")))
   {
     [System.Windows.MessageBox]::Show('The value is invalid. Please re-enter the value.')
     $EqIdValidation =$false
   }
   else
   {
     AI_SetMsiProperty CONDITION_PROP "true"
           $prop = AI_GetMsiProperty CONDITION_PROP
           [system.windows.messagebox]::show("$prop")
     $EqIdValidation = $true
   }

   
Making "Next" button as False with control condition

Code: Select all

CONDITION_PROP = "true"
Refreshing the current dialog

Code: Select all

AI_INSTALL AND ( CONDITION_PROP = "true" )
But still its not working, i am getting the same error as before
Catalin
Posts: 6592
Joined: Wed Jun 13, 2018 7:49 am

Re: How do i throw an error message for the backend powershell script

Hello Abhinay,

That is probably happening due to the fact that you have added the assemblies before the "Param()" function.

Also, please keep in mind that it should look like this:

Code: Select all

Add-Type -AssemblyName PresentationFramework
not like this:

AddType -AssemblyName PresentationFramework

With that being said, could you please modify your code as it follows: Add the following line after the "Your code goes here." comment.

Code: Select all

Add-Type -AssemblyName PresentationFramework
Hope this helps.

All the best,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Abhinay
Posts: 36
Joined: Wed Feb 27, 2019 9:33 am

Re: How do i throw an error message for the backend powershell script

Thank you .. This Worked.. :D
Catalin
Posts: 6592
Joined: Wed Jun 13, 2018 7:49 am

Re: How do i throw an error message for the backend powershell script

You are always welcome, Abhinay.

I am glad it worked.

All the best,
Catalin
:D
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”