Functions/Connect-F5.psm1

function Connect-F5 {

    $SessionFile = $($env:temp+"\f5-session.xml")

    if(Test-Path($SessionFile)){
        $Session = Import-CliXml -Path $SessionFile

        $Headers = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
        $Headers.add('X-F5-Auth-Token',$Session.WebSession.Headers.'X-F5-Auth-Token')
        $Headers.add('Token-Expiration',$Session.WebSession.Headers.'Token-Expiration')
        $WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
        $WebSession.Headers = $Headers
        $Session.WebSession = $WebSession
        $Session = $Session | Add-Member -Name GetLink -MemberType ScriptMethod {
            param($Link)
            $Link -replace 'localhost', $this.Name
        } -PassThru 

        if($(Get-F5Status -F5Session $Session -ErrorAction SilentlyContinue) -eq "ACTIVE"){
            write-host -foregroundcolor white "Connected to active F5: $($Session.Name)"
            return $Session
        } else {
            write-host -foregroundcolor white "Not connected to an ACTIVE F5"
        }
    } else {
        write-host -foregroundcolor white "No F5 session file found"
    }

    $Cred = Set-F5CredFile
    $F5Names = Set-F5NamesFile


    $F5Names | %{
        write-host -nonewline "Checking state of $_..."
        $Session = New-F5Session -LTMName $_ -LTMCredentials $Cred -PassThru -ErrorAction SilentlyContinue
        if($Session.LTMVersion -eq '0.0.0.0'){
            write-host -foregroundcolor red "Failed"
            $Session = $null
        } else {
            $F5Status = Get-F5Status -F5Session $Session -ErrorAction SilentlyContinue
            if($F5Status -eq 'ACTIVE'){
                write-host -foregroundcolor green $F5Status
                $Session | Export-CliXml -Path $SessionFile
                write-host -foreground Magenta "Creating session on active F5: $($Session.Name)"
                return
            } else {
                write-host -foregroundcolor yellow $F5Status
            }
        }
    }
    if($Session -eq $null){
        write-host -foregroundcolor red "Cannot find active F5"
    }
    return $Session
}

Function Set-F5CredFile {
    $CredFile = $($env:temp+"\f5-cred.xml")
    write-host -nonewline "Importing credential..."
    $Cred = $null
    try{
        $Cred = Import-CliXml -Path $CredFile
        write-host -foregroundcolor green "OK ($($Cred.UserName))"
    }catch{
        write-host -foregroundcolor red "Not found"
        
    }
    if($Cred -eq $null){
        write-host -foregroundcolor cyan "Creating credential file..."
        $Cred = Get-Credential
        if($Cred -eq $null){
            write-host -foregroundcolor red "Failed to create credential file"
        } else {
            $Cred | Export-CliXml -Path $CredFile
            write-host -foregroundcolor green "Credential file created"
        }
    }
    return $Cred
}

Function Set-F5NamesFile {
    $F5NamesFile = $($env:temp+"\f5-names.xml")
    write-host -nonewline "Importing F5 names..."
    $F5Names = $null
    try{
        $F5Names = Import-CliXml -Path $F5NamesFile
        write-host -foregroundcolor green "OK"
    }catch{
        write-host -foregroundcolor red "Not found"
        
    }
    if($F5Names -eq $null){
        write-host -foregroundcolor cyan "Creating F5 names file..."
        $F5Names = @()
        do {
            $input = (Read-Host "Enter F5 names")
            if ($input -ne '') {$F5Names += $input}
        }
        until ($input -eq '')
        if($F5Names.count -ne 0){
            $F5Names | Export-CliXml -Path $F5NamesFile
            write-host -foregroundcolor green "F5 names file created"
        } else {
            write-host -foregroundcolor red "Failed to create F5 names file"
        }
    }
    return $F5Names
}