frameworkResources/Scripts/list_all_environments.ps1

function ShowEnvs {
    Write-Host "Fetching Environments"
    $all_resource_groups = Get-AzResourceGroup -ErrorAction Stop | Where-Object { $_.Tags -and $_.Tags.ContainsKey("EnvironmentName") }

    if (-not $all_resource_groups) {
        Write-Output "No Environments Found"
        return
    }

    foreach ($rg in $all_resource_groups) {
        Write-Output "`n======================="
        Write-Output "Environment Information"
        Write-Output "======================="

        $envObject = [PSCustomObject]@{
            EnvironmentName = $rg.Tags["EnvironmentName"]
            OS              = $rg.Tags["OsType"]
            ServerProfile   = $rg.Tags["ServerProfile"]
            Version         = $rg.Tags["Version"]
            Location        = $rg.Location
            ResourceId      = $rg.ResourceId
        }

        $envObject | Format-List

        # ---- FETCH DASHBOARDS FOR THIS RG ----
        $dashboards = Get-AzResource -ResourceGroupName $rg.ResourceGroupName -ResourceType "Microsoft.Portal/dashboards" -ErrorAction SilentlyContinue
        
        Write-Output "`nDashboards for this Environment:"
        if (-not $dashboards) {
            Write-Output "No Dashboards Found"
        }
        else {
            $dashboardObjects = foreach ($dashboard in $dashboards) {
                [PSCustomObject]@{
                    DashboardName = $dashboard.Name
                    Link          = "https://portal.azure.com/#dashboard/arm$($dashboard.Id)"
                }
            }

            $dashboardObjects | Format-List
        }

        ShowDeployedVms -rgName $rg.ResourceGroupName

        Write-Output "`n--------------------------------`n"
    }
}

function ShowDeployedVms {

    param(
        [string]$rgName
    )

    $vms = Get-AzVM -ResourceGroupName $rgName -Status | Where-Object { $_.Tags.ContainsKey("InstallMode") -and $_.Tags["InstallMode"] -eq "server" }

    $results = foreach ($vm in $vms) {
        $nicId = $vm.NetworkProfile.NetworkInterfaces[0].Id
        $nicName = ($nicId -split '/')[8]
        $nic = Get-AzNetworkInterface -ResourceGroupName $rgName -Name $nicName

        $privateIp = $nic.IpConfigurations[0].PrivateIpAddress
        $publicIp = $null
        $managementLink = $null

        if ($nic.IpConfigurations[0].PublicIpAddress) {
            $publicIpId = $nic.IpConfigurations[0].PublicIpAddress.Id
            $publicIpName = ($publicIpId -split '/')[8]
            $publicIpObj = Get-AzPublicIpAddress -ResourceGroupName $rgName -Name $publicIpName
            $publicIp = $publicIpObj.IpAddress

            if ($publicIp) {
                $managementLink = "http://$publicIp`:8251"
            }
        }

        [PSCustomObject]@{
            VMName         = $vm.Name
            PrivateIP      = $privateIp
            PublicIP       = $publicIp
            ManagementLink = $managementLink
        }
    }
    $results | Format-Table -AutoSize
}

Write-Host "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) Executing command"
if (-not (Get-AzContext)) {
    Connect-AzAccount
    if (Get-AzContext) { ShowEnvs }
}
else {
    ShowEnvs
}