Public/Add-CitrixStoreFront.ps1

function Add-CitrixStoreFront {
    <#
.SYNOPSIS
    Adds a Citrix StoreFront Load Balancing configuration to your Citrix ADC.
.DESCRIPTION
    Adds a Citrix StoreFront 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 Licensing GSLB configuration.
.NOTES
    Creation Date: 20/06/2018
.CHANGE CONTROL
    Name Version Date Change Detail
    David Brett 1.0 20/06/2018 Function Creation
.EXAMPLE
    Add-CitrixStoreFront -JSONFile C:\CitrixADC\CitrixADC-Build.json

    Add-CitrixStoreFront -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
        $ServicePrefix = $json.global.ServicePrefix
        $vServerPrefix = $json.global.vServerPrefix

        # 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 Load Balancing Configuration to the Citrix ADC"

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

        # Setup the Redirect URL
        if ($true -eq $json.StoreFront.redirect) {
            $RedirectURL = "/Citrix/" + $json.StoreFront.StoreName + "Web"
            Add-ADCResponderAction -ResponderActionName "res_act_storefront" -ResponderActionType "redirect" -ResponderActionTarget """$RedirectURL"""
            Add-ADCResponderPolicy -ResponderPolicyName "res_pol_storefront" -ResponderActionName "res_act_storefront" -ResponderPolicyRule "HTTP.REQ.URL.PATH.EQ(""/"")"
        }

        # Add Citrix StoreFront servers to ADC
        foreach ($Server in $json.storefront.servers) {
            Add-ADCServer -ServerName $server.dnsname -ServerIP $server.ipaddress
        }

        # Check if you want the load balancer to be secure and act accordingly
        if ($true -eq $json.StoreFront.Secure) {
            Add-ADCCertificateFile -CertificateFile $json.StoreFront.CertFile -CertificateFileName $json.StoreFront.CertFileDesc
            Add-ADCCertificateFile -CertificateFile $json.StoreFront.KeyFile -CertificateFileName $json.StoreFront.KeyFileDesc
            Set-ADCCertificateKey -CertificateFile $json.StoreFront.CertFileDesc -KeyFile $json.StoreFront.KeyFileDesc -CertificateKeyName $json.StoreFront.CertKeyPairDesc
            Add-ADCCustomStoreFrontMonitor -MonitorName $MonitorPrefix'citrix_storefront_443' -StoreName $json.StoreFront.StoreName -Secure
            Add-ADCServiceGroup -ServiceGroupName $ServicePrefix'grp_citrix_storefront_443' -ServiceGroupType "SSL" -StoreFront
            Set-ADCMonitorToServiceGroup -ServiceGroupName $ServicePrefix'grp_citrix_storefront_443' -MonitorName $MonitorPrefix'citrix_storefront_443'
            foreach ($Server in $json.storefront.servers) {
                Add-ADCServiceGroupMember -ServiceGroupName $ServicePrefix'grp_citrix_storefront_443' -ServerName $server.dnsname -ServiceGroupPort 443
            }
            Add-ADCLoadBalancingvServer -vServerName $vServerPrefix'citrix_storefront_443' -vServerType "SSL" -vServerIPv4 $json.StoreFront.StoreFrontServerVirtualServerIP -vServerPort 443
            Set-ADCServiceGroupTovServer -vServerName $vServerPrefix'citrix_storefront_443' -ServiceGroupName $ServicePrefix'grp_citrix_storefront_443'
            if ($true -eq $json.StoreFront.redirect) {
                Set-ADCResponderPolicyTovServer -vServerName $vServerPrefix'citrix_storefront_443' -ResponderPolicyName "res_pol_storefront" -Priority 100 -BindPoint "REQUEST"
            }
            Set-ADCCertificateTovServer -vServerName $vServerPrefix'citrix_storefront_443' -CertificateKeyName $json.StoreFront.CertKeyPairDesc
            if ("both" -eq $json.storefront.type) {
                Add-ADCCustomStoreFrontMonitor -MonitorName $MonitorPrefix'citrix_storefront_80' -StoreName $json.StoreFront.StoreName 
                Add-ADCServiceGroup -ServiceGroupName $ServicePrefix'grp_citrix_storefront_80' -ServiceGroupType "HTTP" -StoreFront
                Set-ADCMonitorToServiceGroup -ServiceGroupName $ServicePrefix'grp_citrix_storefront_80' -MonitorName $MonitorPrefix'citrix_storefront_80'
                foreach ($Server in $json.storefront.servers) {
                    Add-ADCServiceGroupMember -ServiceGroupName $ServicePrefix'grp_citrix_storefront_80' -ServerName $server.dnsname -ServiceGroupPort 80
                }
                Add-ADCLoadBalancingvServer -vServerName $vServerPrefix'citrix_storefront_80' -vServerType "HTTP" -vServerIPv4 $json.StoreFront.StoreFrontServerVirtualServerIP -vServerPort 80
                Set-ADCServiceGroupTovServer -vServerName $vServerPrefix'citrix_storefront_80' -ServiceGroupName $ServicePrefix'grp_citrix_storefront_80'
                if ($true -eq $json.StoreFront.redirect) {
                    Set-ADCResponderPolicyTovServer -vServerName $vServerPrefix'citrix_storefront_80' -ResponderPolicyName "res_pol_storefront" -Priority 100 -BindPoint "REQUEST"
                }
            }
            else {
                Add-ADCServer -ServerName "localhost" -ServerIP "127.0.0.1"
                Add-ADCService -ServiceName "svc_always_up" -ServiceType "TCP" -ServicePort 80 -ServerName "localhost"
                Add-ADCCustomTCPMonitor -MonitorName "mon_always_up" -MonitorPort 80
                Set-ADCMonitorToService -ServiceName "svc_always_up" -MonitorName "mon_always_up"
                Add-ADCLoadBalancingvServer -vServerName $vServerPrefix'citrix_storefront_80' -vServerType "HTTP" -vServerIPv4 $json.StoreFront.StoreFrontServerVirtualServerIP -vServerPort 80
                Set-ADCServiceTovServer -vServerName $vServerPrefix'citrix_storefront_80' -ServiceName "svc_always_up"
                Add-ADCResponderAction -ResponderActionName "res_act_http_to_https" -ResponderActionType "redirect" -ResponderActionTarget """https://"" + HTTP.REQ.HOSTNAME.HTTP_URL_SAFE + HTTP.REQ.URL.PATH_AND_QUERY.HTTP_URL_SAFE"
                Add-ADCResponderPolicy -ResponderPolicyName "res_pol_http_to_https" -ResponderActionName "res_act_http_to_https" -ResponderPolicyRule "HTTP.REQ.IS_VALID"
                Set-ADCResponderPolicyTovServer -vServerName $vServerPrefix'citrix_storefront_80' -ResponderPolicyName "res_pol_http_to_https" -Priority 100 -BindPoint "REQUEST"
            }
        }
        else {
            Add-ADCCustomStoreFrontMonitor -MonitorName $MonitorPrefix'citrix_storefront_80' -StoreName $json.StoreFront.StoreName 
            Add-ADCServiceGroup -ServiceGroupName $ServicePrefix'grp_citrix_storefront_80' -ServiceGroupType "HTTP" -StoreFront
            Set-ADCMonitorToServiceGroup -ServiceGroupName $ServicePrefix'grp_citrix_storefront_80' -MonitorName $MonitorPrefix'citrix_storefront_80'
            foreach ($Server in $json.storefront.servers) {
                Add-ADCServiceGroupMember -ServiceGroupName $ServicePrefix'grp_citrix_storefront_80' -ServerName $server.dnsname -ServiceGroupPort 80
            }
            Add-ADCLoadBalancingvServer -vServerName $vServerPrefix'citrix_storefront_80' -vServerType "HTTP" -vServerIPv4 $json.StoreFront.StoreFrontServerVirtualServerIP -vServerPort 80
            Set-ADCServiceGroupTovServer -vServerName $vServerPrefix'citrix_storefront_80' -ServiceGroupName $ServicePrefix'grp_citrix_storefront_80'
            if ($true -eq $json.StoreFront.redirect) {
                Set-ADCResponderPolicyTovServer -vServerName $vServerPrefix'citrix_storefront_80' -ResponderPolicyName "res_pol_storefront" -Priority 100 -BindPoint "REQUEST"
            }
        }

        # Disconnect from the Citrix ADC
        Disconnect-CitrixADC
    
        Write-Verbose "Finished adding Citrix StoreFront Load Balancing Configuration to the Citrix ADC"
    }

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

}