Last active
September 21, 2023 13:58
-
-
Save joshfinley/9197a7fa649493f3e7400ae3612d794a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to flatten the object for CSV output | |
Function Flatten-AzureArchitecture { | |
param ( | |
[Parameter(Mandatory=$true)] | |
$AzureArchitecture | |
) | |
$flattenedData = @() | |
foreach ($sub in $AzureArchitecture) { | |
foreach ($rg in $sub.ResourceGroups) { | |
foreach ($res in $rg.Resources) { | |
$flattenedObject = [PSCustomObject]@{ | |
"SubscriptionName" = $sub.SubscriptionName | |
"SubscriptionID" = $sub.SubscriptionID | |
"ResourceGroupName" = $rg.ResourceGroupName | |
"ResourceName" = $res.ResourceName | |
"ResourceType" = $res.ResourceType | |
"Subnets" = if ($res.Subnets) { $res.Subnets -join '; ' } else { $null } | |
} | |
$flattenedData += $flattenedObject | |
} | |
} | |
} | |
return $flattenedData | |
} | |
# Initialize empty object for storing Azure architecture | |
$AzureArchitecture = @() | |
# Connect to Azure | |
Connect-AzAccount | |
# Status Update | |
Write-Host "Fetching Azure Subscriptions..." | |
# Enumerate Subscriptions | |
$subscriptions = Get-AzSubscription | |
foreach ($subscription in $subscriptions) { | |
# Status Update | |
Write-Host "Processing Subscription: $($subscription.Name)..." | |
$subObject = [PSCustomObject]@{ | |
SubscriptionName = $subscription.Name | |
SubscriptionID = $subscription.SubscriptionId | |
ResourceGroups = @() | |
} | |
Set-AzContext -SubscriptionId $subscription.SubscriptionId | |
$resourceGroups = Get-AzResourceGroup | |
foreach ($rg in $resourceGroups) { | |
Write-Host "`tProcessing Resource Group: $($rg.ResourceGroupName)..." | |
$rgObject = [PSCustomObject]@{ | |
ResourceGroupName = $rg.ResourceGroupName | |
Resources = @() | |
} | |
$resources = Get-AzResource -ResourceGroupName $rg.ResourceGroupName | |
foreach ($resource in $resources) { | |
$resObject = [PSCustomObject]@{ | |
ResourceName = $resource.Name | |
ResourceType = $resource.ResourceType | |
} | |
if ($resource.ResourceType -eq 'Microsoft.Network/virtualNetworks') { | |
$vnet = Get-AzVirtualNetwork -ResourceGroupName $rg.ResourceGroupName -Name $resource.Name | |
$subnets = $vnet.Subnets | ForEach-Object { $_.Name + " - " + $_.AddressPrefix } | |
$resObject | Add-Member -MemberType NoteProperty -Name 'Subnets' -Value $subnets | |
} | |
$rgObject.Resources += $resObject | |
} | |
$subObject.ResourceGroups += $rgObject | |
} | |
$AzureArchitecture += $subObject | |
} | |
# Choose how to save the output | |
$saveOption = Read-Host "Would you like to save the output? (Y/N)" | |
if ($saveOption -eq 'Y') { | |
$formatOption = Read-Host "Which format would you like to use? (Object/CSV)" | |
if ($formatOption -eq 'CSV') { | |
$csvPath = Read-Host "Enter the CSV file path to save" | |
$flattenedData = Flatten-AzureArchitecture -AzureArchitecture $AzureArchitecture | |
$flattenedData | Export-Csv -Path $csvPath -NoTypeInformation | |
} elseif ($formatOption -eq 'Object') { | |
$objectPath = Read-Host "Enter the PSObject file path to save (.json)" | |
$AzureArchitecture | ConvertTo-Json -Depth 10 | Set-Content -Path $objectPath | |
} | |
} | |
# Output the Azure architecture object | |
$AzureArchitecture |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment