简体   繁体   English

Powershell服务自动化

[英]Powershell Automation for Services

I have 100+ servers for which I have to check services. 我有100多个服务器,必须检查其服务。 On almost 5 servers we have identical services. 在将近5台服务器上,我们具有相同的服务。 Each boxes contains min of 3 services. 每个盒子至少包含3个服务。 I am importing content from the file I saved in a location. 我正在从保存在某个位置的文件中导入内容。 For Server name I am good. 对于服务器名称,我很好。 For Service I have saved like service_starting_name* in column under the file like below 对于服务,我已在以下文件下方的列中保存了service_starting_name *之类的内容

AA*
BB*
CC*

Below is the code. 下面是代码。 Is this good idea for automation as per below code ? 按照下面的代码,这对于自动化是个好主意吗?

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

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


}

Also , How can we do a better display like, without printing the service name ? 另外,如何在不打印服务名称的情况下进行更好的显示? Suppose, In Server X , 5 services , so if all services are running just print all good on that server. 假设在Server X中有5个服务,因此,如果所有服务都在运行,则只需在该服务器上打印所有内容即可。

I tried using if conditions but as there are many services , it is printing multiple times because for for each loop . 我尝试使用if条件,但是由于有许多服务,因此每次打印多次,因为对于每个循环。 Please suggest. 请提出建议。

You can play around with various things.. For testing I use localhost, when you try: 您可以玩弄各种各样的东西。为了进行测试,尝试使用localhost:

Get-Service -ComputerName localhost -DisplayName *sophos*

You get: 你得到: 在此处输入图片说明

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

result 10 结果10

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

result True 结果为真

So if you use: 因此,如果您使用:

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

Or: 要么:

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

You can use various checks for services "stopping" or whatever.. The above are just to give you ideas! 您可以使用各种检查来“停止”服务或其他。.以上只是为了给您一些想法!

Hope it helps. 希望能帮助到你。

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

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