locked
How to Start the multiple services on multiple servers RRS feed

  • Question

  • Hii There 

    I wrote this  script but its not working can anyone help me I am struggling alot.

    The output is coming not correct, it is taking only  first service, I think every time its taking first service because of break, can you help me out with the logic 

    $ComputerName = (Read-Host "Enter the Computer Name (Saprate with Comma)").split(',') | ForEach-Object {$_.trim()}
    $ServiceName = (Read-Host "Enter the Service Name (Saprate with Comma)").split(',') | ForEach-Object {$_.trim()}
    
    
    Try{
    foreach ($ComputerName1 in $ComputerName)
    {
    
         foreach($ServiceName1 in  $ServiceName )
    {
    
    $Command = Start-Service -InputObject $(Get-Service -ComputerName $ComputerName1 -Name $ServiceName1 ) -ErrorAction Stop
    
    $Command 
    
    
    
    $View = Get-Service $ServiceName1 -ComputerName  $ComputerName1 
    
    $View 
    
    break
    
    }
    }
    Write-Host "Service started successfully..!!!" -ForegroundColor Cyan
    
    }Catch{
         write-Host "Sorry!! I am unable to start this service."
    }
    $([char]7)
    sleep 1
    
    $([char]7)
    sleep 1
    
    $([char]7)
    sleep 1
    
    $([char]7)
    sleep 1



    Thank you 

    Sunday, April 22, 2018 1:55 AM

All replies

  • To begin with please post your script correctly.  Use correct formatting an dindentationa nd use the code posting tool provided.  What you have posted is mostly unreadable.

    Saying something "doesn't work" is not a question.  What is the issue and post the complete error message.


    \_(ツ)_/

    Sunday, April 22, 2018 2:03 AM
  • The most obvious issue with your code is that it will terminate on any error. 

    I recommend that you first learn basic PowerShell.  After that study how to use exception handling. 


    \_(ツ)_/

    Sunday, April 22, 2018 2:13 AM
  • This is pretty much easy

    $computers="computer1","computer2"
    $services="service1","service2"
    foreach($computer in $computers) { 
    Foreach($service in $services) {
    Get-Service $Service -ComputerName $computer  | where {$_.Status -ne 'running'}  | foreach { $_.start()  } -Verbose
    
    Start-Sleep -Seconds 60
    }}
    

    • Proposed as answer by Martien van Dijk Tuesday, April 24, 2018 9:01 AM
    • Unproposed as answer by jrv Tuesday, April 24, 2018 9:04 AM
    Tuesday, April 24, 2018 8:37 AM
  • Take out the break.

    Tuesday, April 24, 2018 6:48 PM
  • Wouldn't this work?


    get-service service1,service2 -computername computer1,computer2 | start-service


    • Edited by JS2010 Wednesday, April 25, 2018 3:00 PM
    Wednesday, April 25, 2018 3:00 PM
  • Wouldn't this work?


    get-service service1,service2 -computername computer1,computer2 | start-service


    Yes but errors can also abort this method.


    \_(ツ)_/

    Wednesday, April 25, 2018 3:04 PM