Scripts/Remove-AzApiManagementDefaults.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
28
29
30
31
32
33
34
35
36
param(
    [string][parameter(Mandatory = $true)] $ResourceGroup,
    [string][parameter(Mandatory = $true)] $ServiceName
)

Write-Host "Start removing Azure API Management defaults..."
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroup -ServiceName $ServiceName 

Write-Host "Removing Echo Api..."
$apiResult = Remove-AzApiManagementApi -Context $apimContext -ApiId 'echo-api'

Write-Host "Removing Starter product..."
$starterResult = Remove-AzApiManagementProduct -Context $apimContext -ProductId 'starter' -DeleteSubscriptions

Write-Host "Removing Unlimited product..."
$unlimitedResult = Remove-AzApiManagementProduct -Context $apimContext -ProductId 'unlimited' -DeleteSubscriptions

if ($apiResult -and $starterResult -and $unlimitedResult) {
    Write-Host "Successfully removed the 'echo-api' API, 'starter' Product and 'unlimited' Product"
} else {
    $message = "Failed to remove API Management defaults"
    if (-not $apiResult) {
        $message += [System.Environment]::NewLine + "> Failed to remove the 'echo' API"
    }
    if (-not $starterResult) {
        $message += [System.Environment]::NewLine + "> Failed to remove the 'starter' Product"
    }
    if (-not $unlimitedResult) {
        $message += [System.Environment]::NewLine + "> Failed to remove the 'unlimited' Product"
    }

    Write-Error $message
}

Write-Host "Finished removing Azure API Management defaults!"