Start set of windows service on more than one remote servers

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Akash Kumar Akash Kumar 3 years, 3 months ago.

  • Author
    Posts
  • #13691
    Profile photo of Akash Kumar
    Akash Kumar
    Participant

    Hi,
    I want to start/stop set of windows service on each of more than one remote servers. I've created one in which i am passing values in a csv file for server name and service. but i want this shd be in below manner:-
    One text file shd contain list of remote server
    One text file shd contain list of services.
    One powershell script which will either start or stop all the services mentioned in second text file on first server listed in first text file, once done with first server it shd go to second server with all these services and so on till last entry in list of remote server.

  • #13737
    Profile photo of Akash Kumar
    Akash Kumar
    Participant

    Thanks Tore and Stephen for your inputs.
    I've found solution for same as below.
    I've created two separate text files, one for server name say "servers.txt" and one is for service name say "services.txt"

    In my powershell script i've written below and worked fine for me

    $serverList = gc servers.txt

    $serviceList = gc services.txt

    ForEach ($server in $serverList) {

    ForEach ($service in $serviceList)
    {

    Get-Service -Name $service -ComputerName $server | Start-service

    }
    }

  • #13694
    Profile photo of Stephen Owen
    Stephen Owen
    Participant

    This will get you started. We will build two arrays full of Server Names and Service Names. For Each $server in the array $servers, we then ForEach $service in $services, try to start the service. If it fails to start, display a message.


    $servers = import-csv .servers.csv
    $services = import-csv .services.csv
    ForEach ($server in $servers){
    ForEach ($service in $services){
    Write-host "Starting $service on $server..." -nonewline
    try{
    Start-Service -ComputerName $server -DisplayName $service
    Write-host "Done"
    }
    catch{
    Write-error "Uh, something went wrong. Try running as Sudo :) "
    }
    }
    }

  • #13696
    Profile photo of Tore Groneng
    Tore Groneng
    Participant

    hi,

    Slight snag here. The Start-Service cmdlet does not have a parameter called computername. It does not exist. You could try something like this:

    $servers = import-csv .servers.csv
    $services = import-csv .services.csv
    Function Start-MyCustomStartService
    {
    [cmdletbinding()]
    Param(
    [String] $Computername
    ,
    [String] $Service
    ,
    [Switch] $UseDisplayName
    )
    $ErrorActionPreference = "Stop"
    Try
    {
    if($UseDisplayName)
    {
    Write-Verbose "Starting service with displayname '$service' on computer $computername"
    Get-Service -ComputerName $Computername -DisplayName $service | Start-Service
    }
    else
    {
    Write-Verbose "Starting service with name '$service' on computer $computername"
    Get-Service -ComputerName $Computername -name $service | Start-Service
    }
    }
    catch
    {
    $_
    }
    Finally
    {
    $ErrorActionPreference = "Continue"
    }
    }
    ForEach ($server in $servers)
    {
    ForEach ($service in $services)
    {
    Start-MyCustomStartService -Computername $server -Service $service
    }
    }

    Currently I do not have access to a powershell 2.0/3.0 machine, however I remember that there were some issues with this on those versions. I have tested this only on powershell v4. If you plan to use this on older versions of powershell, let me know and I will dig it up

    Cheers

    Tore

You must be logged in to reply to this topic.