{"id":8739,"date":"2024-01-18T00:11:18","date_gmt":"2024-01-18T00:11:18","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/managing-azure-resources-using-powershell-cmdlet-invoke-azrestmethod-in-azure-rest-api\/"},"modified":"2025-01-20T10:32:29","modified_gmt":"2025-01-20T10:32:29","slug":"managing-azure-resources-using-powershell-cmdlet-invoke-azrestmethod-in-azure-rest-api","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/managing-azure-resources-using-powershell-cmdlet-invoke-azrestmethod-in-azure-rest-api\/","title":{"rendered":"Managing Azure Resources Using PowerShell cmdlet Invoke-AzRestMethod in Azure REST API"},"content":{"rendered":"<div>Sometimes, managing certain Azure resources using PowerShell can be challenging due to the absence of specific cmdlets for those operations or services. This is where the Invoke-AzRestMethod cmdlet comes into play, which allows PowerShell scripts to communicate with Azure services by sending HTTP requests to Azure&#8217;s REST API. It acts as a bridge between PowerShell and Azure services that still need to be integrated with cmdlets.<\/div>\n<h2>Using Invoke-AzRestMethod with a GET method<\/h2>\n<p>As an example, let\u2019s try to get the diagnostics settings of a management group using a GET method in our request. We have two options here.<\/p>\n<ol>\n<li>We can individually specify each parameter.<\/li>\n<li>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.<\/li>\n<\/ol>\n<p>Let\u2019s use option one.<\/p>\n<pre>((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 *<\/pre>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Using-a-GET-method-with-Invoke-AzRestMethod-to-pull-data-from-Azure.png\" target=\"_blank\" rel=\"nofollow noopener\">Using a GET method with Invoke-AzRestMethod to pull data from Azure<\/a><\/p>\n<p>And here is option two:<\/p>\n<pre><\/pre>\n<p>$params = @{<\/p>\n<p>method = &#8216;GET&#8217;<\/p>\n<p>path = &#8216;providers\/microsoft.management\/managementGroups\/{0}\/providers\/microsoft.insights\/diagnosticSettings?api-version=2020-01-01-preview&#8217; -f &#8216;IT&#8217;<\/p>\n<p>}<\/p>\n<p>((Invoke-AzRestMethod @params).content | convertfrom-json).value | fl *<\/p>\n<p>The result will be the same.<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Using-PowerShell-splatting-with-Invoke-AzRestMethod.png\" target=\"_blank\" rel=\"nofollow noopener\">Using PowerShell splatting with Invoke-AzRestMethod<\/a><\/p>\n<h2>Using Invoke-AzRestMethod with a POST method<\/h2>\n<p>We can gather information from Azure by employing an Azure Resource Graph query with the use of <em>Invoke-AzRestMethod<\/em>. 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.<\/p>\n<p>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 &#8220;payload&#8221;.<\/p>\n<pre><\/pre>\n<p>$query = @&#8217;<\/p>\n<p>{<\/p>\n<p>&#8220;query&#8221;: &#8220;resources<\/p>\n<p>| where type contains &#8216;virtualnetworks&#8217;<\/p>\n<p>| where resourceGroup == &#8216;devcenter&#8217;<\/p>\n<p>| join kind=inner (<\/p>\n<p>resourcecontainers<\/p>\n<p>| where type == &#8216;microsoft.resources\/subscriptions&#8217;<\/p>\n<p>| project subscriptionId, subscriptionName = name)<\/p>\n<p>on subscriptionId<\/p>\n<p>| where subscriptionName contains &#8216;2023&#8217;<\/p>\n<p>| project subscriptionName, resourceGroup, name, type&#8221;<\/p>\n<p>}<\/p>\n<p>&#8216;@<\/p>\n<p>$Params = @{<\/p>\n<p>path = &#8216;\/providers\/Microsoft.ResourceGraph\/resources?api-version=2022-10-01&#8217;<\/p>\n<p>Method = &#8216;POST&#8217;<\/p>\n<p>Payload = $($query)<\/p>\n<p>}<\/p>\n<p>Invoke-AzRestMethod @Params | select -ExpandProperty CONTENT | convertfrom-json | select -ExpandProperty data<\/p>\n<p>Using a POST method to send a query to Azure<\/p>\n<h2>Enabling features using a PUT method<\/h2>\n<p>Let&#8217;s examine how we can use the cmdlet with PUT methods.<\/p>\n<p>In certain scenarios, enabling a feature on a service using a PowerShell module is impossible. For such cases, the <em>Invoke-AzRestMethod<\/em> cmdlet is handy. By adding the necessary settings to the request&#8217;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.<\/p>\n<p>In the following example, we will enable diagnostics settings on a management group using the <em>Invoke-AzRestMethod <\/em>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.<\/p>\n<h2>Subscribe to 4sysops newsletter!<\/h2>\n<pre><\/pre>\n<p>$Body = [string](<\/p>\n<p>@{<\/p>\n<p>&#8216;properties&#8217; =@{<\/p>\n<p>&#8216;workspaceId&#8217; = &#8216;\/subscriptions\/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx\/resourceGroups\/loganalytics\/providers\/Microsoft.OperationalInsights\/workspaces\/la01&#8217;<\/p>\n<p>&#8216;logs&#8217; = [array](<\/p>\n<p>@{<\/p>\n<p>&#8216;category&#8217; = [string] &#8216;Administrative&#8217;<\/p>\n<p>&#8216;enabled&#8217; = [bool] $true<\/p>\n<p>},<\/p>\n<p>@{<\/p>\n<p>&#8216;category&#8217; = [string] &#8216;Policy&#8217;<\/p>\n<p>&#8216;enabled&#8217; = [bool] $false<\/p>\n<p>}<\/p>\n<p>)<\/p>\n<p>}<\/p>\n<p>} | ConvertTo-Json -Compress -Depth 3<\/p>\n<p>)<\/p>\n<p>$params = @{<\/p>\n<p>method = &#8216;PUT&#8217;<\/p>\n<p>path = &#8216;\/providers\/microsoft.management\/managementGroups\/{0}\/providers\/microsoft.insights\/diagnosticSettings\/{1}?api-version=2020-01-01-preview&#8217; -f &#8216;IT2&#8242;,&#8217;setting01&#8217;<\/p>\n<p>payload = $body<\/p>\n<p>}<\/p>\n<p>Invoke-AzRestMethod @params<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, managing certain Azure resources using PowerShell can be challenging due to the absence of specific cmdlets for those operations or services. This is where the Invoke-AzRestMethod cmdlet comes into play, which allows PowerShell scripts to communicate with Azure services by sending HTTP requests to Azure&#8217;s REST API. It acts as a bridge between PowerShell [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8740,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,107,126,117],"tags":[],"class_list":["post-8739","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-azure","category-cloud-computing","category-powershell"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/8739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=8739"}],"version-history":[{"count":3,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/8739\/revisions"}],"predecessor-version":[{"id":10387,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/8739\/revisions\/10387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/8740"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=8739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=8739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=8739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}