cancel
Showing results for 
Search instead for 
Did you mean: 

Powershell New-F5Session and close session issues.

LanceLyons
Altocumulus
Altocumulus

Hi,   We have some automation around some F5 functionality.  We have moved from 1 F5 to 2 F5s,  one for external apps and one for internal apps.   In our powershell automation we now have to create two different F5 sessions depending on which F5 we want to make changes in.  I have a snippet of the PS code below.


Is there any way to close the session that was opened and then create a new session?

We are finding the creating a New-F5session when one has already been created doesnt work and still uses the first session opened. 

Can I have 2 sessions and use the  Add-iRuleToVirtualServer   with some sessionID?


 

 

$ltmip = "1.2.3.4"      
$ltmip_inside = "1.3.3.4"            



         if ($vipName.ToLower() -contains "internal") {
             $session = New-F5Session -LTMName $ltmip_inside -LTMCredentials $mycreds
         } else {
             $session = New-F5Session -LTMName $ltmip -LTMCredentials $mycreds          
          }
.
.
.

      $add = Add-iRuleToVirtualServer -InputObject $vipObject -iRuleName $all_MaintRule

 

 

 
9 REPLIES 9

JRahm
Community Manager
Community Manager

Hi @LanceLyons, this issue on Joel's github repo for this might provide some insight to the issue you're facing.

Hi JRahm,   Thanks.  I did also post over there.  Headed over there to see if there was any response.

I posted in this area  https://github.com/joel74/POSH-LTM-Rest/issues  not sure if that was the right one but it seemed to have the most activity.

thanks

Lance

JRahm
Community Manager
Community Manager

Did you try the passthru switch Joel mentioned in the issue I shared? Seems to be the same scenario you mentioned, but posh is not my jam so I'm just throwing ideas out.

Yeah,  I am able to get it to work for 1 session but I am not sure how to do 2 sessions since we have 2 F5s that we have to change iRules on.     It seems like the  New-F5Session is part of the scripts attributes and so the calls to Add-iRuleToVirtualServer assumes the "current F5 session" that is open so passthru might be the default.   Joel may know of an alternative so I will have to wait and see.

I can always create another script that handles iRules for the other F5 but it involves a little more work and 2 separate scripts and commands to be created and executed.

JRahm
Community Manager
Community Manager

Looking through some comments on the codeshare, i found this:

 

$F5Session = New-F5session -LTMName $host_address -LTMCredentials $mycreds -PassThrough; 
Get-Pool -F5Session $F5Session | Select-Object -ExpandProperty fullPath;

 

So maybe if you had that in a loop and just passed in $host_address from the iterable it would update? No worries if you've tried this, still slinging mud against the wall in the event something sticks... 🙂

Hi Jason,  

I have left a post over in this area for Joel but have not heard back  https://github.com/joel74/POSH-LTM-Rest/issues/219

I have tried the code below and I still get this error thinking its coming from the Get-VirtualServer calls

You must either create an F5 Session with script scope (by calling New-F5Session with -passthrough parameter) or pass an F5 session to this function.
$session_inside = New-F5Session -LTMName $ltmip_inside -LTMCredentials $mycreds
$vipObject_inside = Get-VirtualServer -F5Session $session_inside -Name $vipName
               
$session_external = New-F5Session -LTMName $ltmip -LTMCredentials $mycreds 
$vipObject_external = Get-VirtualServer -F5Session $session_external -Name $vipName    
                
if ($vipName.ToLower() -contains "internal") {
   $vipObject = $vipObject_inside
} else {
   $vipObject = $vipObject_external
}
                
foreach ($viprule in $vipObject.rules) {
   switch ($action.tolower()) {
     "add" {
         switch ($type.tolower()) {
            "all" {
                if ($vipName.ToLower() -contains "internal") { 
                  $add = Add-iRuleToVirtualServer -F5Session $session_inside  -InputObject $vipObject -iRuleName $all_MaintRule
                } else {
                  $add = Add-iRuleToVirtualServer -F5Session $session_external -InputObject $vipObject -iRuleName $all_MaintRule
                }
                $status = "Updated"
             }
             "external" {
                if ($vipName.ToLower() -contains "internal") {
                  $add = Add-iRuleToVirtualServer -F5Session $session_inside  -InputObject $vipObject -iRuleName $external_MaintRule
                } else {
                  $add = Add-iRuleToVirtualServer -F5Session $session_external -InputObject $vipObject -iRuleName $external_MaintRule
              }
              $status = "Updated"
           }
             default {
               $status = "Error - rule type not specified"
             }
          } 

         }
      "remove" {
          if ($vipName.ToLower() -contains "internal") {
             $remove = Remove-iRuleFromVirtualServer -F5Session $session_inside -InputObject $vipObject -iRuleName $all_MaintRule
             $remove = Remove-iRuleFromVirtualServer -F5Session $session_inside -InputObject $vipObject -iRuleName $external_MaintRule
          } else {
             $remove = Remove-iRuleFromVirtualServer -F5Session $session_external -InputObject $vipObject -iRuleName $all_MaintRule
             $remove = Remove-iRuleFromVirtualServer -F5Session $session_external -InputObject $vipObject -iRuleName $external_MaintRule
          }

          $status = "Updated"
         }
          default {
            $status = "Error - action type not specfied"
          }
       }
    }

 

 

 

Also tried directly in Powershell session and the same error.

trying to pass the F5 sessiontrying to pass the F5 session

Hi Jason,

I was able to get this going with the session variable.  It looks like if you want to use a session variable for the F5 session then you do have to use  -PassThrough

$F5Session = New-F5Session -LTMName $ltmip_inside -LTMCredentials $mycreds -PassThrough
$vipObject = Get-VirtualServer -F5Session $F5Session -Name $vipName
 
$add = Add-iRuleToVirtualServer -F5Session $F5Session  -InputObject $vipObject -iRuleName $all_MaintRule
Thanks for being persistent with that message.

JRahm
Community Manager
Community Manager

glad it worked out! Like I said, I'm throwing mud at the wall and hoped something stuck. Need to re-engage with posh, it's been years and I wasn't that strong when I was writing it.