#Connect-AzAccount

class ResourceCheck {
    [string] $ResourceId = ""
    [string] $Kind = ""
    [string] $Location = ""
    [string] $ResourceName = ""
    [string] $ResourceGroup = ""
    [string] $ResourceType = ""
    [string] $State = ""
    [string] $SubscriptionId = ""
    [string] $SubscriptionName = ""
    [string] $Tag_Team = ""
    [string] $Tag_Product = ""
    [string] $Tag_Environment = ""
    [string] $Tag_Data = ""
    [string] $Tag_CreatedOnDate = ""
    [string] $Prop_HttpsOnly = ""
    [string] $Prop_PhpVersion = ""
    [string] $Prop_RemoteDebuggingEnabled = ""
    [string] $Prop_MinTlsVersion = ""
    [string] $Prop_FtpsState = ""
}


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

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

$fileName =  "c:\temp\2023-03-15 azure_webapps.csv" 
# rm $fileName

foreach ($subscription in $subscriptions) 
{
    Write-Host "----------------------------------------------------------------------------------------------------------------------------------------------------------------------"

    Set-AzContext -SubscriptionId $subscription.Id

    Write-Host "----------------------------------------------------------------------------------------------------------------------------------------------------------------------"

    $allResourceGroups = Get-AzResourceGroup
    [ResourceCheck[]]$Result = @()

    foreach ($group in $allResourceGroups) {

        Write-Host $group.ResourceGroupName

        $allWebApps = Get-AzWebApp -ResourceGroupName $group.ResourceGroupName
        
        foreach ($webApp in $allWebApps) {

            [ResourceCheck] $resourceCheck = [ResourceCheck]::new()
            $resourceCheck.ResourceId = $webApp.Id
            $resourceCheck.Kind = $webApp.Kind
            $resourceCheck.Location = $webApp.Location
            $resourceCheck.State = $webApp.State
            $resourceCheck.ResourceName = $webApp.Name
            $resourceCheck.ResourceGroup = $webApp.ResourceGroup
            $resourceCheck.ResourceType = $webApp.Type
            $resourceCheck.SubscriptionId = $subscription.Id
            $resourceCheck.SubscriptionName = $subscription.Name
            $resourceCheck.Tag_Team = $webApp.Tags.team
            $resourceCheck.Tag_Product = $webApp.Tags.product
            $resourceCheck.Tag_Environment = $webApp.Tags.environment
            $resourceCheck.Tag_Data = $webApp.Tags.data
            $resourceCheck.Tag_CreatedOnDate = $webApp.Tags.CreatedOnDate
            $resourceCheck.Prop_HttpsOnly = $webApp.HttpsOnly
            $resourceCheck.Prop_PhpVersion = $webApp.SiteConfig.PhpVersion
            $resourceCheck.Prop_RemoteDebuggingEnabled = $webApp.SiteConfig.RemoteDebuggingEnabled
            $resourceCheck.Prop_MinTlsVersion = $webApp.SiteConfig.MinTlsVersion
            $resourceCheck.Prop_FtpsState = $webApp.SiteConfig.FtpsState

            $Result += $resourceCheck

            $allSlots = Get-AzWebAppSlot -Name $webApp.Name -ResourceGroupName $webApp.ResourceGroup

            foreach ($slotTemp in $allSlots) {

                [string] $slotName = $slotTemp.Name.Split("/")[1]
                $slot = Get-AzWebAppSlot -Name $webApp.Name -ResourceGroupName $webApp.ResourceGroup -Slot $slotName

                [ResourceCheck] $resourceCheck = [ResourceCheck]::new()
                $resourceCheck.ResourceId = $slot.Id
                $resourceCheck.Kind = $slot.Kind
                $resourceCheck.Location = $slot.Location
                $resourceCheck.State = $slot.State
                $resourceCheck.ResourceName = $slot.Name
                $resourceCheck.ResourceGroup = $slot.ResourceGroup
                $resourceCheck.ResourceType = $slot.Type
                $resourceCheck.SubscriptionId = $subscription.Id
                $resourceCheck.SubscriptionName = $subscription.Name
                $resourceCheck.Tag_Team = $slot.Tags.team
                $resourceCheck.Tag_Product = $slot.Tags.product
                $resourceCheck.Tag_Environment = $slot.Tags.environment
                $resourceCheck.Tag_Data = $slot.Tags.data
                $resourceCheck.Tag_CreatedOnDate = $slot.Tags.CreatedOnDate
                $resourceCheck.Prop_HttpsOnly = $slot.HttpsOnly
                $resourceCheck.Prop_PhpVersion = $slot.SiteConfig.PhpVersion
                $resourceCheck.Prop_RemoteDebuggingEnabled = $slot.SiteConfig.RemoteDebuggingEnabled
                $resourceCheck.Prop_MinTlsVersion = $slot.SiteConfig.MinTlsVersion
                $resourceCheck.Prop_FtpsState = $slot.SiteConfig.FtpsState

                $Result += $resourceCheck
            }
        }
    }
    $Result | Export-Csv -Path $fileName -Append -NoTypeInformation
}

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