简体   繁体   English

并行在多个远程服务器上执行Windows服务

[英]Windows Service operations on multiple remote servers in parallel

I have 3 servers on same network and having the same username and password to login, on which I want to perform following operations: 我在同一网络上有3台服务器,并使用相同的用户名和密码登录,并且要在其上执行以下操作:

*Check if Service1 is running or not
*If it is running, stop the service 
*Set Service1 to disable mode 
*If it is not running, do nothing
*Check if Service2 is running 
*If it is running, stop the service 
*Set Service2 to disable mode 
*If it is not running, do nothing

and so on for Service3,..4,..5... 对Service3,.. 4,.. 5等等

I want to create a Master.bat file on 1st Server, which should perform above operations on all the 3 servers in parallel. 我想在第一台服务器上创建一个Master.bat文件,该文件应在所有三台服务器上并行执行上述操作。

I am trying with below but not getting how i can do this for more than 1 services in same code and also its getting bit lengthy.Is there any other short method to do this in batch or powershell ? 我在下面尝试,但没有得到如何在相同的代码中为多个服务执行此操作,并且它也变得冗长。是否有其他短方法可以批量或通过powershell进行此操作?

original_serverlist.txt: original_serverlist.txt:

Server1
Server2
Server3

Code: 码:

copy original_serverlist.txt serverlist.txt

:repeat
set currentserver=
for /f "eol=; tokens=1*" %%i in (serverlist.txt) do set currentserver=%%i
if "%currentserver%"=="" goto end

for /F "tokens=3 delims=: " %%H in ('SC query Service1 ^| findstr "        STATE"') do (
  if /I "%%H" "RUNNING" (
  net stop Service1
  echo Service1 stopped @ %date% %time% > C:\result.txt 
  sc config Service1 start= disabled
  echo Service1 set disabled @ %date% %time% >> C:\result.txt 
  )
)

type serverlist.txt| find /v "%currentserver%"> serverlist1.txt
copy serverlist1.txt serverlist.txt
goto repeat

:end
del serverlist.txt
del serverlist1.txt

EDIT1 编辑1

I am not that much comfortable in PS as in batch but i want to learn the things so putting my exact requirement here to learn how to implement it in powershell with code explanation. 我在PS方面不如在批处理中那么自在,但我想学习这些东西,因此将我的确切要求放在这里,以学习如何在Powershell中使用代码说明来实现它。

I need to perform following same set of operations on all the three remote Servers in parallel (Server1,Server2,Server3) having Master.ps1 on Server1: 我需要在Server1上具有Master.ps1的所有三个并行远程服务器(Server1,Server2,Server3)上执行以下同一组操作:

run command  cmd /c iisreset/stop and set IIS service to Disabled mode
Stop service1 and set it to Disabled mode
Stop service2 and set it to Disabled mode
Stop service3 and set it to Disabled mode
Kill process jusched.exe
Kill process conhost.exe

In powershell 3.0 在Powershell 3.0中

$services = "service1", "service2", "service3"
"server1", "server2", "server3" | % {
        Get-Service -Computer $_ $services | ? State -eq "Running" | Stop-Service -Force -PassThru | Set-Service -StartupType Disabled
}

You need to enable powreshell remoting for this to work and add -Credential option anywhere you see -Computer argument if your current domain user doesn't have enough rights. 如果当前域用户没有足够的权限,则需要启用powreshell远程处理才能正常工作,并在看到-Computer参数的任何位置添加-Credential选项。

More generic option would be to use hash 更通用的选择是使用哈希

$ServiceMap = @{ "server1": ("service11", "service12");
                 "server2": ("service21" ... ) }

You will also need to check for errors.. 您还需要检查错误。

To achieve parallel execution you could do something like: 要实现并行执行,您可以执行以下操作:

workflow set_services {
  param(
    [string[]]$Computers, [string[]]$Services
  )

  foreach -parallel ($computer in $computers) {
    Get-Service -ComputerName $computer $services | ? State -eq "Running" | Stop-Service -Force -PassThru | Set-Service -StartupType Disabled 
  }    
}

BTW, do not use batch scripts any more, anywhere. 顺便说一句,不要在任何地方使用批处理脚本了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM