Problem with Get-Service

Welcome Forums General PowerShell Q&A Problem with Get-Service

This topic contains 6 replies, has 4 voices, and was last updated by

1 year, 9 months ago.

  • Author
  • #72334

    Points: 0
    Rank: Member

    i have created a function like below:
    function Get-SQLServices

    # Param1 help description
    [string[]]$ComputerName = $env:COMPUTERNAME

    Get-Service -ComputerName $ComputerName | where {$ -like '*MSOLAP*' -or
    $ -like '*MSSQL*' -or
    $ -like '*SQLAgent*' -or
    $ -like '*ReportServer*' -or
    $ -like '*SQLBrowser*' -or
    $ -like '*SQLPBENGINE*' -or
    $ -like '*SQLPBDMS*' -or
    $ -like '*MsDtsServer*' -or
    $ -like '*MSSQLLaunchpad*' -or
    $ -like '*SQLSERVERAGENT*' -and
    $ -notlike '*MSSQLFDLauncher*'
    } | Select status, Name, DisplayName, MachineName ,DependentServices,ServicesDependedOn


    and when i try to use below command i get error although the service exists:
    Get-SQLServices -ComputerName myserver| where {$ -eq 'MSOLAP$DVLP2014'} |Restart-Service
    Restart-Service : Cannot find any service with service name 'MSOLAP$DVLP2014'.
    At line:1 char:86
    + ... AP$DVLP2014'} |Restart-Service
    + ~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (MSOLAP$DVLP2014:String) [Restart-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.RestartServiceCommand

  • #72340

    Points: 1
    Rank: Member

    Looks like you are get a list of service from a remote computername, but you are try to restart a service on your local computer.

  • #72460

    Points: 0
    Rank: Member

    No, i am sending the result through the pipe-line.
    it works fine if i write it like
    Get-Service *sql* -computername myserver | Restart-Service

  • #72463

    Points: 1
    Rank: Member

    In your function the last thing you are doing is a select-object

     | Select status, Name, DisplayName, MachineName ,DependentServices,ServicesDependedOn

    The result of this is a PSCustomObject being passed to the pipeline.

    When you pipe that into Restart-Service it fails because it only accepts 2 variations of pipeline input.


    Specifies ServiceController objects that represent the services to restart. Enter a variable that contains the objects, or type a command or expression that gets the objects.
    Type: ServiceController[]
    Parameter Sets: InputObject
    Required: True
    Position: 0
    Default value: None
    Accept pipeline input: True (ByValue)
    Accept wildcard characters: False


    Specifies the service names of the services to restart.
    Type: String[]
    Parameter Sets: Default
    Aliases: ServiceName
    Required: True
    Position: 0
    Default value: None
    Accept pipeline input: True (ByPropertyName, ByValue)
    Accept wildcard characters: False

    As you see the -InputObject requires a "ServiceController" object, and -Name requires a String. You are passing neither a string or a ServiceController object.

    If you removed the select-object at the end of your function, it would probably work just fine.

  • #72509

    Points: 0
    Rank: Member

    Thanks for your reply. but the point is i need to use that select.
    how can i fix it by using select!

    and when i do Get-Member:

    Get-SQLServices | gm 

    it returns TypeName: Selected.System.ServiceProcess.ServiceController not PSCustomObject

  • #72521

    Points: 1
    Rank: Member

    Ok it's not a PSCustomObject, but it's still not a System.ServiceProcess.ServiceController, it's a Selected.System.ServiceProcess.ServiceController, which is different and not what Restart-Service is expecting.

       TypeName: Selected.System.ServiceProcess.ServiceController
    Name                MemberType   Definition                                                                                            
    ----                ----------   ----------                                                                                            
    Equals              Method       bool Equals(System.Object obj)                                                                        
    GetHashCode         Method       int GetHashCode()                                                                                     
    GetType             Method       type GetType()                                                                                        
    ToString            Method       string ToString()                                                                                     
    CanPauseAndContinue NoteProperty System.Boolean CanPauseAndContinue=True                                                               
    CanShutdown         NoteProperty System.Boolean CanShutdown=True                                                                       
    CanStop             NoteProperty System.Boolean CanStop=True                                                                           
    Container           NoteProperty  Container=null                                                                                       
    DependentServices   NoteProperty System.ServiceProcess.ServiceController[] DependentServices=System.ServiceProcess.ServiceController[] 
    DisplayName         NoteProperty System.String DisplayName=SQL Server (MSSQLSERVER)                                                    
    MachineName         NoteProperty System.String MachineName=server                                                                      
    Name                NoteProperty System.String Name=MSSQLSERVER                                                                        
    RequiredServices    NoteProperty System.ServiceProcess.ServiceController[] RequiredServices=System.ServiceProcess.ServiceController[]  
    ServiceHandle       NoteProperty SafeServiceHandle ServiceHandle=SafeServiceHandle                                                     
    ServiceName         NoteProperty System.String ServiceName=MSSQLSERVER                                                                 
    ServicesDependedOn  NoteProperty System.ServiceProcess.ServiceController[] ServicesDependedOn=System.ServiceProcess.ServiceController[]
    ServiceType         NoteProperty System.ServiceProcess.ServiceType ServiceType=Win32OwnProcess                                         
    Site                NoteProperty  Site=null                                                                                            
    Status              NoteProperty System.ServiceProcess.ServiceControllerStatus Status=Running                                          
       TypeName: System.ServiceProcess.ServiceController
    Name                      MemberType    Definition                                                                                                                  
    ----                      ----------    ----------                                                                                                                  
    Name                      AliasProperty Name = ServiceName                                                                                                          
    RequiredServices          AliasProperty RequiredServices = ServicesDependedOn                                                                                       
    Disposed                  Event         System.EventHandler Disposed(System.Object, System.EventArgs)                                                               
    Close                     Method        void Close()                                                                                                                
    Continue                  Method        void Continue()                                                                                                             
    CreateObjRef              Method        System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)                                                             
    Dispose                   Method        void Dispose(), void IDisposable.Dispose()                                                                                  
    Equals                    Method        bool Equals(System.Object obj)                                                                                              
    ExecuteCommand            Method        void ExecuteCommand(int command)                                                                                            
    GetHashCode               Method        int GetHashCode()                                                                                                           
    GetLifetimeService        Method        System.Object GetLifetimeService()                                                                                          
    GetType                   Method        type GetType()                                                                                                              
    InitializeLifetimeService Method        System.Object InitializeLifetimeService()                                                                                   
    Pause                     Method        void Pause()                                                                                                                
    Refresh                   Method        void Refresh()                                                                                                              
    Start                     Method        void Start(), void Start(string[] args)                                                                                     
    Stop                      Method        void Stop()                                                                                                                 
    WaitForStatus             Method        void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desiredStatus), void WaitForStatus(System.ServiceProcess...
    CanPauseAndContinue       Property      bool CanPauseAndContinue {get;}                                                                                             
    CanShutdown               Property      bool CanShutdown {get;}                                                                                                     
    CanStop                   Property      bool CanStop {get;}                                                                                                         
    Container                 Property      System.ComponentModel.IContainer Container {get;}                                                                           
    DependentServices         Property      System.ServiceProcess.ServiceController[] DependentServices {get;}                                                          
    DisplayName               Property      string DisplayName {get;set;}                                                                                               
    MachineName               Property      string MachineName {get;set;}                                                                                               
    ServiceHandle             Property      System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}                                                              
    ServiceName               Property      string ServiceName {get;set;}                                                                                               
    ServicesDependedOn        Property      System.ServiceProcess.ServiceController[] ServicesDependedOn {get;}                                                         
    ServiceType               Property      System.ServiceProcess.ServiceType ServiceType {get;}                                                                        
    Site                      Property      System.ComponentModel.ISite Site {get;set;}                                                                                 
    Status                    Property      System.ServiceProcess.ServiceControllerStatus Status {get;}                                                                 
    ToString                  ScriptMethod  System.Object ToString();                                      

    Can you not just do your select, for whatever reason you need it, after the function returns?

    $services = Get-SQLServices -ComputerName server
    $services | Where-Object {$_.Name -eq 'MSOLAP$DVLP2014'} | Restart-Service
    $services | Select status, Name, DisplayName, MachineName ,DependentServices,ServicesDependedOn


    Get-SQLServices -ComputerName server |
    Where-Object {$_.Name -eq 'MSOLAP$DVLP2014'} |
    ForEach-Object {
        $_ | Restart-Service
        $_ | Select status, Name, DisplayName, MachineName ,DependentServices,ServicesDependedOn
  • #72524

    Points: 0
    Rank: Member

    You probably need to wrap your Restart-Service in a Invoke-Command:

    Invoke-Command {Restart-Service $_ } –ComputerName $ComputerName

The topic ‘Problem with Get-Service’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort