skip to Main Content

I have a number of instance running under my app service in Azure. I need to get back all the id’s of these instances.

I am currently using

Get-AzureRmResource -ResourceGroupName $ResourceGroupName -ResourceType 
Microsoft.Web/sites/instances -Name $WebAppName -ApiVersion 2016-03-01

But is there an equivalent command using the az cmdlets ?

2

Answers


  1. I’ve tried the exact PowerShell command that you executed in Azure PowerShell. I could be able to get the expected results as shown:

    enter image description here

    I’ve tried to get the desired outcome with AZ cmdlet(Bash) and received the expected results using:

    az webapp list-instances
    

    To get the instances running info with their respective ID’s on app service in Azure:

    az configure --defaults group=xxxxResourceGroupName
    az configure --defaults web=xxxxwebapp
    az webapp list-instances --name xxxxwebappp --resource-group xxxxResourceGroupName
    

    enter image description here

    If you want to retrieve "default hostname, EnabledHostName as well as state" of WebApp, Use below command as shown:

    enter image description here

    Refer MSDoc az.webapp cmdlets

    Login or Signup to reply.
  2. It is always recommended to use latest Az PowerShell module cmdlets instead of AzureRM since AzureRm is going to retire soon.

    In the above answer shared by @jahanvi replace Get-AzureRMResource with Get-AzResource which is a relevant Az module cmdlet.

    Alternatively, you can use the Azure Management RestAPI to get the list of instances running under a particular webapp using Invoke-RequestMethod you call the rest api from powershell as well.

    Here is the sample script:

    Connect-AzAccount
    $context=Set-AzContext -Subscription "<subscriptionId>"
    $token = Get-AzAccessToken
    $authHeader=@{
        'Content-Type'='application/json'
        'Authorization'='Bearer '+ $token.Token
    }
    $instances=Invoke-RestMethod -Uri https://management.azure.com/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Web/sites/<WebAppName>/instances?api-version=2022-03-01 -Method GET -Headers $authHeader
    $instances.value.id
    

    Here is the sample screenshot for reference:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search