Forum Discussion

Brandon_Lay_104's avatar
Brandon_Lay_104
Icon for Nimbostratus rankNimbostratus
Jan 13, 2009

How do I edit Datagroups via PowerShell

Can someone give me a clue on the method(s) to call for adding strings to datagroups?

6 Replies

  • Check out this tech tip I wrote on Data Groups where I used the iControl Assembly via the PowerShell cmdlets.

     

     

    http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=204

     

    Click here

     

     

     

    -Joe
  • Awesome. that answers a lot of questions. However, I'm actually trying to deal with Data Groups of type Address and thus having difficulty adding a LocalLBClassAddressEntry. I only need to add host entries, not network addresses.

     

     

    I've gotten thus far...

     

     

    Begin

     

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

     

    $AddressClass = New-Object -typename iControl.LocalLBClassAddressClass

     

     

    $AddressClass.name = "MyAddressGroup"

     

     

    Here is where I get my ToString error....

     

    $AddressClass.members = ("10.100.100.1")

     

     

    (Also) If I change it to the following, it seems to want

     

    a network address. Not a host address.

     

    $AddressClass.members = (10.100.100.1)

     

     

    then presumably I would do this...?

     

    $Class.add_address_class_member(,$AddressClass)

     

     

    and when I do the following, it just tells me I get

     

    the member is {iControl.LocalLBClassAddressEntry}

     

    rather than the actual member entry

     

    $Class.get_address_class((,"MyAddressGroup"))

     

     

    End

     

     

    Help!

     

     

    -Brandon

     

  • You are close. The LocalLBClassAddressClass's members member is of type LocalLB.Class.AddressEntry. You'll need to change your string literal of (10.100.100.1) to an appropriate structure. This should work:

    $Class = (Get-F5.iControl).LocalLBClass 
     $AddressClass = New-Object -TypeName iControl.LocalLBClassAddressClass 
      
     $AddressClass.name = "MyAddressGroup" 
      
      Allocate a AddressEntry structure with a  
      netmask of 255.255.255.255 for a Host address. 
     $AddrEntry = New-Object -TypeName iControl.LocalLBClassAddressEntry 
     $AddrEntry.address = "10.100.100.1" 
     $AddrEntry.netmask = "255.255.255.255" 
      
      Assign the entry to the members member. 
     $AddressClass.members = (,$AddrEntry) 
      
      add the address class members 
     $Class.add_address_class_member( (,$AddressClass) )

    Hope this helps!

    -Joe
  • Joe, you ARE the man! Thanks. And for all of you people out there....here is the fruit of my work with Joe's much helpful input. This script can easily be converted to do Data Groups of type String (my next task). I'm sure I could have tightened up things (like the check if the data group exists or not), but it's quite functional. Enjoy!

     ====================  
      ----------------------------------------------------------------------------  
       Created By Brandon Lay  
        
        *Use at your own discretion and your own risk*  
        
       PowerShell Script to Manage Data Groups of type Address.  
      - Create an Address Data Group  
      - Add Address Members to a Data Group  
      - Delete a Data Group  
      - Delete all members in an Address Data Group  
        
      Connection criteria (host IP, userid and pwd) embedded, so set vars below  
      Parameters to pass are Action, Data Group Name, Member to Add  
      For valid Parameters, check out the usage or run script w/o params  
        
      i.e. Addr_DataGroup_Mgmt.ps1 Action AddMember MYDataGroup .gif  
        
      ----------------------------------------------------------------------------  
      param($Action=$null, $DataGroup=$null, $NewMemberToAdd=$null)  
        
        
       Set the connection criteria for device below  
        
      $g_bigip="0.0.0.0"  
      $g_uid="username"  
      $g_pwd="password"  
        
      set-psdebug -strict  
        
         Check to make sure the iControlSnapIn is loaded; otherwise load it.  
        
      $checkSnapIn = Get-PSSnapIn iControlSnapIn -ErrorAction SilentlyContinue;  
      If ($checkSnapIn -notlike "iControlSnapIn") {Add-PSSnapIn iControlSnapIn};  
        
        
      -------------------------------------------------------------------------  
       function Create-AddrGroup  
      -------------------------------------------------------------------------  
      function Create-AddrGroup()  
      {  
      $Exists = "False"  
      $Class = (Get-F5.iControl).LocalLBClass  
      Check if Group Exists first  
      ForEach ($a in $Class.get_address_class_list())   
      {  
      if ($a -like $DataGroup)   
      {  
      $Exists = "True"  
      }  
      }  
      If ($Exists -eq "True") {GeneralError("DataGroup $DataGroup already exists!")}  
        
      $AddressClass = New-Object -TypeName iControl.LocalLBClassAddressClass   
      $AddressClass.name = $DataGroup  
        
       It seems that in order to create a group, you must specify at least one member  
         Sooo, I'll create a bogus entry and then call the Delete-AllMembers Function  
         to create an empty group.  
      $AddrEntry = New-Object -TypeName iControl.LocalLBClassAddressEntry   
       $AddrEntry.address = "1.1.1.1"   
       $AddrEntry.netmask = "255.255.255.255"   
          
       Assign the entry to the members member.   
        
       $AddressClass.members = (,$AddrEntry)   
      $Class.create_address_class(,$AddressClass)  
        
       Now call Delete-AllMembers to result in a new empty group  
        However if I call it too soon, apparently the new data group doesn't show up yet  
         So let's wait a couple seconds  
        
      Delete-AllMembers($DataGroup)  
        
      $Class.get_address_class_list()  
        
      }  
        
        
      -------------------------------------------------------------------------  
       function Add-AddrMembers  
      -------------------------------------------------------------------------  
      function Add-AddrMembers()  
      {  
        
      $Exists = "False"  
      Check if $NewMemberToAdd is null  
      if ($AddrMember=$null) {GeneralError("NewMemberToAdd must be specified in parameters for this Action")}  
        
      $Class = (Get-F5.iControl).LocalLBClass  
        
      Check if Group Exists first  
      ForEach ($a in $Class.get_address_class_list())   
      {  
      if ($a -like $DataGroup)   
      {  
      $Exists = "True"  
      }  
      }  
      If ($Exists -ne "True") {GeneralError("DataGroup $DataGroup doesn't exist!")}  
        
      $AddressClass = New-Object -TypeName iControl.LocalLBClassAddressClass   
      $AddressClass.name = $DataGroup  
        
       Allocate a AddressEntry structure with a    
       netmask of 255.255.255.255 for a Host address.   
        
       $AddrEntry = New-Object -TypeName iControl.LocalLBClassAddressEntry   
       $AddrEntry.address = $NewMemberToAdd   
       $AddrEntry.netmask = "255.255.255.255"   
          
       Assign the entry to the members member.   
        
       $AddressClass.members = (,$AddrEntry)   
        
        add the address class members   
        
       $Class.add_address_class_member( (,$AddressClass) )  
        
      }  
        
      -------------------------------------------------------------------------  
       function Delete-DataGroup  
      -------------------------------------------------------------------------  
      function Delete-DataGroup()  
      {  
      $Exists = "False"  
      $Class = (Get-F5.iControl).LocalLBClass  
      ForEach ($a in $Class.get_address_class_list())   
      {  
      if ($a -like $DataGroup)   
      {  
      $Exists = "True"  
      }  
      }  
      If ($Exists -ne "True") {GeneralError("DataGroup $DataGroup doesn't exist!")}  
        
      $Class.delete_class(,$DataGroup)  
      $Class.get_address_class_list()  
      }  
        
      -------------------------------------------------------------------------  
       function Delete-AllMembers  
      -------------------------------------------------------------------------  
      function Delete-AllMembers()  
      {  
      $Exists = "False"  
      $Class = (Get-F5.iControl).LocalLBClass  
        
      Check if Group Exists first  
      ForEach ($a in $Class.get_address_class_list())   
      {  
      if ($a -like $DataGroup)   
      {  
      $Exists = "True"  
      }  
      }  
      If ($Exists -ne "True") {GeneralError("DataGroup $DataGroup doesn't exist!")}  
        
      $Class.delete_address_class_member($Class.get_address_class((,$DataGroup)))  
      $Class.get_address_class_list()  
      }  
        
      -------------------------------------------------------------------------  
       function usage  
      -------------------------------------------------------------------------  
      function usage()  
      {  
      Write-Host "Usage: DataGroup_Mmgmt.ps1 Action DataGroupName (optional MemberToAdd)";  
      Write-Host "       Valid Actions are CreateGroup:AddMember:DeleteGroup:DeleteAllMembers";  
      Write-Host "       DataGroupName must be specified";  
      Write-Host "       MemberToAdd is only required for Action AddMember ";  
      exit;  
      }  
        
      -------------------------------------------------------------------------  
       function GeneralError  
      -------------------------------------------------------------------------  
      function GeneralError($ErrorMessage)  
      {  
      Write-Host $ErrorMessage;  
      usage;  
        
      }  
        
      -------------------------------------------------------------------------  
       Exception Handler  
      -------------------------------------------------------------------------  
      trap [System.Exception]  
      {  
      Write-Host "Exception Caught"  
      Write-Error $_.Exception.Message;  
      exit;  
      }  
        
      -------------------------------------------------------------------------  
       Main Application Logic  
      -------------------------------------------------------------------------  
      if ( ($Action -eq $null) -or ($DataGroup -eq $null) )  
      {  
      usage;  
      }  
        
        
      $success = Initialize-F5.iControl -HostName $g_bigip -Username $g_uid -Password $g_pwd;  
        
        
        
        
      If ($Action -eq "CreateGroup")   
      {Create-AddrGroup}  
      elseif ($Action -eq "AddMember")  
      {Add-AddrMembers}  
      elseif ($Action -eq "DeleteGroup")  
      {Delete-DataGroup}  
      elseif ($Action -eq "DeleteAllMembers")  
      {Delete-AllMembers}  
      else  
      {GeneralError("No supported Action specified!!!")}  
        
      ======================================= 

  • No, YOU are the man! Thanks for sticking in there and also for the contribution. I'll add it to the CodeShare!

     

     

    Feel free to post any other questions that come up.

     

     

    -Joe
  • I liked this script a lot. Can you tell me what resources should I look into in order to convert this script in Java? I have a hard time to find all the constructors/destructors and public interfaces for F5 framework. For example, I don't know how to instantiate LocalLBClassAddressEntry object with a string IP address.

     

     

    Thanks,

     

    Eve