Functions/Set-F5Node.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
function Set-F5Node {

    <#
        .SYNOPSIS
            Interactive function to put pools members down or up
        .EXAMPLE
            Set-F5Node node1 -Up -Sync
        .EXAMPLE
            Set-F5Node -Sync
        .LINK
            https://github.com/jorioux/F5-LTM-Helper
    #>


    param(
        [Parameter(Mandatory = $false,ValueFromPipeline = $true)]
        [string]$Name,
        [switch]$Up,
        [switch]$Down,
        [string]$Pool,
        [switch]$Sync,
        [switch]$Force
    )

    if($VerbosePreference -ne "SilentlyContinue"){
        $Verbose = $true
    } else {
        $Verbose = $false
    }

    if($Up -and $Down){
        Write-Warning "Specify either Up or Down, not both"
        return
    }

    $Session = Connect-F5 -Verbose:$Verbose

    if($Session -eq $null){
        return
    }

    $Pools = Get-Pool -F5Session $Session | Where-Object {$_.fullPath -like "*$Pool*"} | Select-Object -ExpandProperty fullPath

    $Pools | ForEach-Object {
        $PoolName = $_
        $MemberName = ""
        $Confirm = $false
        write-host -foregroundcolor white "`nPool: $PoolName"
        Get-PoolMember -PoolName $PoolName -F5Session $Session | Where-Object {$_.name -like "*$($Name)*"} | ForEach-Object {
                $MemberName = $_.name
                write-host -NoNewLine "`tCurrent status for $MemberName : "
                if($_.state -eq 'up'){
                    write-host -NoNewLine -foregroundcolor green $($_.state)
                } else {
                    write-host -NoNewLine -foregroundcolor red $($_.state)
                }
                $curconns = (Get-PoolMemberStats -PoolName $PoolName -Name $MemberName -F5Session $Session)."serverside.curConns".value
                write-host " ($curconns connections)"
        }

        if($MemberName -eq ""){
            write-host -foregroundcolor yellow "`tNo match for '$Name'"
        } else {

            #Enable Pool Member
            if($Up -and ($Force -or $($Confirm = read-host "`tENABLE $MemberName ? (y/n) "; $Confirm) -eq "y")){
                write-host -NoNewLine -foregroundcolor cyan "`tEnabling $MemberName..."
                $output = Enable-PoolMember -PoolName $PoolName -Name $MemberName -F5Session $Session

            #Disable Pool Member
            } elseif($Down -and ($Force -or $($Confirm = read-host "`tDISABLE $MemberName ? (y/n) "; $Confirm) -eq "y")){
                write-host -NoNewLine -foregroundcolor cyan "`tDisabling $MemberName..."
                $output = Disable-PoolMember -PoolName $PoolName -Name $MemberName -Force -F5Session $Session
            }

            if(($Up -or $Down) -and ($Force -or $Confirm -eq 'y')) {
                sleep 1
                if($output -eq 'True'){
                    write-host -foregroundcolor green "OK"
                } else {
                    write-host -foregroundcolor red "Fail"
                }
                
                write-host -NoNewLine "`tNew status for $MemberName : "
                $NewState = $((Get-PoolMember -Name $MemberName -PoolName $PoolName -F5Session $Session).State)
                write-host -ForegroundColor $(if($NewState -eq 'up'){"Green"}else{"Red"}) $NewState
            }
        }
    }

    #Sync Device to Group
    if($Sync -and ($Force -or $($Confirm = read-host "`nSYNC $($Session.Name) to group ? (y/n) "; $Confirm) -eq "y") -and $(Get-F5Status -F5Session $Session) -eq 'ACTIVE'){
        write-host -NoNewLine -foregroundcolor white "Syncing device to group..."
        $output = Sync-DeviceToGroup -GroupName syncgroup -F5Session $Session
        if($output -eq 'True'){
            write-host -foregroundcolor green "OK"
        } else {
            write-host -foregroundcolor red "FAIL"
        }
    }

}