cancel
Showing results for 
Search instead for 
Did you mean: 
dan_03_140075
Nimbostratus
Nimbostratus

Problem this snippet solves:

Need iControl snapin for powershell installed Taken from "Ps Virtual Server Lookup" on F5 DevCentral and Paul Westlake's "Multi Layered Menu Demonstration". You have a FQDN and want to find the the Virtual Server that serves it, also the pools and their members. Crude'ish but it works. Think the interesting bits are the authentication and extra info pulled about the virtual server. Wish we could De-Initialize-F5.iControl

To take it for a burn provide your F5 name to the $global:$g_bigip variables e.g. 1{ $global:g_bigip = "dev1.net"; Gen-Menu3; break} #Actual network name or ip for F5 line 47 in this script (also 48 & 66 & 109, etc)

Then run the script e.g .\fqdn_query.ps1 and navigate to the device. E.G. to run against device specificied in line 47: Menu1 select Dev > Menu2 select dev1 > Menu3 select Query VS from FQDN > Enter valid FQDN for this F5 at "Enter FQDN"

Code :

$MainTitle    = "F5 Load Balancers"
$cred = (get-credential)
$global:success = $null
$global:g_bigip = $null
$global:g_hostname = $null
$global:Env = $null
$global:g_virtualaddr  = $null

function LoadMenuSystem(){
[INT]$global:xMenu1=0
[INT]$global:xMenu2=0
[INT]$global:xMenu3=0

while ( $global:xMenu1 -lt 1 -or $global:xMenu1 -gt 5 ){
 CLS
 #… Present the Menu Options
 Write-Host “`n`t$MainTitle`n” -ForegroundColor Magenta
 Write-Host “`t`tPlease select the environment you require`n” -Fore Cyan
 Write-Host “`t`t`t1. Dev” -Fore Cyan
 Write-Host “`t`t`t2. SIT” -Fore Cyan
 Write-Host “`t`t`t3. UAT” -Fore Cyan
 Write-Host “`t`t`t4. PROD/DR” -Fore Cyan
 Write-Host “`t`t`t5. Quit and exit`n” -Fore Cyan
 #… Retrieve the response from the user
 [int]$global:xMenu1 = Read-Host “`t`tEnter Menu Option Number”
 if( $global:xMenu1 -lt 1 -or $global:xMenu1 -gt 5 ){
  Write-Host “`tPlease select one of the options available.`n” -Fore Red;start-Sleep -Seconds 1
 }
}

Switch ($global:xMenu1){    #… User has selected a valid entry.. load next menu
1 { $global:Env = "DEV"
        while ( $global:xMenu2 -lt 1 -or $global:xMenu2 -gt 3 ){
        CLS
        # Present the Menu Options
        Write-Host “`n`t$MainTitle > $global:Env`n” -Fore Magenta
        Write-Host “`t`tPlease select the load balancer you require`n” -Fore Green
        Write-Host “`t`t`t1. dev1” -Fore Green
        Write-Host “`t`t`t2. dev2” -Fore Green
        Write-Host “`t`t`t3. Go to Main Menu`n” -Fore Green
        [int]$global:xMenu2 = Read-Host “`t`tEnter Menu Option Number”  
        if( $global:xMenu2 -lt 1 -or $global:xMenu2 -gt 3 ){
                Write-Host “`tPlease select one of the options available.`n” -Fore Red;start-Sleep -Seconds 1
         }
        }
        Switch ($global:xMenu2){
         1{ $global:g_bigip = "dev1.net"; Gen-Menu3; break} #Actual network name or ip for F5
         2{ $global:g_bigip = "dev2.net"; Gen-Menu3; break}
         3{ LoadMenuSystem; break}
         default { break}
    }
 }
2 { $global:Env = "SIT"
        while ( $global:xMenu2 -lt 1 -or $global:xMenu2 -gt 2 ){
        CLS
        Write-Host “`n`t$MainTitle > $global:Env`n” -Fore Magenta
        Write-Host “`t`tPlease select the load balancer you require`n” -Fore Green
        Write-Host “`t`t`t1. sit1” -Fore Green
        Write-Host “`t`t`t2. Go to Main Menu`n” -Fore Green
        [int]$global:xMenu2 = Read-Host “`t`tEnter Menu Option Number”
        if( $global:xMenu2 -lt 1 -or $global:xMenu2 -gt 2 ){
         Write-Host “`tPlease select one of the options available.`n” -Fore Red;start-Sleep -Seconds 1
         }
        }
        Switch ($global:xMenu2){
         1{ $global:g_bigip = "sit1.net"; Gen-Menu3; break}
         2{ LoadMenuSystem; break}
         default {break}
        }
   }
3 { $global:Env = "UAT"
        while ( $global:xMenu2 -lt 1 -or $global:xMenu2 -gt 3 ){
        CLS
        Write-Host “`n`t$MainTitle > $global:Env`n” -Fore Magenta
        Write-Host “`t`tPlease select the load balancer you require`n” -Fore Green
        Write-Host “`t`t`t1. uat1” -Fore Green
        Write-Host “`t`t`t2. uat2” -Fore Green
        Write-Host “`t`t`t3. Go to Main Menu`n” -Fore Green
        [int]$global:xMenu2 = Read-Host “`t`tEnter Menu Option Number”
        if( $global:xMenu2 -lt 1 -or $global:xMenu2 -gt 3 ){
         Write-Host “`tPlease select one of the options available.`n” -Fore Red;start-Sleep -Seconds 1
        }
        }
        Switch ($global:xMenu2){
         1{ $global:g_bigip = "uat1.net"; Gen-Menu3; break}
         2{ $global:g_bigip = "uat2.net"; Gen-Menu3; break}
         3{ LoadMenuSystem; break}
         default {break}
        }
   }
4 { $global:Env = "PROD/DR"
        while ( $global:xMenu2 -lt 1 -or $global:xMenu2 -gt 7 ){
        CLS
        Write-Host “`n`t$MainTitle > $global:Env`n” -Fore Magenta
        Write-Host “`t`tPlease select the load balancer you require`n” -Fore Green
        Write-Host “`t`t`t1. prod1” -Fore Green
        Write-Host “`t`t`t2. prod2.net” -Fore Green
        Write-Host “`t`t`t3. dr1.net” -Fore Green
        Write-Host “`t`t`t4. pub3.net” -Fore Green
        Write-Host “`t`t`t5. pub4.net” -Fore Green
        Write-Host “`t`t`t6. dr2.net” -Fore Green
        Write-Host “`t`t`t7. Go to Main Menu`n” -Fore Green
        [int]$global:xMenu2 = Read-Host “`t`tEnter Menu Option Number”
        if( $global:xMenu1 -lt 1 -or $global:xMenu1 -gt 7 ){
        Write-Host “`tPlease select one of the options available.`n” -Fore Red;start-Sleep -Seconds 1
        }
        }
        Switch ($global:xMenu2){
         1{ $global:g_bigip = "prod1.net"; Gen-Menu3; break}
         2{ $global:g_bigip = "prod2.net"; Gen-Menu3; break}
         3{ $global:g_bigip = "dr1.net"; Gen-Menu3; break}
         4{ $global:g_bigip = "pub1.net"; Gen-Menu3; break}
         5{ $global:g_bigip = "pub2.net"; Gen-Menu3; break}
         6{ $global:g_bigip = "pub1-dr.net"; Gen-Menu3; break}
         7{ LoadMenuSystem; break}
         default {break}
        }
   }
5 { Return }
default { Return }
}
LoadMenuSystem;
}

#-------------------------------------------------------------------------
# function Write-VSEntry
#-------------------------------------------------------------------------
function Write-VSEntry()
{
  param(
    [string]$vs = $null,
    [string]$address = $null,
    [int]$port = $null
  );
  if ( $vs -and $address -and $port )
  {
    "{0,16}:{1,-5} -> {2}" -f ${address}, ${port}, $vs;
        Get-F5.LTMVirtualServer -virtualserver "$vs" | format-list
        $vspool = (Get-F5.iControl).LocalLBVirtualServer.get_default_pool_name(("$vs"));
        Get-F5.LTMPoolMember -pool "$vspool"
        Invoke-Pause
  }
}

#-------------------------------------------------------------------------
# function Menu3
#-------------------------------------------------------------------------
function Gen-Menu3()
{
        while ( $global:xMenu3 -lt 1 -or $global:xMenu3 -gt 2 ){
        CLS
        Write-Host “`n`t$MainTitle > $global:Env > $global:g_bigip`n” -Fore Magenta
        Write-Host “`t`tPlease select the action you require`n” -Fore Cyan
        Write-Host “`t`t`t1. Query VS from FQDN” -Fore Green
        Write-Host “`t`t`t2. Go to Main Menu`n” -Fore Green
        [int]$global:xMenu3 = Read-Host “`t`tEnter Menu Option Number”
        if( $global:xMenu3 -lt 1 -or $global:xMenu3 -gt 2 ){
                Write-Host “`tPlease select one of the options available.`n” -Fore Red;start-Sleep -Seconds 1
                }
        }
        Switch ($global:xMenu3){
                                1{ Vsfqdn; break}
                                2{ LoadMenuSystem; break}
                                default {break}
                        }
}

#-------------------------------------------------------------------------
# function VSFQDN
#-------------------------------------------------------------------------
function Vsfqdn()
{
                $global:g_hostname = Read-Host “`t`tEnter FQDN” 
                $global:g_virtualaddr = [System.Net.Dns]::GetHostEntry($global:g_hostname).AddressList[0].IPAddressToString
                If($global:g_virtualaddr -match "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"){
                        if ( $global:g_virtualaddr ){
                        Do-Initialize
                        if ($global:success) {
                                                Lookup-Virtual $global:g_virtualaddr
                                                }
                        }
                }
                else{
                        Write-Host "ERROR: No DNS IP address found"
                        Invoke-Pause
                        }
}

#-------------------------------------------------------------------------
# Lookup-Virtual
#-------------------------------------------------------------------------
function Lookup-Virtual()
{
  param([string]$query = $null);

  $addr = "*";
  $port = "*";
  $found = $null;

  if ( $query )
  {
    $addr = $query;
    $tokens = $query.Split(':');
    if ( $tokens.Length -eq 2 )
    {
      $addr = $tokens[0];
      $port = $tokens[1];
    }
    if ( !$addr ) { $addr = "*"; }
    if ( !$port ) { $port = "*"; }
  }

  $vs_list = (Get-F5.iControl).LocalLBVirtualServer.get_list();
  $dest_list = (Get-F5.iControl).LocalLBVirtualServer.get_destination($vs_list);

  for($i=0; $i-lt$dest_list.Length; $i++)
  {
    $vs = $vs_list[$i];
    $daddr = $dest_list[$i].address;
    $dport = $dest_list[$i].port;

    if ( !($query) -or ("${daddr}:${dport}" -like "${addr}:${port}") )
    {
          $found = 1
      Write-VSEntry -vs $vs -address $daddr -port $dport;
    }
   }
   if ($found -eq $null) {
                Write-Host "No virtual server found for $global:g_hostname on $global:g_bigip"
                Invoke-Pause
                 }
}

#-------------------------------------------------------------------------
# Pause - press any key to continue
#-------------------------------------------------------------------------

Function Invoke-Pause() {
Write-Host "Press any key to continue..."
$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer()
}

#-------------------------------------------------------------------------
# Load iControl snapin
#-------------------------------------------------------------------------
function Do-iControl()
{
  if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null )
  {
    Add-PSSnapIn iControlSnapIn
  }
}

#-------------------------------------------------------------------------
# Do-Initialize
#-------------------------------------------------------------------------
function Do-Initialize()
{
  $global:success = Initialize-F5.iControl -HostName $global:g_bigip -credentials $cred;
  if (!($global:success)) {
                $answer = Read-Host “Could not login to $global:g_bigip Try again? (y/n):”
                if ($answer -eq "y") { 
                                                                $cred = (get-credential)
                                                                Do-Initialize
                                                          }
                                        }
}

#-------------------------------------------------------------------------
# Main Application Logic
#-------------------------------------------------------------------------
Do-iControl
LoadMenuSystem
Version history
Last update:
‎09-Mar-2015 11:35
Updated by:
Contributors