c0000005 Access_Violation 当 运行 应用程序在远程 powershell 会话中调用 Process.Start

c0000005 Access_Violation calling Process.Start when running app in remote powershell session

我有一个场景,我需要通过远程 powershell 会话执行一些代码,代码在内部调用 Process.Start() 传入(使用 ProcessStartInfo)。

这似乎可行,至少调用没有返回错误。但后来在进程上引发了 Exited 事件,ExitCode 为 0xc0000007。

如果我 运行 直接在同一台机器上使用相同的用户凭据使用相同的代码一切 运行 都很好,所以我得出的结论是远程会话是问题的原因。

我的问题是在这些情况下如何最好地诊断 Access_Violation 的根本原因。

我试过用 ProcMon 监控进程,看看是否有外部资源由于路径或访问权限而无法访问。这没有帮助。

我运行ning的实际代码是:

this.debug = new StringBuilder();
this.debug.AppendLine("trying to start driver service");
this.driverServiceProcess = new Process();
if (this.user != null)
{
   this.driverServiceProcess.StartInfo.UserName = user.Name;
   this.driverServiceProcess.StartInfo.Password = user.Password;
   this.driverServiceProcess.StartInfo.Domain = user.Domain;
}
this.driverServiceProcess.StartInfo.FileName = Path.Combine(this.driverServicePath, this.driverServiceExecutableName);
this.driverServiceProcess.StartInfo.Arguments = this.CommandLineArguments;
this.driverServiceProcess.StartInfo.UseShellExecute = false;
this.driverServiceProcess.StartInfo.CreateNoWindow = this.hideCommandPromptWindow;
this.driverServiceProcess.StartInfo.LoadUserProfile = true;
this.driverServiceProcess.Exited += (s, e) =>
{
    this.debug.AppendFormat("process exitted at '{0}'{1}", this.driverServiceProcess?.ExitTime, Environment.NewLine);
    this.debug.AppendFormat("with code '{0}'{1}", this.driverServiceProcess?.ExitCode, Environment.NewLine);
 };
 var started = this.driverServiceProcess.Start();
 this.debug.AppendLine("after start call, but before wait, process is " + (this.driverServiceProcess.Responding ? string.Empty : "not ") + "responding and started = " + started );
 bool serviceAvailable = this.WaitForServiceInitialization();

 if (!serviceAvailable)
 {
     string msg = "Cannot start the driver service on '" + this.ServiceUrl + "' {" + this.debug.ToString() + "}";
     throw new WebDriverException(msg);
 }

其中有问题的 exe 是来自 Selenium 的 IEDriverService,WaitForServiceInitialization() 方法向驱动程序服务发出 Web 请求并期望得到满意的响应:

try
{
    Uri serviceHealthUri = new Uri(this.ServiceUrl, new Uri(DriverCommand.Status, UriKind.Relative));
    HttpWebRequest request = HttpWebRequest.Create(serviceHealthUri) as HttpWebRequest;
    request.KeepAlive = false;
    request.Timeout = 5000;
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;

    // Checking the response from the 'status' end point. Note that we are simply checking
    // that the HTTP status returned is a 200 status, and that the resposne has the correct
    // Content-Type header. A more sophisticated check would parse the JSON response and
    // validate its values. At the moment we do not do this more sophisticated check.
    this.debug.AppendLine("status returned from initialized check is " + response.StatusCode);
    isInitialized = response.StatusCode == HttpStatusCode.OK && response.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);
    response.Close();
}
catch (WebException ex)
{
    this.debug.AppendLine(DateTime.Now + "web exception in isinitialized: " + ex.Message);
    this.debug.Append(DateTime.Now + ex.InnerException.Message);
}

由于发送请求时端口未打开和监听,因此未收到,遇到异常:

Cannot start the driver service on 'http://localhost:25531/' {
    trying to start driver service
    after start call, but before wait, process is responding
    waiting for service to initialise
    web exception in isinitialized: Unable to connect to the remote server
    No connection could be made because the target machine actively refused it 127.0.0.1:25531
    process exitted at '1/31/2017 12:21:00 PM'
    with code '-1073741819'at 1/31/2017 12:21:01 PM 
    service not running, returning from initialised check early
    wait for initialized returning False
}

来自事件日志的信息是:

Faulting application name: IEDriverServer.exe, version: 3.0.0.0, time stamp: 0x57ffc8fb
Faulting module name: KERNEL32.DLL_unloaded, version: 6.3.9600.17415, time stamp: 0x545049be
Exception code: 0xc0000005
Fault offset: 0x0001a720
Faulting process ID: 0x78f8
Faulting application start time: 0x01d27c828a7897b0
Faulting application path: C:\...\IEDriverServer.exe
Faulting module path: KERNEL32.DLL
Report ID: c86215a1-e875-11e6-82d4-ecb1d72e6816
Faulting package full name: 
Faulting package-relative application ID: 

在此之前:

Faulting application name: conhost.exe, version: 6.3.9600.17415, time stamp: 0x5450410b
Faulting module name: USER32.dll, version: 6.3.9600.18438, time stamp: 0x57ae642e
Exception code: 0xc0000142
Fault offset: 0x00000000000ecdd0
Faulting process ID: 0x8158
Faulting application start time: 0x01d27c828a7d8ce2
Faulting application path: C:\WINDOWS\system32\conhost.exe
Faulting module path: USER32.dll
Report ID: c835d4bf-e875-11e6-82d4-ecb1d72e6816
Faulting package full name: 
Faulting package-relative application ID: 

这是在会话没有桌面可言时执行进程启动方式的问题。

与此处描述的问题非常相似 Starting a process with credentials from a Windows Service which lead me to the solution using the code in the answer with a "compact" standalone code