Private/GetHttpRequestWithCookie.ps1

# Todo make param:
$request_timeout_warmup = 60;

# Make a get request to a $url while forcing the instance using the ARRAfinity cookie with $instanceGuid
function GetHttpRequestWithCookie(
    [Parameter(Mandatory=$True)]
    [string]$defaultHostName,
    [Parameter(Mandatory=$True)]
    [string]$url,
    [Parameter(Mandatory=$False)]
    [string]$instanceGuid) {

    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession;

    if (![string]::IsNullOrEmpty($instanceGuid)){
        $cookie = New-Object System.Net.Cookie;

        # To force the instance you can set a cookie named ARRAfinity, this is what Azure uses to assign an Instance by ID
        # The domain name has to be the domain of the url calling the instance preceded with a '.'
        $cookie.Name = "ARRAffinity";
        $cookie.Value = $instanceGuid;
        $cookie.Domain = ".{0}" -f $defaultHostName;
        $session.Cookies.Add($cookie);
    }

    $httpResponse = Invoke-WebRequest -Uri $url -WebSession $session -TimeoutSec $request_timeout_warmup;
    return $httpResponse;
}