#Connect-AzAccount

class ResourceCheck {
    [string] $ResourceId = ""
    [string] $Id = ""
    [string] $Kind = ""
    [string] $Location = ""
    [string] $ResourceName = ""
    [string] $ResourceGroupName = ""
    [string] $ResourceType = ""
    [string] $SubscriptionId = ""
    [string] $SubscriptionName = ""
    [string] $Tag_Team = ""
    [string] $Tag_Product = ""
    [string] $Tag_Environment = ""
    [string] $Tag_Data = ""
    [string] $Tag_Delete = ""
    [string] $Tag_Split = ""
    [string] $Tag_CreatedOnDate = ""
    [string] $Tag_Deployment = ""
}

Write-Host "========================================================================================================================================================================"
Write-Host "Creating resource overview."
Write-Host "========================================================================================================================================================================"

$subscriptions = Get-AzSubscription | Where-Object State -eq "Enabled"

$fileName =  "c:\temp\2023-06-12 azure_resources.csv" 
#rm $fileName

foreach ($subscription in $subscriptions) 
{
Set-AzContext -SubscriptionId $subscription.Id

    $allResources = Get-AzResource 
    [ResourceCheck[]]$Result = @()
    
    foreach ($resource in $allResources) {

        [ResourceCheck] $resourceCheck = [ResourceCheck]::new()
        $resourceCheck.ResourceId = $resource.ResourceId
        $resourceCheck.Id = $resource.Id
        $resourceCheck.Kind = $resource.Kind
        $resourceCheck.Location = $resource.Location
        $resourceCheck.ResourceName = $resource.ResourceName
        $resourceCheck.ResourceGroupName = $resource.ResourceGroupName
        $resourceCheck.ResourceType = $resource.ResourceType
        $resourceCheck.SubscriptionId = $subscription.Id
        $resourceCheck.SubscriptionName = $subscription.Name
        $resourceCheck.Tag_Team = $resource.Tags.team
        $resourceCheck.Tag_Product = $resource.Tags.product
        $resourceCheck.Tag_Environment = $resource.Tags.environment
        $resourceCheck.Tag_Data = $resource.Tags.data
        $resourceCheck.Tag_Delete = $resource.Tags.delete
        $resourceCheck.Tag_Split = $resource.Tags.split
        $resourceCheck.Tag_CreatedOnDate = $resource.Tags.CreatedOnDate
        $resourceCheck.Tag_Deployment = $resource.Tags.drp_deployment
        $Result += $resourceCheck
    }

    $Result | Export-Csv -Path $fileName -Append -NoTypeInformation
}

Write-Host "========================================================================================================================================================================"
Write-Host "Done."
