1

我正在尝试使用 Process 类执行批处理文件。此代码位于我使用 LogonUser() 和 WindowsIdentity.Impersonate() 模拟本地 PC 管理员帐户的较大部分代码的中间。

我试图在进程中运行批处理文件,而不在 ProcessStartInfo 中添加凭据,但是这样做会导致批处理文件静默失败 - 没有引发错误,并且从未返回批处理文件的预期输出(我正在阅读stderr 和 stdout 异步,fwiw)。

然后,我将凭据添加到 ProcessStartInfo,但现在如果我不先调用 WindowsImpersonationContext.Undo(),我会收到“拒绝访问”错误,如果我调用 .Undo,则会收到“登录失败:未知用户名或密码错误”错误() 在 Process.Start() 之前。我已经三次检查了多个帐户的用户名/密码/域是否正确。

如果我的代码没有 LogonUser() 或 WindowsIdentity.Impersonate() 调用(并且 ProcessStartInfo 中没有凭据),那么批处理文件的执行和捕获的批处理文件的输出没有问题。

我能够以本地管理员或任意本地用户帐户的身份从桌面成功运行批处理文件。我可以看到它的权限表明它应该可以从我尝试运行它的帐户中读取/可执行。这真的很困难。任何帮助表示赞赏。

4

2 回答 2

0

你在寻找这样的东西吗?

Process proc = new Process();
proc.StartInfo.FileName = @"C:\WINNT\notepad.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;

proc.StartInfo.Domain = "mydomain.com"; // Domain of IIS Computer
proc.StartInfo.UserName = "kaung"; //Administrator for that computer
System.Security.SecureString password = new System.Security.SecureString();
password.AppendChar('m'); //Password
password.AppendChar('y');
password.AppendChar('p');
password.AppendChar('a');
password.AppendChar('s');
password.AppendChar('s');
password.AppendChar('w');
password.AppendChar('o');
proc.StartInfo.Password = password;

proc.Start();
于 2012-02-23T08:00:58.327 回答
0

问题是我需要重定向所有 3 个流;我只是重定向 2(out,err,not in)。这基本上解决了问题。

于 2012-02-27T03:47:16.317 回答