我创建了一个进程处理程序,它启动两种类型的进程:
使用管理员用户名和密码提升的权限
另一种无需输入任何用户名和密码即可正常运行。
我正在努力弄清楚如何从提升的进程中获取输出。启动进程的应用程序不需要管理员凭据即可运行,管理员凭据输入到单独的加密 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;
}
我也尝试过使用 Impersonator 类来启动该流程,无论是否添加了管理员凭据。模仿者类没有做任何事情,只是告诉我我没有访问权限,尽管我正在模仿管理员......
我从这里得到了 Impersonator 类:
http://freshclickmedia.co.uk/2008/11/programmatic-impersonation-in-c/ http://freshclickmedia.co.uk/2008/11/programmatic-impersonation-in-c/
那么,如何在未提升的进程中从提升的进程获取标准和错误输出?