0
var newProcessInfo = new System.Diagnostics.ProcessStartInfo();
newProcessInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
newProcessInfo.Verb = "runas";
newProcessInfo.Arguments = @"-executionpolicy unrestricted -Command ""C:\Windows\system32\sfc.exe /scannow""";


/*
newProcessInfo.Arguments = @"-Command ""sfc /scannow""";
newProcessInfo.Arguments = @"-File ""C:\my\script.ps1""";
newProcessInfo.Arguments = @"–ExecutionPolicy Bypass -File ""C:\my\script.ps1"""; */

These are basically the basic style code commands I want to use. I have been having a problem -executionpolicy unrestricted. It works great when I type it in powershell just like above.

Also, what is the comparable coding in powershell to cmd.exe /k so the command prompt window stays open for troubleshooting?

DDJ
  • 807
  • 5
  • 13
  • 31

2 Answers2

1

After seeing your error, I can say for sure that that error is coming frm the sfc command that you're running. The command is being executed.

Execution Policy is about how to handle script files, so it has no effect on commands run immediately (unless you reference a script file in a command). It won't help with your particular issue I'm afraid.


What makes you think your execution policy is not working?

Since you're not using the -File parameter in the uncommented code, execution policy should be irrelevant anyway.

The analogous powershell command for cmd.exe /k is powershell.exe -NoExit.

briantist
  • 45,546
  • 6
  • 82
  • 127
  • "Windows Resource Protection could not start the repair service" That is the error that shows. I thought the -executionpolicy unrestricted would fix this. – DDJ Nov 17 '15 at 22:56
  • I turned off the trusted installer"net stop TrustedInstaller" and thought this might help, but still have the issue for "SFC /SCANNOW" – DDJ Dec 09 '15 at 05:00
0

Answer found on Stackoverflow(click here)

...
using System.Diagnostics;
...

private void button4_Click(object sender, EventArgs e)
{
    string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    string snat = Environment.GetEnvironmentVariable("windir") + @"\sysnative\sfc.exe";

    Process a = new Process();
      a.StartInfo.FileName = snat;
      a.StartInfo.Arguments = "/SCANNOW";
      a.StartInfo.UseShellExecute = false;
      a.StartInfo.RedirectStandardOutput = true;
      a.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      a.StartInfo.CreateNoWindow = true;
    a.Start();

    string output = a.StandardOutput.ReadToEnd();
    a.WaitForExit();

    MessageBox.Show("DONE!");
}
catch
{
MessageBox.Show("error!");
}
Community
  • 1
  • 1
DDJ
  • 807
  • 5
  • 13
  • 31