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
$VirtualServiceName = "10.80.20.2" 
$VirtualServicePort = 443

$Liste = Get-AdcVirtualService -VirtualService $VirtualServiceName -VSPort $VirtualServicePort -VSProtocol tcp 

$VSIndex = $Liste.Data.VS.subVS.VSIndex

Foreach ($VSClient in $VSIndex) {
    $NASVS = Get-AdcSubVirtualService -SubVSIndex $VSClient

    $Temp = $NASVS.DATA.VS.NickName
    $IndexKemp = $NASVS.DATA.VS.index

    $Temp | Foreach { $Temp = $_ -split '-'
        $ValueCRPCEN = $Temp[0]
        $ValueApp    = $Temp[1]
        }

    #foreach ($Ligne Get-Content clients.csv) {
    Foreach($Ligne in [System.IO.File]::ReadLines("D:\Data\Administration\app-2.csv")) {
        $Ligne | Foreach { $Ligne = $_ -split ';'
            $KempCRPCEN     = $Ligne[0]
            $KempApp        = $Ligne[1]
            $KempPrincipal  = $Ligne[2]
            $KempSecondaire = $Ligne[3]
            $KempPort       = $Ligne[4]
            }

        If($KempCRPCEN -eq $ValueCRPCEN) {

            If($KempApp -eq $ValueApp) {

                #write-host $KempCRPCEN"/"$KempApp"/"$KempPrincipal"/"$KempSecondaire"/"$KempPort"/"$NASVS.DATA.VS.index

                If (Test-Connection $KempPrincipal -count 1 -Quiet) {
                    $ipV4Principal = Test-Connection "$KempPrincipal" -count 1 | select Ipv4Address

                    If ($ipV4Principal -ne $null) {
                        $ipV4Principal | foreach { $ipV4Principal = $_ -split '='
                            $ipV4PrincipalCLOUD = $ipV4Principal[1]
                            }

                        $ipV4PrincipalCLOUD = $ipV4PrincipalCLOUD -replace " ", ""
                        $ipV4PrincipalCLOUD = $ipV4PrincipalCLOUD -replace "}", ""
                    }
                    Else {
                        $ipV4Principal      = "0.0.0.0"
                        $ipV4PrincipalCLOUD = "0.0.0.0"
                    }
                    #write-host "$ipV4PrincipalCLOUD"
                }

                If (Test-Connection $KempSecondaire -count 1 -Quiet) {
                    $ipV4Secondaire = Test-Connection "$KempSecondaire" -count 1 | select Ipv4Address

                    If ($ipV4Secondaire -ne $null) {
                        $ipV4Secondaire | foreach { $ipV4Secondaire = $_ -split '='
                            $ipV4SecondaireCLOUD = $ipV4Secondaire[1]
                            }

                        $ipV4SecondaireCLOUD = $ipV4SecondaireCLOUD -replace " ", ""
                        $ipV4SecondaireCLOUD = $ipV4SecondaireCLOUD -replace "}", ""
                    }
                    Else {
                        $ipV4Secondaire      = "0.0.0.0"
                        $ipV4SecondaireCLOUD = "0.0.0.0"
                    }
                    #write-host "$ipV4SecondaireCLOUD"
                }
                write-host $KempCRPCEN"-"$KempApp":"$ipV4PrincipalCLOUD"/"$ipV4SecondaireCLOUD"-"$KempPort"-"$IndexKemp
                New-AdcRealServer -RealServer $ipV4PrincipalCLOUD -RealServerPort $KempPort -Enable $true -Forward nat -VSIndex $IndexKemp -Weight 800
                New-AdcRealServer -RealServer $ipV4SecondaireCLOUD -RealServerPort $KempPort -Enable $true -Forward nat -VSIndex $IndexKemp -Weight 700
                Start-Sleep -s 5
            }
            Else {

            }
        }
        Else {

        }
    }
}
