Powershell:获取服务检查正在运行或未运行

时间:2014-06-20 05:28:42

标签: powershell service sccm

我有以下问题:

当我使用时:

Get-Service -Name CcmExec -ErrorAction silentlycontinue

我得到以下列表:

Status   Name               DisplayName
------   ----               -----------
Running  CcmExec            SMS Agent Host

Status   Name               DisplayName
------   ----               -----------
Stopped  CcmExec            SMS Agent Host

当我使用

$service = Get-Service -Name CcmExec -ErrorAction silentlycontinue -ComputerName $Computername
write-host "$service" 

我只看到ccmexec但不是“正在运行”或“停止”。

1 个答案:

答案 0 :(得分:2)

如果你想要从Write-Host看到的是你可以使用的状态:

Write-Host $service.Status

或者你可以这样做:

Write-Host "$($service.Name) is $($service.Status)"

评论中提供的代码存在一些问题。首先,您使用多个if语句,而不是ifelseif和/或else。 (您也可以使用switch statement。)其次,它实际上与Running$null匹配,因为您正在使用-match代替{ {1}}。试试这个:

eq

您可以获得有关PowerShell的比较运算符here的更多详细信息。