I'm working on a small C# program to uninstall and reinstall windows appxpackages. I've used many of the powershell commands manually and they all work fine if ran via powershell however I would like to just make a button that runs the command without having to go look up the command then copy paste it into the prompt window. I've researched this method for a few days now and I've not found anything helpful. Everywhere I look other users have done something like this however while this will compile this will not do anything. Perhaps I am just not seeing something or perhaps not understanding something. Can anyone please help!
Powershell commands:
Get-AppxPackage *windowsstore* | Remove-AppxPackage
Get-AppxPackage *windowsstore* | Add-AppxPackage
.
using System.Management.Automation;
.
private void button_Click(object sender, EventArgs e)
{
PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-AppxPackage");
ps.AddArgument("*windowsstore*");
ps.AddCommand("Remove-AppxPackage");
ps.Invoke();
Console.WriteLine("POWERSHELL OUTPUT! =( "+ps+" )");
}
Output:
POWERSHELL OUTPUT! =( System.Management.Automation.PowerShell )
You're printing the PowerShell
object directly, while what you want to do is take the output of ps.Invoke()
and process and print that.
Excerpt from the example in the documentation :
foreach (PSObject result in ps.Invoke())
{
Console.WriteLine(
"{0,-24}{1}",
result.Members["ProcessName"].Value,
result.Members["Id"].Value);
}
Here you can see how the output of ps.Invoke()
is handled.
If you want the output as text:
foreach (PSObject result in ps.Invoke())
{
Console.WriteLine("{0}", result);
}
OK scratch that. Its to convoluted and I've stopped caring.
This is what I did to solve my problem. I hope someone else will find this helpful.
//Make a folder to keep everything together.
if (!Directory.Exists(@"C:\\Folder"))
Directory.CreateDirectory(@"C:\\Folder");
var result = string.Empty;
using (var webClient = new WebClient())
{
try
{
//Download the string of text for the command from a file on the server.
// the command is just this.
//Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}
result = webClient.DownloadString("http://MyServer.net/scripts/ReinstallWinApps.ps1");
//Create a .ps1 file and dump the result into it then save.
FileStream file = new FileStream(@"C:\\Folder\\ReinstallWinApps.ps1", FileMode.Create, FileAccess.ReadWrite);
StreamWriter print_text = new StreamWriter(file);
print_text.Write(result);
print_text.Close();
//Run the file as admin.
ProcessStartInfo PS = new ProcessStartInfo(@"C:\\Folder\\ReinstallWinApps.ps1");
PS.UseShellExecute = true;
PS.Verb = "runas";
Process.Start( PS );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
If you find a better way to do this please give back to the world. I spent a week trying to get the powershell method working and no dice. I wish you all the best of luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.