Use Psexec to run the script in reomte PC same as run in locally RRS feed

  • Question

  • Hi

    I write a shutdown.exe script to restart computer and it have a prompt to user so they can stop the restart process. The script can run in locally.

    But when I use Psexec to run the script on remote PC. It only run on background, it won't have a prompt so user cannot stop it.

    How can I run use Psexec to run the script in remote PC same as locally. 



    • Edited by Chongtsz Tuesday, February 11, 2020 11:54 AM
    Tuesday, February 11, 2020 11:54 AM

All replies

  • You have to run the script interactively in the user session.

    In your case, practically you will need to read the parameter that you can pass to PsExec this way:

    Usage: psexec [\\computer[,computer2[,...] | @file]][-h][-s|-e][-i [session]]cmd [arguments]

         -i         Run the program so that it interacts with the desktop of the
                    specified session on the remote system. If no session is
                    specified the process runs in the console session.
         -h         If the target system is Vista or higher, has the process
                    run with the account's elevated token, if available.
         -s         Run the remote process in the System account.
         cmd            Name of application to execute.
         arguments  Arguments to pass (note that file paths must be
                    absolute paths on the target system).

    The problem will be find the user's session on the remote pc. starting with windows 7 and later what is the console session may no longer be the right one. so, running psexec -i -s may not be enough.. you will need to specify the correct session, which sometimes may be the number 1 sometimes the number 2 or 3..

    So, just to be sure, I would run something like this:

    psexec \\remotePC -s -h -i 1 cmd.exe -c shutdown.cmd

    psexec \\remotePC -s -h -i 2 cmd.exe -c shutdown.cmd

    psexec \\remotePC -s -h -i 3 cmd.exe -c shutdown.cmd

    Or if you want to avoid all of this you can run a first command to get the list of user connected and their session using qwinsta.exe or query session.

    Then you will know the exact session you have to interact with 

     SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
     services                                    0  Disc
     console                                     1  Conn
                       jose.Diaz              10  Disc
    >rdp-tcp#15        administrator            11  Active
                       rodolfo.smith        12  Disc
                       max.planck     13  Disc
     rdp-tcp                                 65536  Listen

    As you can see the console session is connected to session 1 but is not active. 11 is the only one where a user is active right now so it could interact with your script.

    So you command would become:

    psexec \\remotePC -s -h -i 11 cmd.exe -c shutdown.cmd


    Tuesday, February 11, 2020 1:17 PM