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.
- 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 --
- 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.