Authenticate to AKS Rest API with powershell

5/20/2021

Trying to query the AKS REST API to get a list of my clusters using Invoke-Restmethod. How do I authenticate?

I've created an app registration and gave it access on the cluster resource. How do I pass the client ID, secret etc to the cmdlet? Or is there a better method available?

Thanks in advance.

Sameer

-- Sameer Mhaisekar
azure-aks
kubernetes
powershell

1 Answer

5/25/2021

You can use the Azure PowerShell commands to list your clusters through AKS REST API, there is no need to use Invoke-Restmethod. Here is the example code:

$Credential = Get-Credential
Connect-AzAccount -Credential $Credential -Tenant 'xxxx-xxxx-xxxx-xxxx' -ServicePrincipal

Invoke-AzRestMethod -Path /subscriptions/b83c1ed3-c5b6-44fb-b5ba-2b83a074c23f/providers/Microsoft.ContainerService/managedClusters?api-version=2021-03-01 -Method GET

You can even use the Azure PowerShell command to get the list of the AKS cluster:

Get-AzAksCluster
-- Charles Xu
Source: StackOverflow