I have the following problem:
When I use:
Get-Service -Name CcmExec -ErrorAction silentlycontinue
I get the follow list:
Status Name DisplayName
------ ---- -----------
Running CcmExec SMS Agent Host
or
Status Name DisplayName
------ ---- -----------
Stopped CcmExec SMS Agent Host
When I use
$service = Get-Service -Name CcmExec -ErrorAction silentlycontinue -ComputerName $Computername
write-host "$service"
I only see ccmexec but not "running' or stoppend".
If all you want to see from Write-Host
is the status you could use this:
Write-Host $service.Status
Or you could do something like this:
Write-Host "$($service.Name) is $($service.Status)"
There are a couple of problems with the code presented in your comment. First, you're using multiple if
statements, rather than if
, elseif
, and/or else
. (You could aslso just use a switch statement.) Second, it's actually matching both Running
and $null
because you're using -match
instead of eq
. Try this instead:
if ($ccmservice.Status -eq "Running") {
$ExcelCell.cells.item($ExcelRow, 11) = "Running"
} elseif ($ccmservice.Status -eq "Stopped") {
$ExcelCell.cells.item($ExcelRow, 11) = "Not running"
} elseif ($ccmservice.Status -eq $null) {
$ExcelCell.cells.item($ExcelRow, 11) = "Not installed"
}
You can get more details on PowerShell's comparison operators here.