none
Need help getting property values RRS feed

  • Question

  • I'm able to use

    (Get-Service).ServiceName

    on my windows 10/PS v5 which returns all of the services names on my machine (just the name) as expected.  I'm also able to do this on a Windows 7 (PS upgraded to v4) successfully.  But when I try this on a Windows Server 2008 R2 / PS v3, I get absolutely nothing returned.  I did a

    Get-Service | Get-Member

    and verified that the property name was the same.  I also tried this on a Windows Server 2008 / PS v1, and still get nothing returned.

    Am I doing this right?

    Ultimately what I am trying to accomplish exactly is running this code:

    (Get-Service).ServiceName -contains "SNMP"

    So I'm wanting a $true or a $false returned.  Is there maybe a better way of doing this, and in a manner that actually works?



    Thursday, December 10, 2015 1:48 PM

Answers

  • This works as expected in v3.

    For v2, you need to do something like this:

    PS C:\> (Get-Service | Select -ExpandProperty ServiceName) -contains 'SNMP'
    False
    
    PS C:\> (Get-Service | Select -ExpandProperty ServiceName) -contains 'WSearch'
    True


    • Marked as answer by Thomas Garrity Thursday, December 10, 2015 1:57 PM
    Thursday, December 10, 2015 1:53 PM
  • I just noticed I accidentally mentioned PS v3 in the original post on a Windows Server 2008 R2 system. Meant to say v2.

    Anyways, now on the Windows Server 2008 PS v1, I'm getting an error.  I'm not quite sure what changed.

    The following appears for every service that gets returned.

    (Get-Service | Select-Object  <<<< -ExpandProperty ServiceName) -contains 'SNMP'
    
    Select-Object : Cannot expand property "ServiceName" because it has nothing to expand.
    

    I did find these posted as a bug

    https://connect.microsoft.com/PowerShell/feedback/details/657211/select-object-expand-property-throws-and-exception-and-dies-when-property-is-null

    http://powershell.com/cs/blogs/tips/archive/2010/05/11/expandproperty-rocks-sometimes.aspx

    So here's what I did to get around this:

    (Get-Service | Foreach-Object {$_.ServiceName}) -contains 'SNMP'

    • Marked as answer by Thomas Garrity Friday, December 11, 2015 2:41 PM
    Friday, December 11, 2015 2:40 PM

All replies

  • This works as expected in v3.

    For v2, you need to do something like this:

    PS C:\> (Get-Service | Select -ExpandProperty ServiceName) -contains 'SNMP'
    False
    
    PS C:\> (Get-Service | Select -ExpandProperty ServiceName) -contains 'WSearch'
    True


    • Marked as answer by Thomas Garrity Thursday, December 10, 2015 1:57 PM
    Thursday, December 10, 2015 1:53 PM
  • Awesome!  Thanks Mike, that worked!
    Thursday, December 10, 2015 1:57 PM
  • Cheers, you're welcome.

    Thursday, December 10, 2015 2:00 PM
  • I just noticed I accidentally mentioned PS v3 in the original post on a Windows Server 2008 R2 system. Meant to say v2.

    Anyways, now on the Windows Server 2008 PS v1, I'm getting an error.  I'm not quite sure what changed.

    The following appears for every service that gets returned.

    (Get-Service | Select-Object  <<<< -ExpandProperty ServiceName) -contains 'SNMP'
    
    Select-Object : Cannot expand property "ServiceName" because it has nothing to expand.
    

    I did find these posted as a bug

    https://connect.microsoft.com/PowerShell/feedback/details/657211/select-object-expand-property-throws-and-exception-and-dies-when-property-is-null

    http://powershell.com/cs/blogs/tips/archive/2010/05/11/expandproperty-rocks-sometimes.aspx

    So here's what I did to get around this:

    (Get-Service | Foreach-Object {$_.ServiceName}) -contains 'SNMP'

    • Marked as answer by Thomas Garrity Friday, December 11, 2015 2:41 PM
    Friday, December 11, 2015 2:40 PM