Reputation: 99
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.
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 --
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.
Upvotes: 2
Views: 609
Reputation: 99
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.
Upvotes: 1