Powershell脚本 - 服务运行eq OK或列出服务未运行

时间:2013-05-15 10:43:19

标签: powershell scripting cmdlets

我有这个脚本,它会检查多个服务器上运行的特定服务。

我需要帮助或指示如何让它不回退运行/不运行响应。我需要让它成为“如果所有查询的服务都在运行= OK,如果没有列表服务没有运行”

这可行吗?

脚本:

$Check1 = Get-service -Computername server1 -Name *service1* | ConvertTo-Html Name,Status -Fragment
$Check2 = Get-Service  -Computername server2 -name *service2* | ConvertTo-Html Name,Status  
$Check3 = Get-Service  -Computername server3 -name *service3* | ConvertTo-Html Name,Status
$Check4 = Get-Service -ComputerName UKVAULT01 -Name *service4* | ConvertTo-Html Name,Status
ConvertTo-HTML -Body "<b>Check1</b> $Check1 <br> <b>Check2</b> $Check2 <br> <b>Check3</b> $Check3<br> <b>Check4</b> $Check4" -Title "Service Checks" | Out-File c:\StatusReport.html

1 个答案:

答案 0 :(得分:1)

将检查封装在如下函数中:

function Check-Service{$server, $name) {
  $check = Get-Service $server -Name $name | ? { $_.Status -ne "Running" }
  if ( $check -eq $null ) { $check = "OK" }
  $check | ConvertTo-Html -Fragment
}

$Check1 = Check-Service server1 *service1*
$Check2 = Check-Service server2 *service2*
...