locked
Is this a bug? Start-Service | stop-Service RRS feed

  • Question

  • Hello,

    I just ran into an interesting problem when stopping a service on a remote computer. 

    Get-service -name healthservice 
    
    # This returns my local healthservice, happily running
    
    Get-service -name healthservice -computername OtherComputer
    
    #This returns the remote service running
    
    Get-service -name healthservice -computername OtherComputer | Stop-Service
    
    #This doesn't return any errors
    
    Get-service -name healthservice -computername OtherComputer
    
    # this returns the remote service still listed as running
    
    Get-service -name healthservice
    
    #This returns my local healthservice now stopped
    

    What the heck!?!  Do I have to use WMI to do this correctly?  and why does stop service kill the local health service? This feels like a connect bug in the making...


    Hope that helps! Jason

    Friday, May 24, 2013 8:04 PM

Answers

  • Yes, you'd need WMI.

    Stop-Service does not have a ComputerName parameter (even in v3)

    Try this:

    $WmiObject = @{
        Class = "Win32_Service"
        ComputerName = $Computer
        Filter = "Name='healthservice'"
    }
    (Get-WmiObject @WmiObject).StopService() |Out-Null

    Karl

    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer



    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})

    Friday, May 24, 2013 8:14 PM

All replies

  • I should add I'm running Version 2, I'll try it in my lab on version 3

    Hope that helps! Jason

    Friday, May 24, 2013 8:05 PM
  • Yes, you'd need WMI.

    Stop-Service does not have a ComputerName parameter (even in v3)

    Try this:

    $WmiObject = @{
        Class = "Win32_Service"
        ComputerName = $Computer
        Filter = "Name='healthservice'"
    }
    (Get-WmiObject @WmiObject).StopService() |Out-Null

    Karl

    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer



    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})

    Friday, May 24, 2013 8:14 PM
  • Thanks Karl, I was tracking on the GWMI route, I had just not figured that it would stop the local service when piped the service from the remote computer.  After looking at the way it accepts the name property it makes sense.

    Hope that helps! Jason

    Friday, May 24, 2013 8:33 PM
  • I am going to file a connect bug though, it should check that it's stopping the right service.

    Hope that helps! Jason

    Friday, May 24, 2013 8:34 PM