Invoke-command doesn't return expected result

I need to get the status of SQL services on remote computer. So I run this command.

Invoke-Command -ComputerName $server -Credential $creds -ScriptBlock { Get-Service -Name 'MSSQL$SQLEXPRESS','SQLBrowser','SQLTELEMETRY$SQLEXPRESS','SQLWriter' }

and get this output

Status   Name               DisplayName                            PSComputerName                                                                                                                                                 
------   ----               -----------                            --------------                                                                                                                                                 
Running  MSSQL$SQLEXPRESS   SQL Server (SQLEXPRESS)                192.168.160.1                                                                                                                                                   
Running  SQLBrowser         SQL Server Browser                     192.168.160.1                                                                                                                                                   
Running  SQLTELEMETRY$SQ... SQL Server CEIP service (SQLEXPRESS)   192.168.160.1                                                                                                                                                   
Running  SQLWriter          SQL Server VSS Writer                  192.168.160.1

Now I want to get the word “Running”, so I can create an if statement, if the service is not running, to start it. So I change the command to this

Invoke-Command -ComputerName $server -Credential $creds -ScriptBlock { (Get-Service -Name 'MSSQL$SQLEXPRESS','SQLBrowser','SQLTELEMETRY$SQLEXPRESS','SQLWriter').Status }

But instead of getting back the word “Running”, I now get this

PSComputerName RunspaceId                           Value  
-------------- ----------                           -----  
192.168.160.1   6ea29847-f5b5-4522-800f-0517f1f4baf7 Running

I suspect it’s because I’m using Invoke-Command.

How can I modify this command to get back the word “Running” so I can set up my if statement?

Thanks

Hi
Powershell is object language.
Please read about get-member command
try something like

get-service bits |get-member
(get-service bits).status |get-member

I tried this command
Invoke-Command -ComputerName 192.168.160.1 -Credential $cred -ScriptBlock {(Get-Service -Name SQLBrowser).Status | Get-Member}

and got this output

   TypeName: System.ServiceProcess.ServiceControllerStatus

Name        MemberType Definition                                                                                                                                                                                                 
----        ---------- ----------                                                                                                                                                                                                 
CompareTo   Method     int CompareTo(System.Object target), int IComparable.CompareTo(System.Object obj)                                                                                                                          
Equals      Method     bool Equals(System.Object obj)                                                                                                                                                                             
GetHashCode Method     int GetHashCode()                                                                                                                                                                                          
GetType     Method     type GetType()                                                                                                                                                                                             
GetTypeCode Method     System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()                                                                                                                                  
HasFlag     Method     bool HasFlag(System.Enum flag)                                                                                                                                                                             
ToBoolean   Method     bool IConvertible.ToBoolean(System.IFormatProvider provider)                                                                                                                                               
ToByte      Method     byte IConvertible.ToByte(System.IFormatProvider provider)                                                                                                                                                  
ToChar      Method     char IConvertible.ToChar(System.IFormatProvider provider)                                                                                                                                                  
ToDateTime  Method     datetime IConvertible.ToDateTime(System.IFormatProvider provider)                                                                                                                                          
ToDecimal   Method     decimal IConvertible.ToDecimal(System.IFormatProvider provider)                                                                                                                                            
ToDouble    Method     double IConvertible.ToDouble(System.IFormatProvider provider)                                                                                                                                              
ToInt16     Method     int16 IConvertible.ToInt16(System.IFormatProvider provider)                                                                                                                                                
ToInt32     Method     int IConvertible.ToInt32(System.IFormatProvider provider)                                                                                                                                                  
ToInt64     Method     long IConvertible.ToInt64(System.IFormatProvider provider)                                                                                                                                                 
ToSByte     Method     sbyte IConvertible.ToSByte(System.IFormatProvider provider)                                                                                                                                                
ToSingle    Method     float IConvertible.ToSingle(System.IFormatProvider provider)                                                                                                                                               
ToString    Method     string ToString(), string ToString(string format, System.IFormatProvider provider), string ToString(System.IFormatProvider provider), string ToString(string format), string IFormattable.ToString(strin...
ToType      Method     System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)                                                                                                                    
ToUInt16    Method     uint16 IConvertible.ToUInt16(System.IFormatProvider provider)                                                                                                                                              
ToUInt32    Method     uint32 IConvertible.ToUInt32(System.IFormatProvider provider)                                                                                                                                              
ToUInt64    Method     uint64 IConvertible.ToUInt64(System.IFormatProvider provider)                                                                                                                                              
value__     Property   int value__ {get;set;}

Hi

I think you should start first with learning what it’s Powershell.
Look on that. Old one, but done by 2 great person :slight_smile:

Since services with a startup type of Disabled wouldn’t start at all you have to check the startup type as well before you try to restart a service. :wink:

Often it is easier to inspect an object just by looking at its properties and ignore its methods at first. So you could run this:

Get-Service | 
Select-Object -Property * -First 1

Now you see all available properties of the first service on the machine you ran this command.

Now that you know the properties you could filter for services with the startuptype “Automatic” but with a status of “Stopped”. (For better readability we select only 4 properties to display)

Get-Service | 
Where-Object {
    $_.Status -eq 'Stopped' -and
    $_.StartType -eq 'Automatic'
} |
Select-Object -Property Name,DisplayName,Status,StartType

Now you can replace the Select-Object command with a loop and restart all services you want to be restarted. :wink: Of course you can use the same conditions used here for the Where-Object for an if statement inside a loop if you want.

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you making their work twice or more.

Thanks

I highly recommend to you to do a big step back and start with learning the very basics of PowerShell first. That will save you from a lot of wasted time and frustrations. And it will enable you to actually understand the help you get in forums like this. You cannot learn a complex technology like a scripting language by piecing togehter seom arbitrary peieces of code you’va found on the internet.