c0000005在远程PowerShell会话中运行应用程序时调用Process.Start的Access_Violation

时间:2017-02-01 12:26:28

标签: c# powershell debugging remote-access

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

这似乎有效,至少没有从通话中返回错误。但稍后在进程上引发了Exited事件,ExitCode为0xc0000007。

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

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

我已尝试使用ProcMon监控流程,以查看是否存在由于路径或访问权限而无法访问的外部资源。这没有用。

我正在运行的实际代码是:

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: 

1 个答案:

答案 0 :(得分:0)

当会话没有桌面可供使用时,这是执行流程启动的方式的问题。

非常类似于此处描述的问题 Starting a process with credentials from a Windows Service使用answer with a "compact" standalone code

中的代码引导我找到解决方案