Forum Discussion

wallst32_178793's avatar
wallst32_178793
Icon for Nimbostratus rankNimbostratus
May 08, 2015

How do you call Get-F5.iControl multiple times in PowerShell?

I just started using iControl with PowerShell and haven't had any issues performing a single common task (for example retrieving the list of iRules). But now I'm trying to write some more functional scripts and running into problems when Get-F5.iControl is called a second time. It always returns a $NULL result.

 

Here is a basic example:

 

Code
$IC = Get-F5.iControl
 Retrieve a specific iRule DataGroupList

$CLASS = $IC.LocalLBClass
$MEMBERS = ($IC.LocalLBClass.Get_String_Class("Test_DG")) 
$MEMBERS

 Retrieve a list of all iRules

$IRULES = $IC.LocalLBRule.query_all_rules()
$IRULES

If I run the above, it will return the "Test_DG" DataGroupList as an object, but nothing for the iRules.

 

Now if I simply reverse the order:

 

Code
$IC = Get-F5.iControl

 Retrieve a list of all iRules
$IRULES = $IC.LocalLBRule.query_all_rules()
$IRULES

 Retrieve a specific iRule DataGroupList
$CLASS = $IC.LocalLBClass
$MEMBERS = ($IC.LocalLBClass.Get_String_Class("Test_DG")) 
$MEMBERS

Now the list of iRules is returned, but the datagroup list is not.

 

I have also tried "fresh" Get-F5.iControl calls rather than a stored object but the results are the same (for example):

 

Code
$IRULES = (Get-F5.iControl).LocalLBRule.query_all_rules()
$IRULES

$CLASS = (Get-F5.iControl).LocalLBClass
$MEMBERS = ((Get-F5.iControl).LocalLBClass.Get_String_Class("Test_DG")) 
$MEMBERS

Please advise.

 

6 Replies

  • Hi wallst

    I've fiddled quite a bit with powershell and I must say I've never seen this before. The object is actually not Null, it contains data, but it is not shown for some reason. The only explanation I can think of is that it's a bug.

    Check this out:

    $user = 'user'
    $pass = 'password'
    
    if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null ){
        Add-PSSnapIn iControlSnapIn
    }
    
    $success = Initialize-F5.iControl -HostName 10.0.0.1 -Username $user -Password $pass;
    $f5 = Get-F5.iControl
    
    Set the partition (good practice)
    $f5partitions = $f5.ManagementPartition
    $f5partitions.set_active_partition("Common")
    
     Retrieve a specific iRule DataGroupList
    $members = $f5.LocalLBClass.Get_String_Class("images")
    
     Retrieve a list of all iRules
    
    $irules = $f5.LocalLBRule.query_all_rules()
    
    $members
    $irules.Count
    
    For($x=0;$x -lt $IRULES.Count;$x++){
        if($irules[$x].rule_name -eq "/Common/_sys_auth_ssl_crldp") {
            "match"
        }
    }
    

    That would give me the following output in my case:

    name                                                   members
    ----                                                   -------
    images                                                 {.bmp, .gif, .jpg}
    39
    match
    

    As you can see the data is there, but not shown. If I post execution enter one of the variables I get the data:

    rule_name                                                                                         rule_definition
    ---------                                           ---------------
    /Common/_sys_auth_ssl_cc_ldap                       when CLIENT_ACCEPTED {...
    /Common/_sys_auth_ssl_ocsp                          when CLIENT_ACCEPTED {...
    /Common/_sys_auth_ssl_crldp                         when CLIENT_ACCEPTED {...
    /Common/_sys_auth_krbdelegate                       when HTTP_REQUEST {...
    /Common/_sys_https_redirect                         when HTTP_REQUEST {...
    ...
    

    If I have some more time I'll go through it a bit more, but I hope that helps at least a bit.

    Btw, setting the partition is good practice. You can get unpleasant side effects otherwise. 🙂

    /Patrik

  • This worked:

    For($x=0;$x -lt $irules.Count;$x++){
    
        $irules[$x].rule_name
        $irules[$x].rule_definition
    
    }
    

    Also, if you put them in a file you get the data in the variable as well:

    $members | Out-File .\test.txt
    $irules | Out-File -Append .\test.txt
    

    Very very very annoying. 🙂

    /Patrik

  • Patrick - thanks for taking the time to test and provide feedback. Stating "$NULL" was probably a poor choice on my part as I never tested the condition that $SOMEQUERYRESULT -eq $NULL was $TRUE; but just assumed that was the case because my results were "empty" as you saw. I did out-file the second query and it does contain data. I guess I would need to out-file, and then import that data, for processing; not very pretty...

     

  • Hi!

    Actually, as the data is there you can go through it and process it.

    Try:

    For($x=0;$x -lt $irules.Count;$x++){
    
        $irules[$x].rule_name
        $irules[$x].rule_definition
    
    }
    

    /Patrik

  • That's a good one. I think it has to do with the default way that objects are pushed to the powershell command pipeline. When the first object is pushed, it sets up a table with columns,etc. When you enter the next object, it's looking for the previous columns (properties) from the last object and when it doesn't find them, it treats it like the object isn't part of the original result set. PowerShell does this so that you can display numerous objects one after another and it will print like they are all part of the same table.

     

    You can get around this by explicitly passing the object through a formatter like format-table or format-list.

     

    For your first example, give this a try:

     

    Code
    $IC = Get-F5.iControl
     Retrieve a specific iRule DataGroupList
    
    $MEMBERS = ($IC.LocalLBClass.Get_String_Class("Test_DG")) 
    $MEMBERS | Format-Table
    
     Retrieve a list of all iRules
    
    $IRULES = $IC.LocalLBRule.query_all_rules()
    $IRULES | Format-Table

    I've been writing PowerShell scripts for almost 10 years and since 99.99% of the time I parse the returned objects, I haven't come across this one. Next time we run into each other, I'll buy you a drink for finding out something I didn't know about PowerShell. If you by chance are at one of our Agility events this year, look me up!

     

    Hope this helps...

     

    -Joe

     

  • Hi,

     

    What version of Powershell are you running? I just tested your first 10 lines and both iRules and "Test_DG" are returned. I am running Powershell version 5 and iC version 11.3.0, but it may be fixed in a later version of Powershell than what you are running.

     

    -Dean