Public/Add-CitrixStoreFrontGSLB.ps1

function Add-CitrixStoreFrontGSLB {
    <#
.SYNOPSIS
    Adds a Citrix StoreFront Global Server Load Balancing configuration to your Citrix ADC.
.DESCRIPTION
    Adds a Citrix StoreFront Global Server Load Balancing configuration to your Citrix ADC.
    The configuration data for this function is driven by the "StoreFront" section of the JSON file passed in.
.PARAMETER JSONFile
    The JSON file containing the data to be used to configure the Citrix StoreFront GSLB configuration.
.NOTES
    Creation Date: 29/03/2018
.CHANGE CONTROL
    Name Version Date Change Detail
    David Brett 1.0 04/07/2018 Function Creation
.EXAMPLE
    Add-CitrixStoreFrontGSLB -JSONFile C:\CitrixADC\CitrixADC-Build.json

    Add-CitrixStoreFrontGSLB -JSONFile C:\CitrixADC\CitrixADC-Build.json -Verbose
#>


    [CmdletBinding()]
    Param (
        [parameter(Mandatory = $false, ValueFromPipeline = $true)][string[]]$JSONFile
    )

    begin {
        $StartTime = (Get-Date)
        if (test-path $JSONfile) {
            try {
                $JSON = Get-Content -Raw -Path $JSONfile | ConvertFrom-Json -ErrorAction Stop
            }
            catch {
                throw "Error reading JSON. Please Check File and try again."
            }
        }

        # Read in data from the JSON File
        $MonitorPrefix = $JSON.global.MonitorPrefix
        $GSLBLocalServicename = $JSON.StoreFront.gslb.GSLBLocalServiceName
        $GSLBLocalvServerName = $JSON.StoreFront.gslb.GSLBLocalvServerName
        $GSLBRemoteServicename = $JSON.StoreFront.gslb.GSLBRemoteServiceName
        $GSLBRemotevServerName = $JSON.StoreFront.gslb.GSLBRemotevServerName
        $GSLBPrimaryVIP = $JSON.StoreFront.gslb.GSLBPrimaryVIP
        $GSLBRemoteVIP = $JSON.StoreFront.gslb.GSLBRemoteVIP

        # Generate Citrix ADC Credentials
        $SecurePassword = ConvertTo-SecureString $JSON.Global.ADCPassword -AsPlainText -Force
        $ADCCredentials = New-Object System.Management.Automation.PSCredential ($JSON.Global.ADCUserName, $SecurePassword)
    }

    process {
        Write-Verbose "Starting to add Citrix StoreFront GSLB Configuration to the Citrix ADC"

        # Connect to the Citrix ADC
        Connect-CitrixADC -IPAddress $JSON.Global.ADCIP -Credential $ADCCredentials

        # Add the local and remote Citrix StoreFront servers
        Add-ADCServer -ServerName $GSLBPrimaryVIP -ServerIP $JSON.StoreFront.StoreFrontServerVirtualServerIP
        Add-ADCServer -ServerName $GSLBRemoteVIP -ServerIP $JSON.StoreFront.gslb.RemoteStoreFrontServerVirtualServerIP

        # Add the 2 GSLB services
        if ($true -eq $json.StoreFront.Secure) {
            Add-ADCGSLBService -ServiceName $GSLBLocalServicename -ServerName $GSLBPrimaryVIP -SiteName $JSON.global.GSLBLocalSite -Port 443 -ServiceType "SSL"
            Add-ADCGSLBService -ServiceName $GSLBRemoteServicename -ServerName $GSLBRemoteVIP -SiteName $JSON.global.GSLBRemoteSite -Port 443 -ServiceType "SSL"
            Set-ADCMonitorToGSLBService -GSLBServiceName $GSLBLocalServicename -MonitorName $MonitorPrefix'citrix_storefront_443'
            Set-ADCMonitorToGSLBService -GSLBServiceName $GSLBRemoteServicename -MonitorName $MonitorPrefix'citrix_storefront_443'
            if ("aa" -eq $json.StoreFront.gslb.type) {
                Add-ADCGSLBvServer -vServerName $GSLBLocalvServerName -ServiceType "SSL"
                Set-ADCGSLBServiceTovServer -vServerName $GSLBLocalvServerName -ServiceName $GSLBLocalServicename
                Set-ADCGSLBServiceTovServer -vServerName $GSLBLocalvServerName -ServiceName $GSLBRemoteServicename
                Add-ADCGSLBDomainTovServer -vServerName $GSLBLocalvServerName -DomainName $json.StoreFront.gslb.GSLBDomainName
            } else {
                Add-ADCGSLBvServer -vServerName $GSLBLocalvServerName -ServiceType "SSL"
                Add-ADCGSLBvServer -vServerName $GSLBRemotevServerName -ServiceType "SSL"
                Set-ADCGSLBServiceTovServer -vServerName $GSLBLocalvServerName -ServiceName $GSLBLocalServicename
                Set-ADCGSLBServiceTovServer -vServerName $GSLBRemotevServerName -ServiceName $GSLBRemoteServicename
                Add-ADCGSLBDomainTovServer -vServerName $GSLBLocalvServerName -DomainName $json.StoreFront.gslb.GSLBDomainName
                Set-ADCGSLBvServerBackup -vServerName $GSLBLocalvServerName -BackupvServerName $GSLBRemotevServerName
            }
        } else {
            Add-ADCGSLBService -ServiceName $GSLBLocalServicename -ServerName $GSLBPrimaryVIP -SiteName $JSON.global.GSLBLocalSite -Port 80 -ServiceType "HTTP"
            Add-ADCGSLBService -ServiceName $GSLBRemoteServicename -ServerName $GSLBRemoteVIP -SiteName $JSON.global.GSLBRemoteSite -Port 80 -ServiceType "HTTP"
            Set-ADCMonitorToGSLBService -GSLBServiceName $GSLBLocalServicename -MonitorName $MonitorPrefix'citrix_storefront_80'
            Set-ADCMonitorToGSLBService -GSLBServiceName $GSLBRemoteServicename -MonitorName $MonitorPrefix'citrix_storefront_80'
            if ("aa" -eq $json.StoreFront.gslb.type) {
                Add-ADCGSLBvServer -vServerName $GSLBLocalvServerName -ServiceType "HTTP"
                Set-ADCGSLBServiceTovServer -vServerName $GSLBLocalvServerName -ServiceName $GSLBLocalServicename
                Set-ADCGSLBServiceTovServer -vServerName $GSLBLocalvServerName -ServiceName $GSLBRemoteServicename
                Add-ADCGSLBDomainTovServer -vServerName $GSLBLocalvServerName -DomainName $json.StoreFront.gslb.GSLBDomainName
            } else {
                Add-ADCGSLBvServer -vServerName $GSLBLocalvServerName -ServiceType "HTTP"
                Add-ADCGSLBvServer -vServerName $GSLBRemotevServerName -ServiceType "HTTP"
                Set-ADCGSLBServiceTovServer -vServerName $GSLBLocalvServerName -ServiceName $GSLBLocalServicename
                Set-ADCGSLBServiceTovServer -vServerName $GSLBRemotevServerName -ServiceName $GSLBRemoteServicename
                Add-ADCGSLBDomainTovServer -vServerName $GSLBLocalvServerName -DomainName $json.StoreFront.gslb.GSLBDomainName
                Set-ADCGSLBvServerBackup -vServerName $GSLBLocalvServerName -BackupvServerName $GSLBRemotevServerName
            }
        }

        # Disconnect from the Citrix ADC
        Disconnect-CitrixADC

        Write-Verbose "Finished adding Citrix Licensing GSLB Configuration to the Citrix ADC"
    }

    end {
        $EndTime = (Get-Date)
        Write-Verbose "Add-CitrixLicensingGSLB finished."
        Write-Verbose "Elapsed Time: $(($EndTime-$StartTime).TotalMinutes) Minutes"
        Write-Verbose "Elapsed Time: $(($EndTime-$StartTime).TotalSeconds) Seconds"
    }
    
}