简体   繁体   English

powershell管理多个远程服务器上的多个Windows服务

[英]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. 我想在多个远程服务器的每一个上启动/停止一组Windows服务。 I've created one powershell in which i am passing values in a csv file for server name and service. 我创建了一个powershell,其中我在服务器名称和服务的csv文件中传递值。 but i want this should be in below manner:- 但我希望这应该是以下方式: -

One text file shd contain list of remote server 一个文本文件shd包含远程服务器列表

One text file shd contain list of services. 一个文本文件shd包含服务列表。

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. 一个powershell脚本,它将启动或停止第一个文本文件中列出的第一个服务器上的第二个文本文件中提到的所有服务,一旦完成第一个服务器,它就会转到带有所有这些服务的第二个服务器,依此类推,直到列表中的最后一个条目为止。远程服务器。

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 我创建了两个单独的文本文件,一个用于服务器名称,例如servers.txt ,另一个用于服务名称,例如services.txt

In my PowerShell script I've written below and worked fine for me: 在我的PowerShell脚本中,我写了下面的内容,并为我工作得很好:

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

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

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