Filtering a list of VMs using the Azure SDK for Node.js with multiple-attributes

This recently came up as a question with a partner that I’m working with: how to apply multiple filters to a node.js request of VMs.  The partner is essentially trying to manage their resources in Azure and attempting to enumerate all VMs within a subscription based on certain criteria.

First things first, the goal was to use the Azure SDK for Node.js. Next decision point was to determine which module to use. The Compute module seemed like a good choice since you can use the list function to get back a list of VMs. Limitation there is that the only filter that can be applied is Resource Group. Next choice was the list all function, limitation there is that no filters can be applied.

Taking a step back from the Compute module, we can utilize the more generic Resource Module. The Resource Module also has the list function and has the option to pass in filters.

Next idea was to get a list of VMs based on tags. In this example, a single tag and the tag value are used to filter:

client.resources.list({ ‘filter’:

“tagname eq ‘alias’ and tagvalue eq ‘srashid'” }, function (err, vms) {

And what do we get back? A list of all resources within the subscription with a tag name of “alias” and tag value of “srashid”. But, we got back all resources, not just VMs. We’ve got to add another filter, so let’s add a resource type filter filtering on just VMs:

client.resources.list({ ‘filter’:

“tagname eq ‘alias’ and tagvalue eq ‘srashid’ and resourceType eq ‘Microsoft.Compute/virtualMachines'” }, function (err, vms) {

Nope, we get an error. resourceType eq ‘Microsoft.Compute/virtualMachines’ works by itself but when combined with tagname and/or tagvalue, the query fails.

We’ve got multiple filters applied, now need to narrow on just VM resource type.  Working on that next…

Comments (0)

Skip to main content