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

How to hide your PowerShell custom action code from the log

Hello everyone,

I've recently had a customer asking how he can hide the code of his PowerShell custom action from the log file.

Let's consider the following scenario, where we have a PowerShell inline script added in our project, as a "deferred" custom action.
Screenshot_180.png
Screenshot_180.png (123.28 KiB) Viewed 48090 times
Now, if we build the MSI and run it using the log option, e.g.:

Code: Select all

msiexec /i <path_to_our_msi> /l*V <path_to_our_log>
Screenshot_181.png
Screenshot_181.png (19.23 KiB) Viewed 48087 times
we will notice the following in the log file:

Code: Select all

MSI (s) (14:1C) [11:38:54:131]: Doing action: AI_DATA_SETTER
Action 11:38:54: AI_DATA_SETTER. 
Action start 11:38:54: AI_DATA_SETTER.
MSI (s) (14:1C) [11:38:54:131]: PROPERTY CHANGE: Adding My_Sample_CA property. Its value is 'ParamsScript#Requires -version 3
Param()

# Let's see which Powershell we're using. Feel free to remove this function
Function Say-Hello() {
  # Powershell Core (pwsh.exe) is used when Requires -version is greater or equal to 6
  # By default, Windows PowerShell (powershell.exe) will be used. Let's see what we're using now...

  # Access Windows Installer properties using Set-Property and Get-Property
  Set-Property -name "PSVersion" -value $host.Version.ToString()
  [string] $helloMessage = "Hello from PowerShell " + (Get-Property -name "PSVersion")
  $helloMessage += If ([Environment]::Is64BitProcess) { " 64bit" } else { " 32bit" }
  
  # Writing messages to the Windows Installer log is also easy
  Write-Output $helloMessage
  
  # When testing or debugging your script, you can quickly display a message box
  [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
  [System.Windows.Forms.MessageBox]::Show($helloMessage)
}

# Your code goes here
Sa
Action ended 11:38:54: AI_DATA_SETTER. Return value 1.
MSI (s) (14:1C) [11:38:54:131]: Doing action: My_Sample_CA
Action 11:38:54: My_Sample_CA. 
Action start 11:38:54: My_Sample_CA.
As we can see, all our code is visible in the log file.

Now, don't get me wrong, the log file is meant specifically for debugging purposes, but sometimes you might not want your code to be visible in there.

What's important to note here is the fact that all that code is basically inside a Windows Installer property, which has the name of our Custom Action (My_Sample_CA).

Now, in order to hide it from the log file, we can set the "Hidden" attribute for this property. To do so, please:
  • go to "Properties" page
  • click on "New property"
Screenshot_182.png
Screenshot_182.png (11.96 KiB) Viewed 48087 times

Another thing to be kept in mind here is the fact that the code actually appears twice: once in the proeprty mentioned above and another one when the custom action is actually executed.

For the second case, we can make use of the following:

Custom Action Hidden Target Option


What we have to do is modify the value from the "Table Editor", as shown in the attached screenshot:
Screenshot_184.png
Screenshot_184.png (302.53 KiB) Viewed 48090 times

The default value is 1025 to which we add the 8192, resulting in 9217, as in the screenshot.

Now rebuild the project and rerun the installation command.

And voila, the code should now be hidden from the log file. :D
Screenshot_183.png
Screenshot_183.png (17.21 KiB) Viewed 48090 times

Hope this helps!

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

Return to “Sample Projects”