C
C#3y ago
Godspeed

Running PowerShell to install MSIX from .NET MAUI

I am trying to implement a custom auto-updating solution, as the Appinstaller program has issues with the latest Windows Security Update. I am running a process that checks for new update, and if there is one, it downloads the new MSIX. I am able to download the MSIX file, and I am trying to install it using the PowerShell.Core.SDK, with the following code:
var prefix = Path.Combine(FileSystem.Current.AppDataDirectory, "Downloads");
var filePath = Path.Combine(prefix, "myappname.msix"); // <-- Confirmed to exist, not the issue

var scriptContents = new StringBuilder();
scriptContents.AppendLine("Param($FilePath, $ForceShutdown)");
scriptContents.AppendLine("Add-AppxPackage $FilePath $ForceShutdown");

var scriptParameters = new Dictionary<string, object>()
{
{ "FilePath", filePath },
{ "ForceShutdown", "-ForceApplicationShutdown" } // <-- Or -DeferRegistrationWhenPackagesAreInUse
};

using PowerShell ps = PowerShell.Create();
ps.AddScript(scriptContents.ToString());
ps.AddParameters(scriptParameters);
await ps.InvokeAsync().ConfigureAwait(false);
var prefix = Path.Combine(FileSystem.Current.AppDataDirectory, "Downloads");
var filePath = Path.Combine(prefix, "myappname.msix"); // <-- Confirmed to exist, not the issue

var scriptContents = new StringBuilder();
scriptContents.AppendLine("Param($FilePath, $ForceShutdown)");
scriptContents.AppendLine("Add-AppxPackage $FilePath $ForceShutdown");

var scriptParameters = new Dictionary<string, object>()
{
{ "FilePath", filePath },
{ "ForceShutdown", "-ForceApplicationShutdown" } // <-- Or -DeferRegistrationWhenPackagesAreInUse
};

using PowerShell ps = PowerShell.Create();
ps.AddScript(scriptContents.ToString());
ps.AddParameters(scriptParameters);
await ps.InvokeAsync().ConfigureAwait(false);
I am having issues getting the Add-AppxPackage command to work from the instantiated PowerShell. I retrieve errors from ps.Streams.Errors. The main error output I get is {The 'Add-AppxPackage' command was found in the module 'Appx', but the module could not be loaded. For more information, run 'Import-Module Appx'.}. Thus, I tried adding scriptContents.AppendLine("Import-Module Appx");. This only resulted in the error {Operation is not supported on this platform. (0x80131539)}. Googling a bit, I found a GitHub issue referencing this error, https://github.com/PowerShell/PowerShell/issues/13138. Thus, I added the flag to it as such: scriptContents.AppendLine("Import-Module Appx -UseWindowsPowerShell");. I am not sure if this is a good solution on its own, as some comments mentioned that this flag is not supported on Windows 11. Anyway, adding this flag avoid the last error, but returns us to the first error {The 'Add-AppxPackage' command was found in the module 'Appx', but the module could not be loaded. For more information, run 'Import-Module Appx'.}. Q: How can I avoid this error? Am I missing something? Is it not possible? I need this to work on a variety of Windows machines (personal, consumer units). Is there a better approach than the PowerShell.Core.SDK?
6 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Godspeed
GodspeedOP3y ago
Is there nothing I can do but wait till the Appx module is fixed somehow? Aside from running powershell.exe in a separate process
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Godspeed
GodspeedOP3y ago
Did not know there was one - I'll try to find it and ask there It seems that using a Process.Start( powershell.exe, script ) works fine, in the meanwhile but then I do not use the PowerShell.Core.SDK, which I kinda want to learn and use
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Godspeed
GodspeedOP3y ago
I will give it a try!
Want results from more Discord servers?
Add your server