Listing Resources under Resource Group with Azure PowerShell

Simple task:  I want to list resources under a single ResourceGroup within one of the subscription.

This is an excuse to show how to login on different subscription and play a bit with the PowerShell Azure SDK.

First thing, start PowerShell Integrated Script Environment (ISE) with the Azure SDK loaded.

Login

Login to your accounts…

Login-AzureRmAccount

This will prompt you to enter an ID + credentials.  Once you enter them, you should be in your subscription.

…  if you have more than one subscription?  Ok, that’s a little more complicated.

You need to find the subscription ID you are interested in.  Either go to https://account.windowsazure.com/Subscriptions to find it in the UI or…  use more PowerShell scripts:

Get-AzureRmSubscription

Once you have your subscription ID, simply grab the ID and pass it to:

Add-AzureRmAccount SubscriptionId <your subscription ID here>

This should prompt you again and afterwards, you’ll be in the context of the right subscription.

List resources under resource group

Now that you’re within the right subscription, let’s list the resource groups within that subscription:

Get-AzureRmResourceGroup

This will give you the list of resources under that resource group.  Grab the resource group name and then you can list the resources underneath:

$res = Get-AzureRmResource | WhereObject {$_.ResourceGroupName eq <You resource group name>}

Conclusion

That’s it!  I just wanted to get this ceremony out of the way.

You can build on that and use all different kind of cmdlets to query and manipulate your subscriptions.


11 responses

  1. techno 2017-07-10 at 11:23

    how to export all resources under different resurce group

  2. Vincent-Philippe Lauzon 2017-07-10 at 11:53

    Have a look at https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-move-resources

  3. Anonymous 2017-11-27 at 02:48

    hi

  4. Prashant Babber 2018-01-16 at 14:33

    Try this- $groups = Get-AzureRmResourceGroup -Name
    foreach ($g in $groups) {Find-AzureRmResource -ResourceGroupNameEquals $g.ResourceGroupName | ForEach- Object {Get-AzureRmResource}}

  5. Anonymous 2018-02-12 at 23:13

    I want a list to get the list of resources along with the created by user details using power-shell.

    Possible to find?.

  6. Vincent-Philippe Lauzon 2018-02-13 at 03:08

    This is kind of possible but not trivial. You see, “Created By” isn’t a property of the resource objects.

    The way to correlate the two would be to access the Activity Logs (see https://vincentlauzon.com/2016/11/27/primer-on-azure-monitor/) find the log entry for the resource creation and then you’ll find who did the REST action. From there you could query AAD to get details on that user (see https://vincentlauzon.com/2017/02/02/automating-azure-ad/).

    On top of that, activity logs are available only for the last 30 days unless you redirect them to some other storage.

    So, not impossible, but not as simple as joining two tables in a db ;)

    Now, if that is important to your organization to, you could run a job every day to scan newly created resources and TAG those resources with some meta data about the creating user. Then it would be much easier to query the “created by” user, assuming nobody go and mess with the tags.

  7. Jordan P 2018-06-18 at 13:53

    @Vincent-Philippe Lauzon do you have a script to scan resources and tag them with certain values within Azure?

  8. peeblesjordan 2018-06-18 at 13:54

    Vincent, do you have a script that will scan resources such as VM for a certain attribute then tag them appropriately within Azure?

  9. Vincent-Philippe Lauzon 2018-06-19 at 12:34

    You can scan through resources in multiple ways. The most generic is to use Get-AzureRmResource.

    You can also update the tags of a resource using its update cmdlet. E.g. https://docs.microsoft.com/en-us/powershell/module/azurerm.compute/update-azurermvm?view=azurermps-6.3.0 for VMs.

  10. Anonymous 2018-09-25 at 10:04

    How might you list all resource groups that contain zero resources?

  11. Vincent-Philippe Lauzon 2018-09-25 at 10:10

    You would need to use a where-clause and check the content of the resource group (see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=powershell-6).

    That will likely be slow since you’ll need to check the number of resources (API call) for each resource group.

    Alternatively, you can list all resources (with their resource group) and grab only the resource group name. Then compare that with the list of resource groups. You should find the ones that weren’t attached to any resources.

Leave a comment