none
Execute a batch file on a remote PC

    Question

  • Hello,

    I have a powershell script that I would like to exceute several batch commands on a remote computer.  I first need the script to change to the network drive on that remote computer then execute a batch command that I write into the script is that possible?

    G

     

    Tuesday, February 22, 2011 6:54 PM

Answers

All replies

  • You can execute powershell script on remote machine using invoke-command:

     

     

    invoke-command -computername server1 -filepath C:\proc.ps1
    

    http://technet.microsoft.com/en-us/library/dd819505.aspx

     
    Tuesday, February 22, 2011 7:16 PM
  • I thought of that but I need to invoke a .bat file
    Tuesday, February 22, 2011 7:19 PM
  • Maybe its not bat file but try in PowerShell script file (.ps1) execute yous commands with Invoke-Expression

    Example ipconf.ps1:
    Invoke-Expression "ipconfig"

    Next remote execute ps1 script with invoke-command.
    invoke-command -computername server1 -filepath C:\ipconf.ps1

    Tuesday, February 22, 2011 7:37 PM
  • I forgot.
    Or better way, use PsExec tool from Sysinternals:
    http://technet.microsoft.com/pl-PL/sysinternals/bb897553.aspx
    Tuesday, February 22, 2011 7:45 PM
  • Thanks but I need to invoke a .bat file on a remote machines any ideas on that?

     

    Tuesday, February 22, 2011 8:26 PM
  • You try Psexec tools ??
    PsExec.exe \\server1 c:\script.bat

    Tuesday, February 22, 2011 8:38 PM
  • Only execute script on remote server from PowerShell

    $server = "serve1"
    $process = [WMICLASS]"\\$server\ROOT\CIMV2:win32_process"  
    $result = $process.Create("C:\script.bat") 
    
    
    • Proposed as answer by dupren Thursday, March 22, 2012 11:23 AM
    Tuesday, February 22, 2011 9:02 PM
  • I dont need to create the .bat file I already have it

    I need something like

    excecute command \\server1\folder\blah\stopserver.bat <servername> -user <user> -password <passwprd>

    I want to write this into a powershell  script.

    Tuesday, February 22, 2011 9:34 PM
  • No you don't understand. You don't create bat file. You create a process to execute your existing bat file on server.

    Create object to make process:
    $process = [WMICLASS]"\\$server\ROOT\CIMV2:win32_process

    Set to object what you need execute. In your case execute bat file on server. Not create a file.
    $result = $process.Create("C:\script.bat")

     

    • Marked as answer by geniiw Friday, February 25, 2011 6:18 PM
    Tuesday, February 22, 2011 9:50 PM
  • I did try it and no results were giving and the jvm services on the remote machine was not stopped.  Here is what im trying to do based on your suggestions.

    $process

     

     

    = [WMICLASS]"\\severgoeshere\ROOT\CIMV2:win32_process"

    $result

     

     

    = $process.Create("e:\stopserver.bat JVMdfs -user <myusername> -password <mypassword> ")

    Wednesday, February 23, 2011 4:36 PM
  • I test it many times and always it works. Script with parameters or without.
    I  think it’s not problem with this script to execute this but maybe you don’t have right to stop this or something.

    Sorry but with no details about your script then will be hard to find problem.

    Wednesday, February 23, 2011 8:08 PM
  • Even if you're not wanting to run it as a process, MichalGajda already gave you the ideal solution to run a batch file remotely. PSEXEC, which is a free tool developed by Sysinternals and is now from Microsoft. I have used it to do exactly what you are looking to do several times, including passing the credentials as you described. Just put the batch file on a network share. It's easy to do this for a 'one off' but if you have a list of computers that you want to run this on you can do it by creating a .vbs file and having it read a file for the list (one per line) call the batch file, and then give feedback in another file.

    Try something like this: (Note: please give MichalGajda credit for answering the question though)

    Option Explicit

    Const ForReading = 1
    Const ForWriting = 2
    Const OverwriteExisting = TRUE

    Dim objFSO, strComputer, objFile, filesys, testfile, strPSExec, strCmd, objShell, strUser, strPass

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\scripts\Computers.txt") 'Change the name and location of the output file as needed
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set testfile = filesys.CreateTextFile("c:\scripts\ScriptFeedback.txt", True) 'Change the name and location of the output file as needed

    strUser = InputBox("Please Enter Your Username: " & (Chr(13) & Chr(10)) & "(Format: domain\username) ")
    strPass = InputBox("Please Enter Your Password: ")

    Do Until objFile.AtEndOfStream
     On Error Resume Next
        strComputer = objFile.ReadLine
     Set objShell = CreateObject("Wscript.Shell")
           
      strPSExec = "c:\windows\system32\psexec.exe"

      strCmd = strPSExec & " -d " & " -u " & strUser & " -p " & strPass & " \\" & strComputer & " cmd /c " & "\\fileserver\scripts\script.bat" 'Change to reflect location of batch file in network
      objShell.Run strCmd, 0, False

      If Err.Number <> 0 Then
           testfile.WriteLine strComputer
           Err.Clear
       End If
      Set objFSO = Nothing
    Loop
    testfile.Close 

    Thursday, February 24, 2011 4:34 PM
  • Thanks Michal it worked for me :)
    Wednesday, December 07, 2011 7:12 AM
  • You try Psexec tools ??
    PsExec.exe \\server1 c:\script.bat

    works like a charm. another example:

    C:\temp\PsTools\PsExec.exe \\192.168.0.99 -u "devdom\admin" -p mypass "c:\scripts\Start.bat"

    Wednesday, February 08, 2012 6:13 PM
  • To run a batch file with Invoke-Command use a script block.

    copy c:\temp\bat.bat \\server1\c$\temp\bat.bat

    invoke-command -script {c:\temp\bat.bat} -computer server1


    ¯\_(ツ)_/¯

    Wednesday, February 08, 2012 6:42 PM
  • Hi Michal,

       I have used PsTool, I tried, but batch file(.bat) not working

    1)C:\WINDOWS\system32>psexec \\ipaddress -u "computername\userid" -p "password" "D:\Test\test.bat"

    2)C:\WINDOWS\system32>psexec \\computerName D:\Test\test.bat

    when I use any exe in same path it works. but batch file(.bat) not working.

    When I am working with batch file named test.bat, it shows error....

    '..\SetRelease.bat' is not recognized as an internal or external command,
    operable program or batch file.
    The system cannot find the path specified.
    D:\Test\test.bat exited on 333.33.33.3 with error code 1.

    [333.33.3.3 is ip address, I have not given correct for security reason]

    Could u help me?

    Thanks in advanced.. 

    Ashish

    Friday, February 10, 2012 10:39 AM
  • try running it with CMD

    psexec \\computerName "c.:\windows\system32\cmd.exe /c D:\Test\test.bat"


    ¯\_(ツ)_/¯

    Friday, February 10, 2012 8:30 PM
  • This worked flawlessly, I stripped out the stuff for the list of computers and pulled in a user password.  This works really well for a trigger in sql to execute a remote batch file.  Thank you.
    Tuesday, April 10, 2012 3:19 PM
  • I have tried this and can't get it to work, but I am very much a novice.  I have a .bat file on a Remote Desktop that I would like to execute from VBA code. Please see my code and send me as specific information as you can.  When I run this code, I get "Object Required" error on the "result" line.  Please give me full code.  It seems to have a problem with the path to the .bat file.  Thanks in advance.  Also, I would like to know when the .bat file finishes.

    This line,  Process = [WMICLASS]"\\server\ROOT\CIMV2:win32_process",  gives a syntax error, so I replaced as below.

    Dim Server, Process, Result

    Server = "ServerName"

    Process = "\\server\ROOT\CIMV2:win32_process"

    Result = Process.Create("C:\Path\path\desktop\filename.bat")


    Monday, May 06, 2013 8:09 PM
  • Considering this thread has been marked as answered for over two years, you'll be better off starting a new thread.

    Good luck.


    Monday, May 06, 2013 8:16 PM
  • Just thought I would say .. thanks.. !!  was exactly what I needed - couldnt use invoke-command and was scrambling to find something else.

    - Glenda

    Friday, May 23, 2014 10:46 PM