Problem with Get-Service

Tagged: 

This topic contains 6 replies, has 4 voices, and was last updated by  Ben Camareno 2 months, 2 weeks ago.

  • Author
    Posts
  • #72334

    Bob
    Participant

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

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

    Get-Service -ComputerName $ComputerName | where {$_.name -like '*MSOLAP*' -or
    $_.name -like '*MSSQL*' -or
    $_.name -like '*SQLAgent*' -or
    $_.name -like '*ReportServer*' -or
    $_.name -like '*SQLBrowser*' -or
    $_.name -like '*SQLPBENGINE*' -or
    $_.name -like '*SQLPBDMS*' -or
    $_.name -like '*MsDtsServer*' -or
    $_.name -like '*MSSQLLaunchpad*' -or
    $_.name -like '*SQLSERVERAGENT*' -and
    $_.name -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 {$_.name -eq 'MSOLAP$DVLP2014'} |Restart-Service
    Error:
    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

    Frank Tucker
    Participant

    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

    Bob
    Participant

    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

    Curtis Smith
    Participant

    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.

    1)

    -InputObject
    
    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
    Aliases: 
    
    Required: True
    Position: 0
    Default value: None
    Accept pipeline input: True (ByValue)
    Accept wildcard characters: False

    2)

    -Name
    
    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

    Bob
    Participant

    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

    Curtis Smith
    Participant

    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

    or

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

    Ben Camareno
    Participant

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

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

You must be logged in to reply to this topic.