我创建了一个启动两种类型进程的进程处理程序: 用管理员用户名和密码提升的进程处理程序 另一个运行正常,没有任何用户名和密码输入。从高架子进程获取错误和标准输出
我struggeling弄清楚如何从高架过程获取输出。启动进程的应用程序不需要管理员凭据运行,管理员凭据将输入到单独加密的xml文件中,该文件应用程序在需要管理员凭证的脚本和其他位置使用该文件。
由于应用程序与正常用户运行,访问该应用程序已经开始升高的过程中,似乎是不可能的。我可以开始一个过程,并且可以轻松地检查它是否完成了它所支持的过程,但我无法将其操作读取到字符串或日志中。
public bool CreateProcessWithAdminRights(string filePath, string commandlineArgument, bool log)
{
if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(commandlineArgument) && _user.UserDataExsists())
{
var securePassword = GetSecureString(_user.Password);
ToolsProvider.Logger.Debug("Creating process with the following filepath: {0} and commandline argument: {1}", filePath, commandlineArgument.Replace(_user.Password, "<REPLACED>"));
ToolsProvider.Logger.Info("Creating Process with admin rights for {0} against {1}", _user.Name);
_proc = new Process
{
StartInfo =
{
FileName = @filePath,
Arguments = commandlineArgument,
ErrorDialog = false,
RedirectStandardInput = false,
RedirectStandardOutput = _log,
RedirectStandardError = _log,
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UserName = _user.Name,
Password = securePassword,
Domain = _user.Domain
}
};
_proc.ErrorDataReceived += ErrorDataReceived;
_proc.OutputDataReceived += OutputDataReceived;
return true;
}
return false;
}
的进程正在使用开始:
private bool StartProcess()
{
if (_proc != null)
{
try
{
_proc.Start();
_proc.BeginErrorReadLine();
_proc.BeginOutputReadLine();
_proc.WaitForExit();
_proc.CancelOutputRead();
_proc.CancelErrorRead();
if (_standardOutput.Length > 2)
{
// use writeline, the builder itself will add the DEBUG/info tag
ToolsProvider.Logger.WriteLine(_standardOutput.ToString());
}
if (_errorBuilder.Length > 2)
{
// use writeline, the builder itself will add the DEBUG/info tag
ToolsProvider.Logger.WriteLine(_errorBuilder.ToString());
}
return true;
}
catch (Win32Exception ex)
{
ToolsProvider.Logger.Error(
"Missing file while trying to run an action: " + _proc.StartInfo.FileName, ex.Message);
}
}
ToolsProvider.Logger.Error("");
return false;
}
我试着用模仿类为好,有和没有加入到过程中的管理员凭据启动进程。假冒者类没有做任何事情,但告诉我,我没有acccess沉绵我冒充管理员...
我从这里冒领类:
http://freshclickmedia.co.uk/2008/11/programmatic-impersonation-in-c/
所以,如何在不升高的过程中从升级过程中获得标准和错误输出?
您可以使用'Named Pipes':http://msdn.microsoft.com/en-us/library/bb546085.aspx – SepehrM