I tried to fetch details of resource owners created an year ago using powershell script but iam getting errors. Is it possible to create one?
This is the code I used:
foreach ($resource in $resources) {
$createdBy = $resource.Properties.createdBy
if ($createdBy -and $createdBy.userPrincipalName) {
$ownerDetails = Get-AzADUser -UserPrincipalName $createdBy.userPrincipalName
Write-Host "Resource: $($resource.Name), Created by: $($ownerDetails.DisplayName) - $($ownerDetails.UserPrincipalName)"
}
}
The PowerShell script you used is looking good to me to achieve your requirement. But it might not work as expected if the
creationdate/timedoesn't contain the proper information about the owner identity.Instead use
createdbyto retrieve the exact requirement as shown below.Efficient script:
Alternatively, you can also use the
Get-AzLogcommand to get the above needed information which is detailed below.