如何在 Powershell 中同时针对多个服务器运行命令 [英] How to run a command against multiple servers simultaneously in Powershell

查看:63
本文介绍了如何在 Powershell 中同时针对多个服务器运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在多台服务器上同时重启三个服务的方法.我知道如何使用循环针对服务器列表重新启动服务,但由于我有很多服务器,因此等待每个服务器上的每个服务按顺序重新启动需要很长时间.有没有办法一次向所有服务器发送重启服务命令,而不是等待每个服务器?

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 servers it would take a long time to wait for each service on each server to restart in a sequential order. Is there a way to send restart service command to all servers at once instead of waiting for each server?

推荐答案

您可以尝试处理作业.作业在后台运行,您必须使用 Get-Job 检索它们以查看它们的状态.请阅读以下两个站点上有关 Powershell 作业的信息:

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

您的代码如下所示:

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

这将为每个服务器名称创建一个后台作业.如前所述,您可以使用 cmdlet Get-Job 查看状态.要获得结果,请使用 cmdlet Receive-Job.

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.

这篇关于如何在 Powershell 中同时针对多个服务器运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