sasha
Posts: 91
Joined: Fri Oct 20, 2023 8:37 pm

Problems running PowerShell scripts

Hello!

Our installer is using inline PS scripts (and at one point an external PS script).
Reports started coming in about installer failure.
The reason turned out to be tightly locked-down environments where the PowerShell is running in the Restricted or Constrained language mode.

Here is a representative error for an inline PS script:

Code: Select all

MSI (c) (FC!40) [12:06:34:528]: PROPERTY CHANGE: Adding POWERSHELL_EXECUTION_LOG property. Its value is 'C:\Users\XXX\AppData\Local\Temp\pssC4EF.ps1 : Cannot dot-source this command because it was defined in a 
different language mode. To invoke this command without importing its contents, omit the '.' operator.
    + CategoryInfo          : InvalidOperation: (:) [pssC4EF.ps1], NotSupportedException
    + FullyQualifiedErrorId : DotSourceNotSupported,pssC4EF.ps1
We were invoking an attached external PS script via the dot-source operator, but since migrated to native CAs in a DLL. But we have no control over how inline scripts are launched by the AI PS engine.

Do you have a suggestion on how to resolve such issues?

Thanks!
Liviu
Posts: 1419
Joined: Tue Jul 13, 2021 11:29 am
Contact: Website

Re: Problems running PowerShell scripts

Hello,

Thank you for the detailed report.

Our PowerShell custom actions don't have support for PowerShell Constrained Language Mode.

The available workaround would be to use a different type of custom action, such as a C# custom action, or avoid the Inline PowerShell custom action and launch the script explicitly through powershell.exe.

Instead of using -File, you can invoke the script through -Command and the call operator, for example:

Code: Select all

powershell.exe -Command "& { & 'C:\Path\YourScript.ps1' <arguments> }"
This executes the script instead of dot-sourcing it into the current scope and has been useful in similar Constrained Language / WDAC environments.

You can configure this using a Launch EXE with Working Directory custom action, with powershell.exe as the executable.

One downside of this approach is that the PowerShell script needs to exist as a file on the target machine, for example under the application folder, and may remain there after installation unless you explicitly remove it. Since the script is allowed to execute in a highly restricted WDAC environment, this can also slightly increase the attack surface compared to using an inline script.

If you want to avoid that entirely, a native or C# custom action would be the safer option.

Let me know if there is anything else I can help you with.

Best regards,
Liviu
________________________________________
Liviu Sandu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
sasha
Posts: 91
Joined: Fri Oct 20, 2023 8:37 pm

Re: Problems running PowerShell scripts

Thanks for the reply, Liviu.

The inline scripts are used mostly to set various internal properties. So, invoking the external scripts via powershell.exe may not work well, I suspect.

Looks like I will need to convert them all to 'native' CAs invoked from a DLL.
Thanks,
.a

p.s. it's rather ironic, that having deprecated VBScript, MSFT forced the move to PowerShell, but it suffers from such severe restrictions.
sasha
Posts: 91
Joined: Fri Oct 20, 2023 8:37 pm

Re: Problems running PowerShell scripts

Perhaps - for the future - AI could add a new PowerShell-based CA that implements your suggestion of using the -Command?
Basically, an inline script that is launched not via dot-source, but using your proposed workaround.

Thanks,
.a
Liviu
Posts: 1419
Joined: Tue Jul 13, 2021 11:29 am
Contact: Website

Re: Problems running PowerShell scripts

Hello,

I agree that having an improvement here could be useful for such environments.

I have forwarded this as a feature/improvement request to our development team for consideration.

Thank you for the detailed feedback.

Best regards,
Liviu
________________________________________
Liviu Sandu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”