How to run a command against multiple servers simultaneously in Powershell

前端 未结 3 552
夕颜
夕颜 2021-01-21 23:06

I am looking for a way to restart three services on multiple servers simultaneously. I know how to restart services against a list of servers by using a loop but as I have many

3条回答
  •  走了就别回头了
    2021-01-21 23:33

    You could try to work with jobs. Jobs are run in the background and you have to retrieve them with Get-Job to see their status. Please read the information to Powershell jobs on these two sites:

    http://msdn.microsoft.com/en-us/library/dd878288%28v=vs.85%29.aspx

    http://technet.microsoft.com/de-DE/library/hh847783.aspx

    Your code would look something like this:

    $servernames | ForEach-Object {Start-Job -Name "Job-$_" -Scriptblock {"Enter your code here -Computername $_"}}
    

    This will create a background job for each servername. As already mentioned you can see the status using the cmdlet Get-Job. To get the result use the cmdlet Receive-Job.

提交回复
热议问题