scripts/Export-PSBicepApiManagementService.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function Export-PSBicepApiManagementService (
    $SubscriptionId ,
    $ResourceGroupName ,
    $ApiManagementName 
)
{
    write-host "Connecting to Subscription Id $SubscriptionId"
    Set-AzContext -SubscriptionId $SubscriptionId|Out-Null
    $sourceApiManagement= Get-AzApiManagement -Name $ApiManagementName -ResourceGroupName $ResourceGroupName

    $tempFile = "$($env:TEMP)/$(get-random).json"
    write-host "Exporting Api Management to $tempFile"
    $exportFile = Export-AzResourceGroup -ResourceGroupName $ResourceGroupName -Resource $sourceApiManagement.Id -Path "$tempFile" -IncludeParameterDefaultValue
    Copy-Item "$PSScriptRoot/bicepconfig.json" "$($env:TEMP)/bicepconfig.json"
    bicep decompile $exportFile.Path --outfile "$($exportFile.Path).bicep"|Out-Null
    $bicepData = Get-Content "$($exportFile.Path).bicep" -Raw   

    Remove-Item "$tempFile"
    write-host " $tempFile removed"
    Remove-Item "$($exportFile.Path).bicep"
    write-host " $($exportFile.Path).bicep removed"
    Remove-Item "$($env:TEMP)/bicepconfig.json"

    $bicepDocument = $bicepData|ConvertFrom-PSBicepDocument 
    
    return $bicepDocument
}