PowerShell scrip to run a cmd file on multiple servers

jason ray 1 Reputation point
2020-12-18T10:30:53.003+00:00

Hey,

I have a cmd(D:\Test\AppPool.bat) command file which i want to run on multiple server (like 15 servers) using PowerShell. Is there a way to do that?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,389 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2020-12-18T22:21:10.673+00:00

    Depending on what's in that file you may be able use it's contents (but not the file) by including it in a script block, something like this (if ipconfig was the contents of the bat file):

    Invoke-Command -ComputerName (get-content FileWithServerNamesInIt.txt) -ScriptBlock{ ipconfig }
    

    If that's not possible, put the bat file in a network share. Then create a .ps1 file that does something like running "cmd.exe /c <shared-bat-file>" and using:

    Invoke-Command -ComputerName (get-content FileWithServerNamesInIt.txt) -FilePath "PS1-File-That-Runs-CMD.exe"
    

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,376 Reputation points Microsoft Vendor
    2020-12-21T10:11:07.773+00:00

    Hi

    You can place the contents of the batch file in a ps1 file like this

    $batch=@"  
    #contents of the batch file  
    "@  
    $batch|cmd.exe  
    

    Then run the ps1 with Invoke-Command

    Invoke-Command -ComputerName $computer -FilePath $file  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments