none
powershel script to ran batch file for mutiple machines RRS feed

  • Question

  • Hi I am looking powershel script to ran batch file for mutiple machines sceanrio is i have a batchfile and needs to ran on mutiple machines kindly help me how to run bat file in mutiple machines
    Friday, February 1, 2019 12:25 AM

All replies

  • Hi,

    Thanks for your question.

    You can use "Invoke-Command" cmdlet to run commands remotely.

    Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {type your commands}

    https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-6

    Best Regards,

    Lee


    Just do it.

    Friday, February 1, 2019 3:26 AM
  • thanks kindly share sample complete script ( I have a batch file stored on a machines) C:\temp then how to run on mutiple machines that batch file
    Friday, February 1, 2019 5:01 AM
  • Hi,

    https://stackoverflow.com/questions/32125893/running-batch-script-on-remote-server-via-powershell

    $cred=Get-Credential

    Invoke-Command -ComputerName "Server1" -credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\Users\nithi.sund ar\Desktop\Test.bat'"}

    Best Regards,

    Lee



    Just do it.

    Friday, February 1, 2019 5:55 AM
  • Thanks a lot  $cred=Get-Credential means will take current or running session credentials?

    kindly let me know how to use psexec in powershell requirment is needs to run batch (bat) on mutiple machines

    Friday, February 1, 2019 7:05 AM
  • No, when you run "Get-Credential", you will get a credential object based on a user name and password.

    You can type the username and password, and the variable $cred will save the credential. You can use the credential to get a remote session with your machines.

    Invoke-command can help solve your PowerShell remote session.

    Best Regards,

    Lee


    Just do it.

    Friday, February 1, 2019 7:21 AM
  •  

    Save the remote machines' names or IPs in the text file.

    psexec @computers.txt -d -n 5 cmd /c \\server\folder\file.BAT
    You enlist the names or IPs of the PCs to contact in the computers.txt file. The -d will detach psexec, allowing to run the batch in-parallel on several machines - without psexec will wait until the batch file terminates, before switching to the next PC.

    Download it here:

    https://docs.microsoft.com/en-us/sysinternals/downloads/psexec


    Just do it.

    Friday, February 1, 2019 7:24 AM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Lee

    Just do it.

    Thursday, February 21, 2019 6:28 AM