I am trying to write a powershell script with c# in my app. the problem is the the script doesn't do the job if I run it with c#, but it does the work if I run it by hand.
That's the script: DISM /online /Enable-Feature /Featurename:NetFx3 /All
The script enables Framwork 3.5 on windows server. Source
My code:
NuGet: System.Management.Automation
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("DISM /online /Enable-Feature /Featurename:NetFx3 /All ");
// begin invoke execution on the pipeline
IAsyncResult result = PowerShellInstance.BeginInvoke();
// do something else until execution has completed.
// this could be sleep/wait, or perhaps some other work
while (result.IsCompleted == false)
{
Console.WriteLine("Waiting for pipeline to finish...");
Thread.Sleep(1000);
// might want to place a timeout here...
}
Console.WriteLine("Finished!");
}
I used examples from Here
Note: my app run as admin.
PowerShellInstance.HadErrors. If that's true, checkPowerShellInstance.InvocationStateInfo.Reasonfor the exception, if that's null checkPowerShellInstance.Streams.Errorfor non-terminating error records. If none of those give the information you needPowerShellInstance.EndInvoke(result)will give the output of the command.