Forum Discussion

Jake_46670's avatar
Jake_46670
Icon for Nimbostratus rankNimbostratus
Jun 04, 2009

Working with Datagroup members in Powershell

I'm run into a bit of an issue when attempting to work with members of a Datagroup via powershell. So far I have:

 

 

If ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null )

 

{

 

Add-PSSnapIn iControlSnapIn

 

}

 

 

$LBConRC = Initialize-F5.iControl -Hostname $LBAddress -Username $LBUsername -Password $LBPassword

 

$LBClass = (Get-F5.iControl).LocalLBClass

 

 

$colOldVer = $LBClass.get_string_class((,"POD_Org_Old_Version"))

 

 

I know my datagroup "POD_Org_Old_version" contains a couple of strings, i've verified this by:

 

 

$colOldVer

 

 

I see the strings as expected but from that point... how can I, for example, check if the string "Engineer_Not_Developer" is in the datagroup? I've tried:

 

 

if ($colOldVer -contains "Engineer_Not_Developer") {...

 

 

Without luck... I also tried a foreach...

 

 

Thanks for the help!

 

Jason

1 Reply

  • Sorry for the delay in the response, somehow this slipped on my radar.

     

     

    There are a couple of ways you can search for a comparison on a string. First, let's look at the response from the get_string_class method so we are starting in the right place. The API definition for that method is Click here.

     

     

    This is defined as:

     

     

    class StringClass { 
       String name; 
       String [] members; 
     }; 
     StringClass [] get_string_class( 
         in String [] class_names 
     );

     

     

    So the returned value from the call is an array of StringClass objects. Since you are only passing in a single value of "POD_Org_Old_Version", the returned array will be of size 1.

     

     

    The next step will be iterating through the "members" array in the returned list. In your example, you were trying to match on the StringClass array while you should have been matching on the enclosed "members" member.

     

     

    The following code should work for an exact comparison. You can replace "-eq" with "-match" or "-regex" if you want to do more of a wildcard based comparison.

     

     

    $classList = $LBClass.get_string_class((,"POD_Org_Old_Version")); 
     $class = $classList[0]; 
     $matches = $class.Members | Where-Object { $_ -eq "Engineer_Not_Developer" } 
     if ( $matches ) { Write-Host "Found match!" } else { Write-Host "Didn't find match" }

     

     

    Or, you could do it a bit more concise like this:

     

     

    $classList = $LBClass.get_string_class((,"POD_Org_Old_Version")); 
     if ( $classList[0].Members | Where-Object { $_ -eq "Engineer_Not_Developer" } ) 
     { 
       Write-Host "Found match!" 
     } 
     else  
     { 
       Write-Host "Didn't find match"  
     }

     

     

    You could also do a "foreach" over the $classList[0].Members if you wanted to. That's essentially what the Where-Object cmdlet does.

     

     

    $classList = $LBClass.get_string_class((,"POD_Org_Old_Version")); 
     foreach ($member in $classList[0].Members ) 
     { 
       if ( $member -eq "Engineer_Not_Developer" } 
       { 
         Write-Host "Found match!" 
       } 
       else  
       { 
         Write-Host "Didn't find match"  
       } 
     }

     

     

    Hope this helps...

     

     

    Please feel free to post any other issues you come across while working with PowerShell as I'd be glad to help you out if I can.

     

     

    -Joe