Formatting output to pass as part of a where clause from Select_Object

  • I am really new to PS and I am trying to write a script to set up our AGs with RegisterProviderAllIPs = 0. In order to do this, I need to get the name of the Network Name associated with the AG. For this I start by determining the name of the AG:

    Get-ClusterResource | Where-Object -Property ResourceType -EQ "SQL Server Availability Group" | Select-Object Name

    That's all fine, but I want to save that into a variable and then use the variable to filter in order to find the Network Name where I will set the cluster parameters:

    $AGName = Get-ClusterResource | Where-Object -Property ResourceType -EQ "SQL Server Availability Group" | Select-Object Name

    Get-ClusterResource | Where-Object {$_.OwnerGroup -EQ $AGName -AND $_.ResourceType -EQ "Network Name"} | Select-Object Name

    However, this is failing because $AGName is not literally just the string of the name. It is "Name: Agname" How can I set this up properly?

    Jared
    CE - Microsoft

  • Find out what type $AGName is. You can just execute $AGName at the PS command line or $AGName.GetType().

    From this you should be able to find the property to get the "name" value for the AG.

    Once you get that you will be able to replace $AGName with $AGName.Value (or whatever you discover the property to be) in the second statement.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Hmm... I get the following when I use GetType()

    IsPublic IsSerial Name BaseType

    -------- -------- ---- --------

    True False PSCustomObject System.Object

    Not really sure what to do with that.

    Jared
    CE - Microsoft

  • Ok, so I piped the variable to Gm and got the following:

    Name MemberType Definition

    ---- ---------- ----------

    Equals Method bool Equals(System.Object obj)

    GetHashCode Method int GetHashCode()

    GetType Method type GetType()

    ToString Method string ToString()

    Name NoteProperty System.String Name=SSRSINT_AG

    So, I then used $AGName.Name and it worked! Final code:

    $AGName = Get-ClusterResource | Where-Object -Property ResourceType -EQ "SQL Server Availability Group" | Select-Object Name

    Get-ClusterResource | Where-Object {$_.OwnerGroup -EQ $AGName.Name -AND $_.ResourceType -EQ "Network Name"} | Select-Object Name

    Jared
    CE - Microsoft

  • Starting point:

    Get-ClusterResource | Where-Object -Property ResourceType -EQ "SQL Server Availability Group" | Select-Object Name

    Option 1: Just add -ExpandProperty to your select-object:

    Get-ClusterResource | Where-Object -Property ResourceType -EQ "SQL Server Availability Group" | Select-Object -ExpandProperty Name

    This will change the type to System.String and allow you to use it for your filter.

    Option 2: Specify the property name which you already deteremined

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply