none
c# execute more commands with psexec as system RRS feed

  • Question

  • I have to connect to a remote workstation with psexec as system then map a network drive with a specific letter and then run a script on that disk.

    I'm able to connect with psexec but I don't know how to run more command on the same psexec's session.

    Thanks

    Friday, November 16, 2018 7:57 PM

All replies

  • You've posted this in a C# forum.  Are you just having trouble with the usage of psexec, or are you actually invoking psexec from inside a C# application?

    The EASIEST method is to create a batch file that does what you need, copy that batch file to the remote computer, and invoke the batch file with psexec.  psexec can be used to run an interactive session, but that's a much more complicated task.  You have to connect pipes to the stdin and stdout of the process, and then send individual commands as if you were a user typing interactively.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, November 16, 2018 8:35 PM
  • Thank you for your reply.

    I invoke psexec from c# application. I'm able to run a single command but I wanted to know if it was possible to execute multiple commands. I have to map a network drive on remote workstation (but I don't want to put a the password in a cmd/batch file on remote system) and then execute some other commnands and a script on that network drive.


    • Edited by nopnop78 Monday, November 19, 2018 12:36 PM
    Monday, November 19, 2018 12:08 PM
  • Hi nopnop78,

    If you want to perform multiple operations in a command prompt, you could consider ProcessStartInfo.RedirectStandardInput and use a StreamWriter to write to it, here is a sample for your reference.

    https://stackoverflow.com/questions/437419/execute-multiple-command-lines-with-the-same-process-using-net

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 20, 2018 5:52 AM
    Moderator
  • Multiple commands can be separated with “&&” or “&”. For example: ‘dir C: && dir D:’. Check if this can be used in your case.


    Tuesday, November 20, 2018 6:39 AM
  • I have read that article but multiple commands but the commands are executed locally on my wks.

    These are the steps that I perform manually and that I want to automate with my c# application:

    psexec \\servername -s cmd

    then on the server

    net use f: \\share1\path1 /user:domain/user1 password

    f:

    script.cmd

    i have tried to put the last 3 instruction in a batch file on remote wks but when i try to run it with psexec  it gets stuck on net use




    • Edited by nopnop78 Tuesday, November 20, 2018 4:33 PM
    Tuesday, November 20, 2018 4:32 PM
  • Hi nopnop78,

    Try the following code, which would execute the command one by one.

    Process p = new Process();
                ProcessStartInfo info = new ProcessStartInfo();
                info.FileName = "cmd.exe";
                info.RedirectStandardInput = true;
                info.UseShellExecute = false;
    
                p.StartInfo = info;
                p.Start();
    
                using (StreamWriter sw = p.StandardInput)
                {
                    if (sw.BaseStream.CanWrite)
                    {
                        sw.WriteLine("psexec \\servername -s cmd");
                        sw.WriteLine(@"net use f: \\share1\path1 /user:domain/user1 password");
                        sw.WriteLine("f:");
                    }
                }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 21, 2018 8:11 AM
    Moderator