Created
May 28, 2024 22:52
-
-
Save Rugby-Ball/f63c70076c3470cb84e3230824565043 to your computer and use it in GitHub Desktop.
or Azure, creates all the Bicep files for an existing Azure Subscription, and exports to a folder. #Bicep #Public #Utility #BackUp #Azure #Disaster_Recovery #DR
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
| # Azure-create-all-bicep-files-environment.ps1 | |
| <# | |
| Description: For Azure, creates all the Bicep files for an existing Azure Subscription, and exports to a folder. | |
| Edited by: Ed Walsh | |
| PowerShell.Core tested: Yes | |
| MS-Graph: No | |
| Version: 1.0.0 | |
| Create Date: 5/28/2024 | |
| Revised Date: 5/28/2024 | |
| #> | |
| # Login to Azure | |
| Connect-AzAccount | |
| # Specify the subscription you want to work with | |
| $subscriptionId = "your-subscription-id" | |
| Set-AzContext -SubscriptionId $subscriptionId | |
| #Start Timer and let user know. | |
| Clear-Host | |
| $stopWatch = [System.Diagnostics.Stopwatch]::StartNew() | |
| $stopwatch.Start() | |
| Write-Host "Script started on $(Get-Date -f "MM-dd-yyyy hh:mm tt") This script might take sometime to run." -ForegroundColor Yellow -BackgroundColor Red | |
| # Get all resources in the subscription | |
| $resources = Get-AzResource | |
| # Group resources by resource type | |
| $resourcesByType = $resources | Group-Object -Property ResourceType | |
| # Create a folder to store the Bicep files | |
| $outputFolder = "AzureBicepFiles_$(Get-Date -Format 'yyyy-MM-dd')" | |
| New-Item -ItemType Directory -Path $outputFolder -Force | Out-Null | |
| # Get the total number of resource types for the progress bar | |
| $totalResourceTypes = ($resourcesByType | Measure-Object).Count | |
| # Initialize the progress bar | |
| $progressBar = { | |
| param($id, $activity, $status, $percentComplete) | |
| Write-Progress -Id $id -Activity $activity -Status $status -PercentComplete $percentComplete | |
| } | |
| $progressBarScriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock($progressBar) | |
| # Loop through each resource type and generate a Bicep file | |
| $resourceTypeIndex = 0 | |
| foreach ($group in $resourcesByType) { | |
| $resourceType = $group.Name | |
| $bicepFilePath = Join-Path $outputFolder "$($resourceType.Replace('/', '_')).bicep" | |
| $bicepContent = @" | |
| // Bicep file for resource type: $resourceType | |
| "@ | |
| foreach ($resource in $group.Group) { | |
| $resourceName = $resource.Name | |
| $resourceGroupName = $resource.ResourceGroupName | |
| $location = $resource.Location | |
| $tags = $resource.Tags | |
| # Collect resource-specific properties | |
| $properties = @{} | |
| if ($null -ne $resource.Properties) { | |
| $resource.Properties.PSObject.Properties | ForEach-Object { | |
| $properties[$_.Name] = $_.Value | |
| } | |
| } | |
| $bicepContent += @" | |
| // Resource: $resourceType/$resourceName | |
| resource $resourceName '$resourceType' = { | |
| name: '$resourceName' | |
| location: '$location' | |
| tags: { | |
| "@ | |
| foreach ($tagKey in $tags.Keys) { | |
| $tagValue = $tags[$tagKey] | |
| $bicepContent += @" | |
| '$tagKey': '$tagValue' | |
| "@ | |
| } | |
| $bicepContent += @" | |
| } | |
| properties: { | |
| "@ | |
| foreach ($propertyKey in $properties.Keys) { | |
| $propertyValue = $properties[$propertyKey] | |
| $propertyValueString = $propertyValue | ConvertTo-Json -Compress -Depth 5 -AsArray | |
| $bicepContent += @" | |
| '${propertyKey}': $propertyValueString | |
| "@ | |
| } | |
| $bicepContent += @" | |
| } | |
| } | |
| "@ | |
| } | |
| # Write the Bicep content to a file | |
| $bicepContent | Out-File -FilePath $bicepFilePath -Encoding utf8 -Force | |
| Write-Host "Bicep file generated: $bicepFilePath" | |
| # Update the progress bar | |
| $resourceTypeIndex++ | |
| $percentComplete = ($resourceTypeIndex / $totalResourceTypes) * 100 | |
| $status = "Generating Bicep files ($resourceTypeIndex/$totalResourceTypes)" | |
| & $progressBarScriptBlock -id 1 -activity "Generating Bicep files" -status $status -percentComplete $percentComplete | |
| } | |
| Write-Host "Bicep files generated in folder: $outputFolder" | |
| # End timer and display. | |
| $stopwatch.Stop() | |
| $time = $stopwatch.Elapsed | |
| Write-Host "Script finished on $(Get-Date -f "MM-dd-yyyy hh:mm tt"), Elapsed time to run script (HH:MM:SS.MS): $Time" -ForegroundColor Green -BackgroundColor Yellow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment