10

Intent: Know when a resource group was created for the first time. The client organization wants to report and act on resource group creation timestamps. This will be used in automation scripts.

Unfortunately there is no creation timestamp property on resource groups. Using Get-AzureRmResourceGroup returns objects like this:

ResourceGroupName : eastus2-something-rg
Location          : eastus2
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/eastus2-something-rg

How do I retrieve the creation timestamp for a resource group?

rcabr
  • 1,298
  • 10
  • 20
  • Dead URL needs to be removed or updated: https://feedback.azure.com/forums/223579-azure-portal/suggestions/32424859-creation-date-as-a-first-order-property-for-all-re – gigalul Jan 31 '23 at 15:46
  • Removed the dead URL. – rcabr Jan 31 '23 at 19:40

4 Answers4

5

Indeed, resource groups don't have a creation timestamp.

But management operations are recorded in logs, and these logs can be retrieved with the Get-AzureRmLog command.

Here is a PowerShell statement that goes through a subscription's resource groups and finds those that were created n or more days ago (from this gist):

$days = 7
$pointInTime = [DateTime]::Now.AddDays(-$days);
$horizon = $pointInTime.AddDays(-$days);

"===Removing resource groups created between $horizon and $pointInTime==="

# Get potential log entries
$logs = @()
$logs += Get-AzureRmLog -StartTime $horizon -EndTime $pointInTime -Status "Succeeded" -ResourceProvider "Microsoft.Resources" -WarningAction "SilentlyContinue" `
    | Select-Object ResourceGroupName, ResourceId, @{Name="EventNameValue"; Expression={$_.EventName.Value}}, @{Name="OperationNameValue"; Expression={$_.OperationName.Value}}, EventTimestamp, @{Name="HttpVerb"; Expression={$_.HttpRequest.Method}} `
    | Where-Object -FilterScript {$_.EventNameValue -EQ "EndRequest" -and $_.OperationNameValue -eq "Microsoft.Resources/subscriptions/resourcegroups/write" -and $_.HttpVerb -eq "PUT"} `
    | Select-Object -ExpandProperty ResourceGroupName -Unique

"Expired resource groups (created BEFORE $pointInTime) -> $logs"

# Get recent log entries to remove from the list
$nologs = @()
$nologs += Get-AzureRmLog -StartTime $pointInTime -Status "Succeeded" -ResourceProvider "Microsoft.Resources" -WarningAction "SilentlyContinue" `
| Select-Object ResourceGroupName, ResourceId, @{Name="EventNameValue"; Expression={$_.EventName.Value}}, @{Name="OperationNameValue"; Expression={$_.OperationName.Value}}, EventTimestamp, @{Name="HttpVerb"; Expression={$_.HttpRequest.Method}} `
| Where-Object -FilterScript {$_.EventNameValue -EQ "EndRequest" -and $_.OperationNameValue -eq "Microsoft.Resources/subscriptions/resourcegroups/write" -and $_.HttpVerb -eq "PUT"} `
| Select-Object -ExpandProperty ResourceGroupName -Unique

"Resource groups created AFTER $pointInTime -> $nologs"

# remove any that were found to have recent creation
$rgs = $logs | Where-Object {$nologs -notcontains $_} | Select-Object @{Name="ResourceGroupName"; Expression={$_}} | Get-AzureRmResourceGroup -ErrorAction "SilentlyContinue"

"Existing resource groups to delete -> $($rgs | Select-Object -ExpandProperty ResourceGroupName)"

$rgs | Remove-AzureRmResourceGroup -Force -AsJob

It returns a list of the jobs that are running to delete the resource groups (they can take some time depending on their contents).

rcabr
  • 1,298
  • 10
  • 20
  • 1
    The Azure audit logs contain data only for 90 days. Is there any way to fetch data (like RG created time) for RG created sometime last year?? – Manjunath Rao Jun 04 '18 at 14:45
  • You could query the deployments. RGs are not recorded in the deployment log, but you could use the earliest resource deployed to the RG, maybe. Also, deployments can be deleted, so you risk that as well. – rcabr Jun 04 '18 at 16:38
  • I appreciate the solution but it is hacky and makes a lot of assumptions that aren't obvious or inherently true. – gigalul Jan 31 '23 at 15:48
  • @gigalul Indeed. And the Powershell modules in this answer from 4.5 years ago are now deprecated. – rcabr Jan 31 '23 at 19:45
3

get creation date time of Azure Resource Group using below PowerShell cmdlets (AzRestMethod)

$subId = (Get-AzContext).Subscription.ID
((Invoke-AzRestMethod -Path "/subscriptions/$subId/resourcegroups?api-version=2020-06-01&`$expand=createdTime" -Method GET).Content|ConvertFrom-Json).value|select name,createdTime
VIJAY RAAVI
  • 388
  • 3
  • 11
2

This information is available via ARM, but you have to call the API directly rather than the PS Get-AzureRmResourceGroup (or Get-AzResourceGroup) cmdlets.

See Deleting all resources in an Azure Resource Group with age more than x days

Essentially, you need to add the to the $expand=createdTime to your query parameters, ie.:

GET https://management.azure.com/subscriptions/1237f4d2-3dce-4b96-ad95-677f764e7123/resourcegroups?api-version=2019-08-01&%24expand=createdTime
kwill
  • 10,867
  • 1
  • 28
  • 26
0

You can use az group deployment list -g [RESOURCE_GROUP_NAME] and then parse out the oldest "timestamp" value using any method you'd like.

Note that as of the time of this writing, az group deployment is implicitly deprecated and will be replaced by az deployment group.

gigalul
  • 416
  • 3
  • 13