parth bhorania
parth bhorania

Reputation: 181

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

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?

Upvotes: 17

Views: 185953

Answers (5)

Akash Soori
Akash Soori

Reputation: 31

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

Upvotes: 0

npocmaka
npocmaka

Reputation: 57252

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"

Upvotes: 12

Dzmitry Paliakou
Dzmitry Paliakou

Reputation: 1627

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.

Upvotes: 27

Paul
Paul

Reputation: 2710

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

Upvotes: 1

F. Stephen Q
F. Stephen Q

Reputation: 4306

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.

Upvotes: -2

Related Questions