Function KempAuthenticDuo {

    [CmdletBinding()]
    PARAM (
        [Parameter(Mandatory=$true)][String] $CRPCEN,
        [Parameter(Mandatory=$true)][String] $PortPROXY,
        [Parameter(Mandatory=$true)][String] $Principal,
        [Parameter(Mandatory=$true)][String] $Secondaire,
        [Parameter()][String] $Description = $null
    )

    #Import-Module Kemp.LoadBalancer.Powershell.psd1 -errorAction SilentlyContinue -Verbose

    $lm49 = ConvertTo-SecureString "F1ch0rg@PM$" -AsPlainText -Force
    $creds = New-Object System.Management.Automation.PSCredential("bal", $lm49)
							
    Initialize-LmConnectionParameters -Address 10.101.50.1 -LBPort 443 -Credential $creds -Verbose

    #Variable
    $ContentMatchingRuleName    = $CRPCEN+"_ui_Authentic"
    $ContentMatchingRulePattern = "/^\/$CRPCEN-ui-Authentic./" 
    $VirtualServiceName         = "10.80.20.2" 
    $VirtualServicePort         = 443
    $CheckedPort                = $Port
    $SubVSName                  = "$CRPCEN-ui-Authentic"
    $Url                        = "/$CRPCEN-ui-Authentic/kemp.jsp"
    $AllowedVirtualHosts        = "www.cloud-fichorga.fr"
    $AllowedVirtualDirectories  = "/$CRPCEN-ui-Authentic*"
    $SchedulingMethod           = "fixed"
    #$HTTPHeaderModifications 	= ""

    # Creation Rules & Checking > Content Rules
    New-AdcContentRule -RuleName $ContentMatchingRuleName -MatchType regex -Pattern $ContentMatchingRulePattern -NoCase 1
    
    # Create Sub Virtual Service 
    $NASVS = New-AdcSubVirtualService -VirtualService $VirtualServiceName -VSPort $VirtualServicePort -VSProtocol tcp 
    $NASVSIndex = ($NASVS.Data.VS.SubVS[-1]).VSIndex 

    Set-AdcSubVirtualService -SubVSIndex $NASVSIndex -CheckPort $PortPROXY -CheckType http -CheckUrl $Url -CheckUse1_1 0 -Nickname $SubVSName -VSType http -Weight 1000 -ESPEnabled $True -AllowedHosts $AllowedVirtualHosts -AllowedDirectories $AllowedVirtualDirectories -Schedule $SchedulingMethod -CheckUseGet 1 -AddVia 2 -StandByAddr 10.101.20.21 -StandByPort 8015
    
    New-AdcRealServer -RealServer $Principal -RealServerPort $PortPROXY -Enable $true -Forward nat -VSIndex $NASVSIndex -Weight 800
    New-AdcRealServer -RealServer $Secondaire -RealServerPort $PortPROXY -Enable $true -Forward nat -VSIndex $NASVSIndex -Weight 700

}

