17

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?

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
parth bhorania
  • 181
  • 1
  • 1
  • 8
  • 1
    possible duplicate of [Execute batch file on remote computer](http://stackoverflow.com/questions/25569893/execute-batch-file-on-remote-computer) – wOxxOm Sep 09 '15 at 15:21
  • 1
    There are [many questions](http://stackoverflow.com/search?q=%5Bbatch-file%5D+run+script+on+remote+server) about how to do this. – SomethingDark Sep 09 '15 at 15:21
  • Although it really depends on how you connect to INIDSOASRV01 from your local PC. Do you use Remote Desktop? FTP? SSH? Telnet? Do you mount it as a network drive? – SomethingDark Sep 09 '15 at 15:23
  • 1
    possible duplicate of [Run script file on remote server](http://stackoverflow.com/questions/31056573/run-script-file-on-remote-server) – SomethingDark Sep 09 '15 at 15:40
  • 1
    possible duplicate of [How to connect to remote server and start/stop the Tomcat that's running on that particular server using Ant?](http://stackoverflow.com/questions/10432416/how-to-connect-to-remote-server-and-start-stop-the-tomcat-thats-running-on-that) – F. Stephen Q Sep 09 '15 at 17:00

5 Answers5

26

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.

Dzmitry Paliakou
  • 1,587
  • 19
  • 27
12

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"

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    I am also involved in the similar type of task I am getting ERROR: Description = Access is denied. ON Executing below command from a PC 'A' command line to execute a batch file on a PC 'B' (100-SER) WMIC /user:100 /password:nspl /node:"100-SER" process call create "cmd.exe /c c:\erp\qrgen.bat" – Azad Hussain Oct 24 '18 at 10:35
  • @Azad - try to execute these as admin. – npocmaka Oct 24 '18 at 15:53
  • 1
    @npocmaka Same issue – Azad Hussain Oct 25 '18 at 07:18
  • 1
    The correct command will be : WMIC /NODE:"remote_machine" /user:user /password:password process call create "c:\some.bat","c:\exec_dir" – Neha Jul 20 '21 at 16:50
1

If you are in same WORKGROUP shutdown.exe /s /m \\<target-computer-name> should be enough shutdown /? for more, otherwise you need software to connect and control the target server.

UPDATE:

Seems shutdown.bat here is for shutting down apache-tomcat.

So, you might be interested to psexec or PuTTY: A Free Telnet/SSH Client

As native solution could be wmic

Example:

wmic /node:<target-computer-name> process call create "cmd.exe c:\\somefolder\\batch.bat"

In your example should be:

wmic /node:inidsoasrv01 process call create ^
    "cmd.exe D:\\apache-tomcat-6.0.20\\apache-tomcat-7.0.30\\bin\\shutdown.bat"

wmic /? and wmic /node /? for more

Community
  • 1
  • 1
Paul
  • 2,620
  • 2
  • 17
  • 27
0

With all the new security updates from Microsoft in the latest operating systems it is becoming more and more difficult to connect and execute scripts remotely. PsExec is one tool that helps you to connect a windows host from another windows host and execute command(s) or a script. Limitation of this tool is that it will execute the command(s) or a script, but it will not print the execution details. It will only return the process id.

C:\apps\tools\psexec \\%RemoteHostName% -u %Domain%\%userName% -p %userPassword% -accepteula -d -h -i 1 cmd.exe /c "cd C:\apps\test\ & echo Hello World" & call C:\apps\test\script.bat
-2

While I would recommend against this.

But you can use shutdown as client if the target machine has remote shutdown enabled and is in the same workgroup.

Example:

shutdown.exe /s /m \\<target-computer-name> /t 00

replacing <target-computer-name> with the URI for the target machine,

Otherwise, if you want to trigger this through Apache, you'll need to configure the batch script as a CGI script by putting AddHandler cgi-script .bat and Options +ExecCGI into either a local .htaccess file or in the main configuration for your Apache install.

Then you can just call the .bat file containing the shutdown.exe command from your browser.

Paul
  • 2,620
  • 2
  • 17
  • 27
F. Stephen Q
  • 4,208
  • 1
  • 19
  • 42