none
"Get-Service" does not work inside "IF Statement" RRS feed

  • Question

  • Hello Experts,

    It may be a silly question but I am not able to figure out what is going wrong here.

    In case I run only "Get-Service -ComputerName $GW1 -Name TestServiceName | Stop-Service -Force", it runs fine.

    However, When I add it inside an IF Statement, it throws an error

    Script:

    $restult1 = (D:\utils.bat -listServices | findstr "STARTED")
    $count1 = $restult1.count
    Write-Host $count1

    if ($count1 -eq 7) {

    Write-Host "IF Statement Started"

         $GW1 = "Server1"
    Get-Service -ComputerName $GW1 -Name TestServiceName | stop-Service -Force

    } else { write-host "else statement started" }

    Output:

    IF Statement Started

    Cannot find any service with service name 'TestServiceName'.
        + CategoryInfo          : ObjectNotFound: (TestServiceName :String) [Get-Service], ServiceCommandException
        + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
        + PSComputerName        : Server1


    Thursday, May 23, 2019 4:48 PM

All replies

  • This worked fine for me

    if(1 -eq 1){
      Get-Service Wcmsvc
    }

    Status   Name               DisplayName
    ------   ----               -----------
    Running  Wcmsvc             Windows Connection Manager

    The error is saying that the service doesn't exist, and should be the same if you ran it outside

    Also since you are using the -Name parameter, make sure it is the actual service name and not the friendly display name, otherwise change the parameter to -DisplayName


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Thursday, May 23, 2019 6:27 PM typo
    Thursday, May 23, 2019 6:25 PM
  • Hello Clayman2,

    I just checked and it do work if I remove "-ComputerName $GW1" from the command. So it checks the Service Status on LocalHost and delivers the output.

    Do you see any error in the code? why it is not working with "-ComputerName $GW1" though I declared $GW1 inside the IF loop.

    And yes, I am using the actual name and not the Display name.

    Regards,
    Sahil Gupta


    Thursday, May 23, 2019 8:59 PM
  • A few questions first:

    • Why are you using a DOS batch file to find services in the "started" state?
    • Why does it matter if there are only seven services running?

    If you only want services that are running on the server, than "Get-Service" piped to "Where {$_.Status -eq 'Running'" will do.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, May 24, 2019 1:16 AM
  • Add this right before your "Get-Service . . .":

    if (test-connection $GW1 -quiet){
        Write-Output "Connection to $GW1 okay"
    }
    else{
        Write-Output "Connection tp $GW1 failed"
    }

    If you still have problems (other than a bad computer name), try replacing the "Get-Service . . . | Stop-Service . . ." with this:

    Get-WMIObject Win32_Service -computer $GW1 | Where {$_.Name -eq 'TestServiceName'} | ForEach { $_.stopservice() }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, May 24, 2019 1:43 AM