Using Invoke-AzRestMethod with a GET method
As an example, let’s try to get the diagnostics settings of a management group using a GET method in our request. We have two options here.
- We can individually specify each parameter.
- We can use PowerShell splatting, a method where we create a collection of parameters and pass them to the cmdlet as the parameter set. Doing so can streamline the process and make commands in your scripts easier to read.
Let’s use option one.
((Invoke-AzRestMethod -Method 'GET' -Path ( 'providers/microsoft.management/managementGroups/{0}/providers/microsoft.insights/diagnosticSettings?api-version=2020-01-01-preview' -f 'IT')).content | convertfrom-json).value | fl *
Using a GET method with Invoke-AzRestMethod to pull data from Azure
And here is option two:
$params = @{
method = 'GET'
path = 'providers/microsoft.management/managementGroups/{0}/providers/microsoft.insights/diagnosticSettings?api-version=2020-01-01-preview' -f 'IT'
}
((Invoke-AzRestMethod @params).content | convertfrom-json).value | fl *
The result will be the same.
Using PowerShell splatting with Invoke-AzRestMethod
Using Invoke-AzRestMethod with a POST method
We can gather information from Azure by employing an Azure Resource Graph query with the use of Invoke-AzRestMethod. To deliver our query to the Azure Resource Graph Service, we require the POST method to provide Azure with the said query. The Services processes it and brings back the results as expected.
We start with creating a Resource Graph Query. In the following demonstration, the query is intended to retrieve all resources within a certain resource group of a subscription. We can include the query in our request by placing it in the “payload”.
$query = @'
{
"query": "resources
| where type contains 'virtualnetworks'
| where resourceGroup == 'devcenter'
| join kind=inner (
resourcecontainers
| where type == 'microsoft.resources/subscriptions'
| project subscriptionId, subscriptionName = name)
on subscriptionId
| where subscriptionName contains '2023'
| project subscriptionName, resourceGroup, name, type"
}
'@
$Params = @{
path = '/providers/Microsoft.ResourceGraph/resources?api-version=2022-10-01'
Method = 'POST'
Payload = $($query)
}
Invoke-AzRestMethod @Params | select -ExpandProperty CONTENT | convertfrom-json | select -ExpandProperty data
Using a POST method to send a query to Azure
Enabling features using a PUT method
Let’s examine how we can use the cmdlet with PUT methods.
In certain scenarios, enabling a feature on a service using a PowerShell module is impossible. For such cases, the Invoke-AzRestMethod cmdlet is handy. By adding the necessary settings to the request’s payload and using the PUT method, we can send the request to the ARM endpoint for the desired settings to be applied to the Azure service.
In the following example, we will enable diagnostics settings on a management group using the Invoke-AzRestMethod cmdlet. As we need to specify values for the new settings on the management group, we will use a PUT method. As a result, the request must include a BODY in the payload, which will contain all the required settings in the request. These settings will then be applied to the service when enabling diagnostics settings on the management group.
Subscribe to 4sysops newsletter!
$Body = [string](
@{
'properties' =@{
'workspaceId' = '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx/resourceGroups/loganalytics/providers/Microsoft.OperationalInsights/workspaces/la01'
'logs' = [array](
@{
'category' = [string] 'Administrative'
'enabled' = [bool] $true
},
@{
'category' = [string] 'Policy'
'enabled' = [bool] $false
}
)
}
} | ConvertTo-Json -Compress -Depth 3
)
$params = @{
method = 'PUT'
path = '/providers/microsoft.management/managementGroups/{0}/providers/microsoft.insights/diagnosticSettings/{1}?api-version=2020-01-01-preview' -f 'IT2','setting01'
payload = $body
}
Invoke-AzRestMethod @params