简体   繁体   中英

powershell manage multiple windows service on multiple remote servers

I want to start/stop set of windows service on each of more than one remote servers. I've created one powershell in which i am passing values in a csv file for server name and service. but i want this should 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.

I have found solution to above.

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
    }
}

Thanks again to all who have responded here on my question.

You could put the service names as a comma-separated list in a tab-separated file:

Hostname    Services
HostA   ServiceA,ServiceB
HostB   ServiceA,ServiceC,ServiceD
HostC   ServiceB,ServiceC

and process the file like this:

Import-Csv C:\path\to\list.tsv -Delimiter "`t" | % {
  $services = $_.Services -split ','
  Start-Service -Computer $_.Hostname -Name $services
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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