0

I need to run this command from cd.bat

powershell -windowstyle hidden -command "Start-Process cmd -ArgumentList '/c takeown /f \"C:\Windows\System32\wuaueng.dll\" && icacls \"C:\Windows\System32\wuaueng.dll\" /grant *S-1-3-4:F /t /c /l' -Verb runAs"

When i run this BAT manualy from right click -> Run as Administrator it work perfect, when i try to Run from CMD:

Process.Start(Application.StartupPath + "\\cd.bat");

CMD Start but command wont work, why?

My Application has Administrative permission in Manifest

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

I have also tried with normal command:

proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.Arguments = "/c powershell -windowstyle hidden -command \"Start - Process cmd - ArgumentList '/c takeown /f \"C:\\Windows\\System32\\wuaueng.dll\" && icacls \"C:\\Windows\\System32\\wuaueng.dll\" /grant *S-1-3-4:F /t /c /l' - Verb runAs\"";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();

and

 var p = new Process();
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.WorkingDirectory = @"C:\Windows\System32";
            p.StartInfo.Arguments = "/k powershell -windowstyle hidden -command \"Start - Process cmd - ArgumentList '/c takeown /f \"C:\\Windows\\System32\\wuaueng.dll\" && icacls \"C:\\Windows\\System32\\wuaueng.dll\" /grant *S-1-3-4:F /t /c /l' - Verb runAs\"";
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardInput = false;
            p.StartInfo.UseShellExecute = false;
            p.OutputDataReceived += (a, b) => line = line + b.Data + Environment.NewLine;
            p.ErrorDataReceived += (a, b) => line = line + b.Data + Environment.NewLine;
            p.Start();
            p.BeginErrorReadLine();
            p.BeginOutputReadLine();

            MessageBox.Show(line);

MessageBox result Empty, so no error...

Same result =(

Marcus J.Kennedy
  • 680
  • 5
  • 22
  • Try reading from the processes standard output and standard error streams. (Process p = Process.Start("\\cd.bat"); p.StandardOutput ...) This may tell you what the problem is. – George Apr 18 '19 at 16:07
  • No error, no output... – Marcus J.Kennedy Apr 18 '19 at 16:18
  • Maybe I'm missing something, but cmd.exe and batch files don't execute powershell commands. Can't you call powershell.exe directly https://stackoverflow.com/a/45645978/3225 – kenny Apr 18 '19 at 16:32
  • My guess is because you've named the batch file `cd.bat`, and `cd` is an internal CMD.Exe command (`cd`, which lists or changes the current directory). Try renaming it to something that doesn't conflict with an internal system command and see if that helps. – Ken White Apr 18 '19 at 16:36
  • @kenny thank you, i have solved! – Marcus J.Kennedy Apr 18 '19 at 16:40

1 Answers1

0

Solved with:

Process.Start("powershell.exe",
                 "-windowstyle hidden -command "
                + "Start-Process cmd -ArgumentList '/c takeown /f \"C:\\Windows\\System32\\wuaueng.dll\" && icacls \"C:\\Windows\\System32\\wuaueng.dll\" /grant *S-1-3-4:F /t /c /l' -Verb runAs"
            );
Marcus J.Kennedy
  • 680
  • 5
  • 22