IF Else statement running the opposite of the way it should.

I’m having a problem properly running an if else statement. $service2 is the WinRM service and the Get-Service cmdlet properly evaluates the whether the WinRM service is running or not… If the WinRM service is stopped and the first part of the statement evaluates as true, the program runs the code under the else statement. If I change the operator from -eq to -ne, which would evaluate to false, the custom antimalware scan does not run run and PowerShell displays: Cannot run custom antimalware scan – WinRM service is stopped. When I flip it, where the WinRM service is running and the custom antimalware scan is supposed to run if the service is running and if it is not, it should display: Cannot run custom antimalware scan – WinRM service is stopped; I get the the same error. Any thoughts about what I am doing wrong?

Get-Service -computername $computer -name $service2

if ($service2.Status -eq "Stopped") 

    {Write-Host "Cannot run custom antimalware scan -- WinRM service is stopped."}

else 

    {Invoke-Command -computername $computer {& "C:\Program Files\Microsoft Security Client\Antimalware\MpCmdRun.exe" -scan -scantype 3 -File C:\ProgramData\Microsoft\Search\Data\Applications\Windows\}}

$service2 appears to be a string variable in your code. You haven’t assigned the result of the call to Get-Service to any variable (and it’s this result that has the Status property you’d be checking). For example:

$serviceObject = Get-Service -ComputerName $computer -Name $service2

if ($serviceObject.Status -eq 'Stopped') # ... etc

Thank You!! That fixed it.