Private/GetHttpRequestWithCookie.ps1
# 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=$True)] [System.TimeSpan]$requestTimeOut, [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 ARRAffinity, 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 = ".$defaultHostName"; $session.Cookies.Add($cookie); } $timeOutSeconds = $requestTimeOut.TotalSeconds; $httpResponse = Invoke-WebRequest -Uri $url -WebSession $session -TimeoutSec $timeOutSeconds; return $httpResponse; } |