Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the Local System account is not allowed to perform powershell script in a custom action #1680

Closed
ltemimi opened this issue Nov 30, 2024 · 5 comments

Comments

@ltemimi
Copy link

ltemimi commented Nov 30, 2024

HI

I have a custom action that uses power shell to install an app using an msix installer
(Custom action => poweshell=> launches an msix fiel to install an app)

The error I get:

ActionStart: Action 16:40:46: InstallMsix.
Info: SFXCA: Extracting custom action to temporary directory: C:\WINDOWS\Installer\MSI73F6.tmp-0
Info: SFXCA: Binding to CLR version v4.0.30319
Info: Calling custom action PlcData.InstallerApp!PlcData.InstallerApp.CustomActions.InstallMsix
Info: Attempting to un-install MSIX package from path: C:\Program Files (x86)\ACL\Apps\PlcData.LoggingService\License.Manager_1.0.0.0_x64.msix
Info: PowerShell Output:
Info: PowerShell Error: Add-AppxPackage : Deployment failed with HRESULT: 0x80073CF9, Install failed. Please contact your software vendor.
(Exception from HRESULT: 0x80073CF9)
Deployment Add operation rejected on package com.automationconsultancy.licensemanager_1.0.0.0_x64__743hwmv1h732c from:
License.Manager_1.0.0.0_x64.msix install request because the Local System account is not allowed to perform this
operation.

@ltemimi ltemimi changed the title the Local System account is not allowed to perform powershell script in a custome action the Local System account is not allowed to perform powershell script in a custom action Nov 30, 2024
@ltemimi
Copy link
Author

ltemimi commented Nov 30, 2024

by adding impersonation and running a command prompt in admin mode and using msiexec /i installation works . also if I wrap the msi in a bootstrap bundle and run the exe in admin mode.
The issue I have my customers are not all tech to run cmd in admin mode and downloading exe files are full of scary warning.

Is there a way to run an msi in admin mode by just clicking on it?

  new ElevatedManagedAction(CustomActions.InstallService,
      Return.check,
      When.After,
      Step.InstallFiles,
      Condition.NOT_Installed)
  {
      Impersonate = true  // Add this line
  },

@oleg-shilo
Copy link
Owner

oleg-shilo commented Dec 2, 2024

I would try to implement an alternative technique.
You do not need to resort to the powereshell. You can simply start the process from any custom action:

"notepad.exe".StartElevated("c:\\boot.ini"); // this will pop the elevation prompt

Or better yet, use project.AfterInstall event. It is a special custom action that is already elevated for you.

StartElevated does not do any magic. It's a simple wrapper around Process.Start:

public static Process StartElevated(this string fileName, string args = "")
{
    bool alreadyAdmin = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);

    return Process.Start(new ProcessStartInfo
    {
        WorkingDirectory = fileName.PathGetFullPath().PathGetDirName(),
        FileName = fileName,
        Arguments = args,
        UseShellExecute = true,
        Verb = alreadyAdmin ? "" : "runas"
    });
}

@ltemimi
Copy link
Author

ltemimi commented Dec 2, 2024

Hi Oleg

thanks I agree project.AfterInstall would help but then which event can I use when I uninstall

Laz

@oleg-shilo
Copy link
Owner

The same event. This event is fired after the install session is complete. And the install session can be one of three types: install/repair/uninstall.
The event args will give all required context: install dir, type of the installation. This is an example:

project.AfterInstall += args =>
{
    if (!args.IsUninstalling)
        args.InstallDir.PathJoin("MyApp.msix").StartElevated("-uninstall");
    else
        args.InstallDir.PathJoin("MyApp.msix").StartElevated("-install");
};

@ltemimi
Copy link
Author

ltemimi commented Dec 4, 2024

Thanks that is great

@ltemimi ltemimi closed this as completed Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants