Pregunta

I have 3 servers on same network and having the same username and password to login, on which I want to perform following operations:

*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...

I want to create a Master.bat file on 1st Server, which should perform above operations on all the 3 servers in parallel.

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 ?

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

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.

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:

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
¿Fue útil?

Solución

In 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.

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top