Aug 24 2024 08:29 PM
Hello:
I wonder if there is a way to find out ALL Resources in our tenant that were created since specific date (like last 7 days).
Ideally I'd like to run it via PowerShell...
How can I do it?
We are not enforcing "create date" tags... I thought about Powershell, but sometimes there are CreateDate, sometimes CreateAt...
Thank you!
Aug 25 2024 10:47 AM
Solution
Try this:
Connect-AzAccount
$startDate = (Get-Date).AddDays(-7) # Change this to your desired start date
$endDate = Get-Date
$activityLogs = Get-AzActivityLog -StartTime $startDate -EndTime $endDate -MaxRecord 1000 | Where-Object { $_.OperationName -like "Microsoft.Resources/subscriptions/resourceGroups/deployments/write" }
$createdResources = @()
foreach ($log in $activityLogs) {
$resource = @{
ResourceId = $log.ResourceId
ResourceName = $log.ResourceId.Split('/')[-1]
ResourceType = $log.ResourceType
ResourceGroupName = $log.ResourceGroupName
SubscriptionId = $log.SubscriptionId
CreatedTime = $log.EventTimestamp
}
$createdResources += New-Object PSObject -Property $resource
}
# Display the results
$createdResources | Format-Table -AutoSize
Aug 25 2024 05:45 PM
@Kidd_Ip ,
Thank you very much!
Such a great idea to use logs...
I'll try tomorrow and let you know.
Thank you!
Aug 26 2024 12:26 PM
Aug 25 2024 10:47 AM
Solution
Try this:
Connect-AzAccount
$startDate = (Get-Date).AddDays(-7) # Change this to your desired start date
$endDate = Get-Date
$activityLogs = Get-AzActivityLog -StartTime $startDate -EndTime $endDate -MaxRecord 1000 | Where-Object { $_.OperationName -like "Microsoft.Resources/subscriptions/resourceGroups/deployments/write" }
$createdResources = @()
foreach ($log in $activityLogs) {
$resource = @{
ResourceId = $log.ResourceId
ResourceName = $log.ResourceId.Split('/')[-1]
ResourceType = $log.ResourceType
ResourceGroupName = $log.ResourceGroupName
SubscriptionId = $log.SubscriptionId
CreatedTime = $log.EventTimestamp
}
$createdResources += New-Object PSObject -Property $resource
}
# Display the results
$createdResources | Format-Table -AutoSize