2016-03-05 68 views
0

我有100多台服务器,我必须检查服务。在几乎5台服务器上,我们有相同的服务每个箱子包含最少3个服务。 我从保存在某个位置的文件导入内容。对于服务器名称,我很好。对于我的服务,我保存了像service_starting_name *列在下面的文件如下Powershell自动化服务

AA* 
BB* 
CC* 

以下是代码。按照以下代码,这是否是自动化的好主意?

$ServerName = Get-Content "Absolutepath" 
$Service = Get-Content "Absolutepath" 
foreach ($Server in $ServerName) { 

     write-host $($server) 
     Get-Service -ComputerName $Server $Service 


} 

此外,如何在不打印服务名称的情况下做出更好的显示效果? 假设在Server X中有5个服务,所以如果所有的服务都在运行,那么只需在该服务器上打印所有的好东西。

我试过使用如果条件,但由于有很多服务,它是打印多次,因为为每个循环。 请建议。

回答

0

你可以玩弄各种东西.. 为了测试我使用localhost,当您尝试:

Get-Service -ComputerName localhost -DisplayName *sophos* 

你得到: enter image description here

(Get-Service -ComputerName localhost -DisplayName *sophos*).count 

结果10

(Get-Service -ComputerName localhost -DisplayName *sophos*).Status -contains "stopped" 

结果真

所以如果你使用:

if (!((Get-Service -ComputerName localhost -DisplayName *sophos*).Status -contains stopped)) { Write-Host "Localhost: All ok" } 

或者:

if (!(Get-Service -ComputerName $server -DisplayName $Service | select status | where {$_.Status -like "Stopped"})) { Write-Host "$($Server): All OK" } 

您可以使用各种检查服务 “停止” 或什么的。以上 只是为了给你想法!

希望它有帮助。

+0

对于您提供的最后一个解决方案,无论服务是停止还是正在运行,所有远程计算机的所有服务器的输出(所有正常)都会变得相同。 – RIshu

+0

你能复制你使用的代码吗? – tfonias74

+0

1您需要将ServerName更改为Server :: foreach($ ServerName $ ServerName){if(!((Get-Service -ComputerName $ Server $ Service).Status -contains“Stopped”)){Write-Host“ Localhost:All ok“}}。此外这只搜索服务“名称”。如果名称不相关,则DisplayName可能会更好。对我来说,区别在于从“DisplayName”到从“name”查询返回的8个服务的12个服务。 – tfonias74