Send a batch file to a Windows machine, and execute it

17,932

Solution 1

You can execute a script, or binary, remotely using WMI. This is a minimal example, written in VBS, which runs the batch file C:\test.cmd located on the remote computer FCSD-ISC1.

strComputer = "FCSD-ISC1"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objWMIService.Create "C:\test.cmd", null, null, intProcessID

This is covered in more detail in this Scripting Guy article.

Solution 2

I recommend mounting the C$ share, testing for the .\Temp directory (creating if it doesn't exist), and copying there. (There is an "admin$" administrative share, but I recommend against using it.)

You can do this from the command line via net use * \\servername\c$ /user:domain/account or specify a drive as net use t: \\servername\c$ /user:domain/account

This should be well documented in MSDN. If writing for a script, there are a lot of examples in the Microsoft Script Center Repository

Share:
17,932

Related videos on Youtube

Luca Matteis
Author by

Luca Matteis

http://scholar.google.com/citations?user=4shOPsgAAAAJ&hl=en

Updated on September 17, 2022

Comments

  • Luca Matteis
    Luca Matteis over 1 year

    I know PsExec is perfect for sending files over a computer on a network and then execute it on that remote computer, however I can't use PsExec because I'm developing a piece of software and I can't have PsExec as a dependency (for legal reasons etc.).

    What's the easiest way to achieve the execution part on a remote computer? Is it really that hard? I am already copying the .bat file with xcopy... I just need to run it.

    • zero_r
      zero_r over 13 years
      have a look at WMI or DCOM
  • jscott
    jscott over 13 years
    Even if you mount the remote share, execution of the batch file would occur on the local computer. @Luca says he's using xcopy place the file on the remote machine.
  • gWaldo
    gWaldo over 13 years
    Yup, I missed that.