Quickstart: Run your first Resource Graph query using .NET
Note
Special thanks to Glenn Block for contributing the code used in this quickstart.
The first step to using Azure Resource Graph is to check that the required NuGet packages are installed. This quickstart walks you through the process of adding the packages to your .NET application.
At the end of this process, you'll have added the packages to your .NET application and run your first Resource Graph query.
Prerequisites
- .NET SDK 6.0 or later
- An Azure subscription. If you don't have an Azure subscription, create a free account before you begin.
- An Azure service principal, including the clientId and clientSecret. If you don't have a service principal for use with Resource Graph or want to create a new one, see Azure management libraries for .NET authentication. Skip the step to install the NuGet packages, as we'll do that in the next steps.
Create the Resource Graph project
To enable .NET to query Azure Resource Graph, create a new console application and install the required packages.
Create a new .NET console application named "argQuery":
dotnet new console --name "argQuery"
Change directories into the new project folder. Install the packages for the Azure Resource Graph and Azure Identity client libraries:
dotnet add package Azure.ResourceManager.ResourceGraph dotnet add package Azure.Identity
Replace the default
Program.cs
with the following code and save the updated file:using Azure.Identity; using Azure.ResourceManager; using Azure.ResourceManager.ResourceGraph; using Azure.ResourceManager.ResourceGraph.Models; string strTenant = args[0]; string strClientId = args[1]; string strClientSecret = args[2]; string strQuery = args[3]; var client = new ArmClient( new ClientSecretCredential(strTenant, strClientId, strClientSecret)); var tenant = client.GetTenants().First(); //Console.WriteLine($"{tenant.Id} {tenant.HasData}"); var queryContent = new ResourceQueryContent(strQuery); var response = tenant.GetResources(queryContent); var result = response.Value; Console.WriteLine($"Count: {result.Data.ToString()}");
Note
This code creates a tenant-based query. To limit the query to a management group or subscription, set the
ManagementGroups
orSubscriptions
property on theQueryRequest
object.Build and publish the
argQuery
console application:dotnet build dotnet publish -o {run-folder}
Run your first Resource Graph query
With the .NET console application built and published, it's time to try out a simple tenant-based Resource Graph query. The query returns the first five Azure resources with the Name and Resource Type of each resource.
In each call to argQuery
, replace the variables with your own values:
{tenantId}
- Replace with your tenant ID{clientId}
- Replace with the client ID of your service principal{clientSecret}
- Replace with the client secret of your service principal
Change directories to the
{run-folder}
you defined with the earlierdotnet publish
command.Run your first Azure Resource Graph query using the compiled .NET console application:
argQuery "{tenantId}" "{clientId}" "{clientSecret}" "Resources | project name, type | limit 5"
Note
As this query example does not provide a sort modifier such as
order by
, running this query many times is likely to yield a different set of resources per request.Change the final parameter to
argQuery.exe
and change the query toorder by
the Name property:argQuery "{tenantId}" "{clientId}" "{clientSecret}" "Resources | project name, type | limit 5 | order by name asc"
Note
Just as with the first query, running this query multiple times is likely to yield a different set of resources per request. The order of the query commands is important. In this example, the
order by
comes after thelimit
. This command order first limits the query results and then orders them.Change the final parameter to
argQuery.exe
and change the query to firstorder by
the Name property and thenlimit
to the top five results:argQuery "{tenantId}" "{clientId}" "{clientSecret}" "Resources | project name, type | order by name asc | limit 5"
When the final query is run several times, assuming that nothing in your environment is changing, the results returned are consistent and ordered by the Name property, but still limited to the top five results.
Clean up resources
If you wish to remove the .NET console application and installed packages, you can do so by
deleting the argQuery
project folder.
Next steps
In this quickstart, you've created a .NET console application with the required Resource Graph packages and run your first query. To learn more about the Resource Graph language, continue to the query language details page.
Feedback
Submit and view feedback for