Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a batch file on a remote PC using a batch file on local PC

Tags:

batch-file

cmd

I want to execute a batch file

D:\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\shutdown.bat

Which is on my server inidsoasrv01.

How should I write my .bat file?

like image 348
parth bhorania Avatar asked Sep 09 '15 14:09

parth bhorania


People also ask

How do I run a batch file locally?

Type the following command to run a Windows 10 batch file and press Enter: C:\PATH\TO\FOLDER\BATCH-NAME. bat.

How do I use WMIC on a remote computer?

To create a share on a remote computer by using WMIC: At a command prompt, type wmic, and then press ENTER. Type /node:computer name where computer nameis the name of the target computer. If you want to pass administrator credentials, type /user:"domain\username", to receive a prompt for a password.

How do I run a batch file on a remote computer using Psexec?

Doing it manually works fine: Run cmd, launch "C:\pstools\psexec.exe \user-pc -u domain\admin -p password cmd /k", it goes into CMD on a remote computer. Then i type "start \share\script. bat" and it launches my batch file.

How do I run a batch file on a server?

To run a batch file, move to the directory containing the file and type the name of the batch file. For example, if the batch file is named "hope. bat," you'd type "hope" to execute the batch file.


2 Answers

Use microsoft's tool for remote commands executions: PsExec

If there isn't your bat-file on remote host, copy it first. For example:

copy D:\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\shutdown.bat \\RemoteServerNameOrIP\d$\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\

And then execute:

psexec \\RemoteServerNameOrIP d:\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\shutdown.bat

Note: filepath for psexec is path to file on remote server, not your local.

like image 106
Dzmitry Paliakou Avatar answered Oct 26 '22 19:10

Dzmitry Paliakou


You can use WMIC or SCHTASKS (which means no third party software is needed):

  1. SCHTASKS:

    SCHTASKS /s remote_machine /U username /P password /create /tn "On demand demo" /tr "C:\some.bat" /sc ONCE /sd 01/01/1910 /st 00:00 SCHTASKS /s remote_machine /U username /P password /run /TN "On demand demo"

  2. WMIC (wmic will return the pid of the started process)

    WMIC /NODE:"remote_machine" /user:user /password:password process call create "c:\some.bat","c:\exec_dir"

like image 36
npocmaka Avatar answered Oct 26 '22 17:10

npocmaka