Function KempAuthenticQuatro {

    [CmdletBinding()]
    PARAM (
        [Parameter(Mandatory=$true)][String] $CRPCEN,
        [Parameter(Mandatory=$true)][String] $PortPROXY1,
        [Parameter(Mandatory=$true)][String] $PortPROXY2,
        [Parameter(Mandatory=$true)][String] $Principal,
        [Parameter(Mandatory=$true)][String] $Secondaire,
        [Parameter()][String] $Description = $null
    )

    #Import-Module Kemp.LoadBalancer.Powershell.psd1 -errorAction SilentlyContinue -Verbose

    $lm49 = ConvertTo-SecureString "F1ch0rg@PM$" -AsPlainText -Force
    $creds = New-Object System.Management.Automation.PSCredential("bal", $lm49)
							
    Initialize-LmConnectionParameters -Address 10.101.50.1 -LBPort 443 -Credential $creds -Verbose

    #Variable
    $ContentMatchingRuleName    = $CRPCEN+"_ui_Authentic"
    $ContentMatchingRulePattern = "/^\/$CRPCEN-ui-Authentic./" 
    $VirtualServiceName         = "10.80.20.2" 
    $VirtualServicePort         = 443
    $CheckedPort                = $Port
    $SubVSName                  = "$CRPCEN-ui-Authentic"
    $Url                        = "/$CRPCEN-ui-Authentic/kemp.jsp"
    $AllowedVirtualHosts        = "www.cloud-fichorga.fr"
    $AllowedVirtualDirectories  = "/$CRPCEN-ui-Authentic*"
    $SchedulingMethod           = "fixed"
    #$HTTPHeaderModifications 	= ""

    # Creation Rules & Checking > Content Rules
    New-AdcContentRule -RuleName $ContentMatchingRuleName -MatchType regex -Pattern $ContentMatchingRulePattern -NoCase 1
    
    # Create Sub Virtual Service 
    $NASVS = New-AdcSubVirtualService -VirtualService $VirtualServiceName -VSPort $VirtualServicePort -VSProtocol tcp 
    $NASVSIndex = ($NASVS.Data.VS.SubVS[-1]).VSIndex 

    Set-AdcSubVirtualService -SubVSIndex $NASVSIndex -CheckPort $PortPROXY1 -CheckType http -CheckUrl $Url -CheckUse1_1 0 -Nickname $SubVSName -VSType http -Weight 1000 -ESPEnabled $True -AllowedHosts $AllowedVirtualHosts -AllowedDirectories $AllowedVirtualDirectories -Schedule $SchedulingMethod -CheckUseGet 1 -AddVia 2 -StandByAddr 10.101.20.21 -StandByPort 8015
    
    New-AdcRealServer -RealServer $Principal -RealServerPort $PortPROXY1 -Enable $true -Forward nat -VSIndex $NASVSIndex -Weight 1000
    New-AdcRealServer -RealServer $Principal -RealServerPort $PortPROXY2 -Enable $true -Forward nat -VSIndex $NASVSIndex -Weight 800

    Start-Sleep -s 5
    
    New-AdcRealServer -RealServer $Secondaire -RealServerPort $PortPROXY1 -Enable $true -Forward nat -VSIndex $NASVSIndex -Weight 900
    New-AdcRealServer -RealServer $Secondaire -RealServerPort $PortPROXY2 -Enable $true -Forward nat -VSIndex $NASVSIndex -Weight 700

}

Import-Module Kemp.LoadBalancer.Powershell.psd1 -errorAction SilentlyContinue -Verbose

$lm49 = ConvertTo-SecureString "F1ch0rg@PM$" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential("bal", $lm49)
							
Initialize-LmConnectionParameters -Address 10.101.50.1 -LBPort 443 -Credential $creds -Verbose

Foreach($Line in Get-Content C:\tmp\CRPCEN.txt) {

    If ($Line -ne $null) {
        $Line | foreach { $Line = $_ -split ';'
        $CRPCEN             = $Line[0]
        $Serveur1           = $Line[1]
        $PortServeur1       = $Line[2]
        $PoidServeur1       = $Line[3]
        $Serveur2           = $Line[4]
        $PortServeur2       = $Line[5]
        $PoidServeur2       = $Line[6]
        $Serveur3           = $Line[7]
        $PortServeur3       = $Line[8]
        $PoidServeur3       = $Line[9]
        $Serveur4           = $Line[10]
        $PortServeur4       = $Line[11]
        $PoidServeur4       = $Line[12]
    } }

    write-host "$CRPCEN"

    If ($Serveur3  -ne '') {
        write-host "ici" $Serveur3

        If ( $PoidServeur1 -lt $PoidServeur2 ) {
            write-host $CRPCEN $PortServeur1 $PortServeur2 $Serveur3  $Serveur1
            KempAuthenticQuatro $CRPCEN $PortServeur1 $PortServeur2 $Serveur3  $Serveur1
        }
        Else {
            write-host $CRPCEN $PortServeur1 $PortServeur2 $Serveur1  $Serveur3
            KempAuthenticQuatro $CRPCEN $PortServeur1 $PortServeur2 $Serveur1  $Serveur3
        }

    }
    Else {
        write-host "la" $Serveur3

        If ( $PoidServeur1 -lt $PoidServeur2 ) {
            write-host $CRPCEN $PortServeur1 $Serveur2  $Serveur1
            KempAuthenticDuo $CRPCEN $PortServeur1 $Serveur2  $Serveur1
        }
        Else {
            write-host $CRPCEN $PortServeur1 $Serveur1  $Serveur2
            KempAuthenticDuo $CRPCEN $PortServeur1 $Serveur1  $Serveur2
        }
    }

    Start-Sleep -s 10

}