Windows Batch File: Execute .exe on server with 'Run as different user'

I'm looking to create a batch file to execute restarting CruiseControl.Net's .exe on a server. There are two complications to this:

• Firstly I need to access the server remotely
• Secondly I need to run the .exe under "Run a different user"

I've seen some examples using PSEXEC to connect to the server, but this is running the bat file using the server connection credentials not running the exe as a different user.

For Example imagine the respective parts are as below:

• Server Name : "Test-Server-01"

• exe location: "Test-Server-01\d\$\CC.exe"

Example:

\\\Test-Server-01 -c test.bat -u User1 -p Password1


This runs "test.bat" on the server "Test-Server-01" with server's username and password. I then need to expand this to run the application under "Run as different user"

Question:How could I go about achieving this result?

I'm currently working backwards from this by starting with the "Run as different user" and executing this bat file on the server. This correctly starts the ccnet.exe but under the wrong folder.

CD "/D D:\Program Files (x86)\CruiseControl.NET\server"
RUNAS /user:user@company.com "D:\Program Files (x86)\CruiseControl.NET\server\ccnet.exe"
PAUSE


psexec \\Test-Server-01 -u User -p Pass d:\local\path\to\CC.exe

• You might need to use for psexec the argument of -h, or else use -s (might require administrator user/password). If problems, please add to the post your tests and exact results. – harrymc Aug 7 '17 at 9:40