2

I am trying to create an application console to install an UWP package in sideload. Using the command directly in powershell works fine. The .exe of the application run by administrator:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

There are 2 commands the program runs.

  1. Installation of the certificate in TrustedPeople store like below
string certCommand0 = "Import-Certificate -FilePath THEFILEPATH -CertStoreLocation 'Cert:\\LocalMachine\\TrustedPeople' -Verbose";
string certCommand1 = certCommand0.Replace("THEFILEPATH", certificate);

-- THIS PART OF THE CODE WORKS FINE WITH THE ATTRIBUTE AS ADMINISTRATOR --

  1. Installation of the package somehow does not work. Here is the code below:
    public static string Command(string script/*, string filePath*/)
    {
      string errorMsg = string.Empty;

      ps.AddScript(script);
      ps.AddArgument("Start-Process -FilePath \"powershell\" -Verb RunAs");

      ps.AddCommand("Out-string");

      PSDataCollection<PSObject> outPutCollection = new();
      ps.Streams.Error.DataAdded += (object sender, DataAddedEventArgs e) =>
      {
         errorMsg = ((PSDataCollection<ErrorRecord>)sender)[e.Index].ToString();
      };

      IAsyncResult result = ps.BeginInvoke<PSObject, PSObject>(null, outPutCollection);

      ps.EndInvoke(result);

      StringBuilder sb = new();

      foreach (var outputItem in outPutCollection)
      {
         sb.AppendLine(outputItem.BaseObject.ToString());
      }

      ps.Commands.Clear();

      if (!string.IsNullOrEmpty(errorMsg))
          return errorMsg;

      return sb.ToString().Trim();
    }

When trying to install the package, the following error occured: "The 'Add-AppxPackage' command was found in the module 'Appx', but the module could not be loaded. For more information, run 'Import-Module Appx'"

I made some researches and find one potential solution which was to had the following line :

ps.AddArgument("Import-Module -Name Appx -UseWIndowsPowershell");

It didn't work.

Can someone help me figured it out? Thanks.

aynber
  • 22,380
  • 8
  • 50
  • 63
Flaubert TAGU
  • 67
  • 1
  • 7
  • Check which PowerShell version your application is using, the `-UseWindowsPowerShell` flag only exists in PowerShell Core (so starting with PS >= 6 IIRC) – Irwene Oct 20 '22 at 15:02
  • Glad to hear it, @FlaubertTAGU. Note that `.AddScript()` is for adding _strings_ that contain PowerShell code. Use `.AddCommand()` for script _files_ and individual commands (arguments are added with `.AddParameter()` or `.AddArgument()`) - see [this answer](https://stackoverflow.com/a/51110482/45375). – mklement0 Oct 20 '22 at 15:10
  • Maybe changing `ps.AddArgument("Import-Module -Name Appx -UseWIndowsPowershell");` to `ps.AddScript("Import-Module -Name Appx -UseWIndowsPowershell");` is enough - but note that each `.AddScript()` or `.AddCommand()` call must be followed by `.AddStatement()` if there are _multiple_ ones. If that doesn't help, you can inspect `ps.Streams.Error` for the errors that resulted, which will reveal the reason that importing failed. – mklement0 Oct 20 '22 at 17:07
  • 1
    Dear @mklement0 I found a simpler method. Instead of trying to install the .msix package from the powershell command, I just added a command to open the msix file right after the certificate has been installed correctly. Below the command – Flaubert TAGU Oct 20 '22 at 17:30
  • `var p = new Process(); p.StartInfo = new ProcessStartInfo(unpackDirectoryName) { UseShellExecute = true }; p.Start();` – Flaubert TAGU Oct 20 '22 at 17:32
  • for posting an answer. – mklement0 Oct 20 '22 at 18:18

1 Answers1

1

Thank you for community members who gave me advice. I was at the same time trying to find another way.

The simplest solution I found was to open directly the .msix file from the app console command after the certificate has been installed in the "TrustedPeople" store in "local machine".

the code to install the certificate correctly is the following:

  • string certCommand0 = "Import-Certificate -FilePath 'THEFILEPATH' -CertStoreLocation 'Cert:\\LocalMachine\\TrustedPeople' -Verbose";

  • string certCommand1 = certCommand0.Replace("THEFILEPATH", certificate);

To open the .msix file package I used the following code:

  • var p = new Process();
  • p.StartInfo = new ProcessStartInfo(filePath){ UseShellExecute = true };
  • p.Start();

Hope it will help someone.

Flaubert TAGU
  • 67
  • 1
  • 7