The batch file doesn't display properly when it run remotely

jane_2ee 21 Reputation points
2021-01-04T02:48:19.987+00:00

When running the batch file remotely using WinRM, it runs but doesn't display properly.

Below is an execution environment.

Run PowerShell script on the remote computer.
It performs the following command.
psexec -i "$session_id" -d "C:\test.bat"

The problem is;

It runs and displays properly when I execute the Powershell script directly on the Windows server.

53112-img-2.png

It runs but doesn't display properly as below when it runs on the remote server.

I checked the event log and there is nothing suspicious. It uses the same CPU and Memory usage.
53066-img-1.png

I just think it's not permission problems, but I'm not sure what is the problem. Can anyone suggest to me how to solve this?

Windows Server Management
Windows Server Management
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Management: The act or process of organizing, handling, directing or controlling something.
424 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,566 Reputation points
    2021-01-04T19:02:06.823+00:00

    When running the batch file remotely using WinRM,
    psexec -i "$session_id" -d "C:\test.bat"

    Why are you using Sysinternal's psexe?

    -d         Don't wait for process to terminate (non-interactive).
     -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.
    

    The -d switch says to launch the process and then terminate. So yes, you won't get any output. I would expect you would also need to pass the -accepteula switch, otherwise it might hang.

    Your image shows "C:\Test.exe". Is that the root program that you are trying to execute? What are you trying to do with the session variable?

    Why don't you take the commands that are in C:\Test.bat and just put them into your WinRM script? Replace the line that has whoami.exe with the path to your test.exe.

    Like this.

     $script = 
     {
    
        "This script is running on {0}" -f $env:COMPUTERNAME
        "Calling whoami to execute on the remote system"
        c:\Windows\system32\whoami.exe
    
     }
     Invoke-Command -ComputerName "test10b"  -ScriptBlock $script 
    

    Is test.exe a GUI or command line program? If you want to run it over a remote WinRM session, it has to be a command line program so that stdout and stderr can captured and sent back to the source machine.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jenny Yan-MSFT 9,326 Reputation points
    2021-01-04T06:49:09.743+00:00

    Hi,
    I am wondering where the bat file is located: the remote server or the local machine?

    If the former one, please try to add the remote server IP or computer name as a test.
    If the latter one, kindly check the commands shared in below link:
    How to run a local bat file on a remote computer
    https://superuser.com/questions/822239/how-to-run-a-local-bat-file-on-a-remote-computer
    Please note: Information posted in the given link is hosted by a third party. Microsoft does not guarantee the accuracy and effectiveness of information.


    Hope this helps and please help to accept as Answer if the response is useful.

    Thanks,
    Jenny