murrayju / CreateProcessAsUser

Creates a process in a different Windows session
MIT License
369 stars 114 forks source link

[Help Wanted] Work as service but not as command line #29

Open koraniar opened 5 years ago

koraniar commented 5 years ago

I modified DemoService to run ProcessExtensions.StartProcessAsCurrentUser("cmd.exe", "/d /s /c \"node Path\\To\\file\""), I have installed it and it works perfect,

So I made an executable to receive the route to execute as a parameter by this way

private static int RunAndReturnExitCode(RunVerb opts)
{
    string command = opts.commands.FirstOrDefault();
    Console.WriteLine(command); // This prints -> node Path\To\file 
    if (command == null || command == "")
    {
        return 1;
    }

    ProcessExtensions.StartProcessAsCurrentUser("cmd.exe", $"/d /s /c \"{command}\"");
    return 0;
}

I launch a CMD as System user using psexec with this command PsExec64.exe -i -s cmd.exe and at runnning the command CreateProcessAsUserCMD.exe --run "node Path\To\file" it responses an error

System.Exception: StartProcessAsCurrentUser: CreateProcessAsUser failed.  Error Code -2
   en murrayju.ProcessExtensions.ProcessExtensions.StartProcessAsCurrentUser(String appPath, String cmdLine, String workDir, Boolean visible) en C:\GitHub\CreateProcessAsUser\ProcessExtensions\ProcessExtensions.cs:l�nea 249
   en CreateProcessAsUserCMD.Program.RunAndReturnExitCode(RunVerb opts)
   en CreateProcessAsUserCMD.Program.<>c.<Main>b__0_0(RunVerb opts)
   en CommandLine.ParserResultExtensions.MapResult[TSource,TResult](ParserResult`1 result, Func`2 parsedFunc, Func`2 notParsedFunc)
   en CreateProcessAsUserCMD.Program.Main(String[] args)

I have tried to launch the executable from an installed service but it responses the same error.

BryanLaValley commented 4 years ago

Try this: ProcessExtensions.StartProcessAsCurrentUser("cmd.exe", "cmd.exe arguments")