Search code examples
azure-web-app-serviceinvoke-webrequest

Invoke-WebRequest on all Azure app service instances


I want to invoke list of urls for all azure app service instances

Invoke-WebRequest -UseBasicParsing -uri $url -Method Get

with this command it will not work for multiple web app instances

I tried following script it runs and shows recourse id for all instance and website status 200 OK

  $webSiteInstances = Get-AzureRmResource -ResourceGroupName $rg -ResourceType 

  Microsoft.Web/sites/instances -Name $webapp -ApiVersion 2016-03-01

 foreach ($instance in $webSiteInstances)
 {
 $instanceId = $instance.Name

"Going to enumerate all processes on {0} instance" -f $instanceId

$processList =  Get-AzureRmResource -ApiVersion 2016-03-01 -ResourceId $resourceID

foreach ($process in $processList){
    Invoke-WebRequest -UseBasicParsing -uri $url -Method Get
}

}

question is I am not sure if it actually running for each instance if not how can I do that ?


Solution

  • To request the specific instance of your web app, you need to add a cookie in your requests to aim at a specific instance by setting the ARRAffinity cookie with the instance name, see here.

    So your script should be like below, it works fine on my side.

    $groupname = "xxxxxx"
    $webappname = "joyweba"
    $url = "https://joyweba.azurewebsites.net"
    
    $webSiteInstances = Get-AzureRmResource -ResourceGroupName $groupname -ResourceType Microsoft.Web/sites/instances -Name $webappname -ApiVersion 2016-03-01
    
    foreach ($instance in $webSiteInstances)
    {
        $instanceId = $instance.Name
    
        "Going to enumerate all processes on {0} instance" -f $instanceId
    
        $s = New-Object Microsoft.PowerShell.Commands.WebRequestSession
        $c = New-Object System.Net.Cookie('ARRAffinity',$instanceId,'/',"$webappname.azurewebsites.net")
        $s.Cookies.Add($c)
    
        Invoke-WebRequest -UseBasicParsing -Method Get -Uri $url -WebSession $s
    }
    

    enter image description here

    There are three instances in my web app, if you catch the requests via fiddler, it is more clear.

    enter image description here