【发布时间】:2019-04-18 15:47:13
【问题描述】:
我需要从 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"
当我通过右键单击手动运行此 BAT -> 以管理员身份运行时,它运行完美,当我尝试从 CMD 运行时:
Process.Start(Application.StartupPath + "\\cd.bat");
CMD 启动但命令不起作用,为什么?
我的应用程序在 Manifest 中具有管理权限
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
我也尝试过使用普通命令:
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();
和
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 结果为空,所以没有错误...
同样的结果 =(
【问题讨论】:
-
尝试从进程标准输出和标准错误流中读取。 (Process p = Process.Start("\\cd.bat"); p.StandardOutput ...) 这可能会告诉您问题所在。
-
无错误,无输出...
-
也许我遗漏了一些东西,但是 cmd.exe 和批处理文件不执行 powershell 命令。不能直接调用powershell.exe吗stackoverflow.com/a/45645978/3225
-
我猜是因为您将批处理文件命名为
cd.bat,而cd是一个内部CMD.Exe 命令(cd,它列出或更改当前目录)。尝试将其重命名为不与内部系统命令冲突的名称,看看是否有帮助。 -
@kenny 谢谢你,我已经解决了!
标签: c# batch-file