Using PowerShell or the command line to call an Azure REST API is a quick method to retrieve or update information about a specific resource in Azure. Although Postman can also be used for this purpose, here is an example of how to make these requests using PowerShell.
Sign in
First, log in to your Azure account:
Connect-AzAccountSet the subscription context if you have multiple subscriptions:
Set-AzContext -Subscription "<SubscriptionId>"Get a token and build the headers
# Get the current token
$Token = (Get-AzAccessToken).Token# Set the authorization header
$Headers = @{
Authorization = "Bearer $Token"
}Build the request URL
Define which resource you want to query. In this example, I want to get the properties of a storage account in a resource group in my subscription.
To get the API URL and properties, use the REST API reference documentation .
Construct the API URL by substituting the subscription ID, resource group, and storage account name with proper values:
$Uri = "https://management.azure.com/subscriptions/{SubscriptionId}/resourceGroups/{ResourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}?api-version=2023-01-01"Call the API
Use Invoke-WebRequest to make the call:
Invoke-WebRequest -Method GET -UseBasicParsing -Uri $Uri -Headers $HeadersThe JSON content of the response can be accessed with:
(Invoke-WebRequest -Method GET -Uri $Uri -Headers $Headers).ContentFull script
Connect-AzAccount
Set-AzContext -Subscription "<SubscriptionId>"
# Get the current token
$Token = (Get-AzAccessToken).Token
# Set the authorization header
$Headers = @{
Authorization = "Bearer $Token"
}
$Uri = "https://management.azure.com/subscriptions/{SubscriptionId}/resourceGroups/{ResourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}?api-version=2023-01-01"
$Result = Invoke-WebRequest -Method GET -Uri $Uri -Headers $Headers
if ($Result.StatusCode -eq "200") {
$Result.Content
}