CreateProcessWithLogonW退出代码

时间:2015-10-06 11:49:13

标签: c# winapi vbscript impersonation wsh

我正在尝试捕获通过CreateProcessWithLogonW创建的进程的退出代码。正在启动的进程是wscript,因为我正在运行VBScript文件。我正在使用此API调用b / c,需要让当前用户冒充服务帐户。当我尝试捕获脚本返回的退出代码时,我收到一个拒绝访问的错误,我认为这是因为该进程是在服务帐户下创建的。我尝试在尝试捕获退出代码之前模拟用户,但是由于重复的令牌错误而失败。有谁知道如何在这种特定情况下正确捕获退出代码?我不能使用Marshal.GetLastWin32Error(),因为这不会给我脚本失败或成功时返回的退出代码。非常感谢任何帮助。

    public static Process_Info Run_Command_As(String str_Command, String str_Working_Directory,
    String str_Domain, String str_Name, String str_Password)
{
    // DECLARE VARIABLES
    Process_Info pi_Process_Info = new Process_Info();
    Startup_Info si_Start_Info = new Startup_Info();  

    // CREATE PROCESS
    si_Start_Info.i_CB = Marshal.SizeOf(si_Start_Info);

    try
    {
    // GET RESULT OF PROCESS CREATION
    Boolean b_Result = CreateProcessWithLogonW(
        str_Name,
        str_Domain,
        str_Password,
        0,
        null,
        str_Command,
        0,
        IntPtr.Zero,
        str_Working_Directory,
        ref si_Start_Info,
        out pi_Process_Info
    );

    // CHECK IF NOT CREATED SUCCESSFULLY
    if (!b_Result)
    {
        // THROW EXCEPTION
        throw new Exception(String.Format("CreateProcessWithLogonW Error #{0}",
        Marshal.GetLastWin32Error()));
    }
    }
    catch(Exception Ex)
    {
    // CLOSE HANDLES
    CloseHandle(pi_Process_Info.ip_Process);
    CloseHandle(pi_Process_Info.ip_Thread);
    }

    return pi_Process_Info;
}

/ CALL TO CREATE NEW PROCESS INFO OBJECT AND SET EXECUTABLE INFO OBJECT
Process_Info Process_Info = Run_Command_As(String.Format("wscript.exe {0}",
    Path.Combine(Server_Info.Server, Executable_Info.Location, Command)), Path.Combine(Server_Info.Server, Executable_Info.Location),
    Credential.Domain, Credential.Username, Credential.Password);

// GET PROCESS BY ID
Process Proc = Process.GetProcessById(Process_Info.i_Process_ID);
if (!Proc.HasExited) { Proc.WaitForExit(); }
Int32 i_Exit_code = Proc.ExitCode;

0 个答案:

没有答案