简体   繁体   English

发出运行powershell命令以检查远程服务器服务状态的问题

[英]Issues running powershell command to check status of remote servers services

Title is pretty self explanatory, I'm having issues when running the below script against a list of servers and services. 标题是很容易解释的,在针对服务器和服务列表运行以下脚本时遇到问题。 I keep ending up with the following error when I run it even though when I run a get-service on ex01 by itself, I can see the service and it's status. 即使我自己在ex01上运行get-service,我仍然看到以下错误,但我仍然可以看到该服务及其状态。 Not sure where the disconnect is, but any help is appreciated. 不知道断开的位置,但是可以提供任何帮助。

Get-Service : Cannot find any service with service name 'MsExchangeIS'.

At \\dc01\\c$\\DriveMan\\ServiceStatusCHK\\Srvstatuscheck.ps1:12 char:31 + $service_status = (Get-Service -Name $service).Status + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (MsExchangeIS:String) [Get-Service], ServiceCommandException + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand 在\\ dc01 \\ c $ \\ DriveMan \\ ServiceStatusCHK \\ Srvstatuscheck.ps1:12 char:31 + $ service_status =(Get-Service -Name $ service).Status + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~ + CategoryInfo:ObjectNotFound:(MsExchangeIS:String)[Get-Service],ServiceCommandException + FullyQualifiedErrorId:NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand

$ErrorActionPreference= 'continue'
$services = ("dns","dhcp","MsExchangeIS")
$servers  = 'dc01','dc02','ex01','ex02'


foreach($server in $servers){

$service_status = (Get-Service -Name $service).Status

    foreach($service in $services){

       #start-sleep -s 1



       if((((Get-Service -Name $service).status) -eq 'running')){

           "$service on $server is Running" | write-host -foregroundcolor green 

                } else  

                {"$service on $server is DOWN" | write-host -foregroundcolor red 

                } }}         

            pause   

You're checking the local computer for the services, not the remote computers. 您正在检查本地计算机上的服务,而不是远程计算机。

Get-Service -Name $service

Should be 应该

Get-WmiObject win32_service -computername $server -filter "name='$Service'"

However, you can do this without the loops because Get-WMIObject can take a collection of remote computers and a filter to specify multiple services. 但是,您可以在没有循环的情况下执行此操作,因为Get-WMIObject可以获取一组远程计算机和一个用于指定多个服务的筛选器。

$ErrorActionPreference= 'continue'
$services = ("dns","dhcp","MsExchangeIS")
$servers  = 'dc01','dc02','ex01','ex02'
$filter = "";
foreach ($svc in $services) {
    $filter += "name = '$svc' or ";
}
$filter = $filter.Substring(0,$filter.Length-4);
Get-WmiObject -Class win32_service -filter $filter -ComputerName $servers|select-object -property pscomputername,name,state | format-table -autofit;

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

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