Use PowerShell Script to Manage Your API Connection of Logic App (Consumption) Resources
Published Aug 23 2021 07:28 PM 9,951 Views
Microsoft

When you are developing Logic Apps (Consumption) or testing existing Logic Apps, you might create many API connections which might be never used later. Those orphan resources could make your resource group a great mess and hard to choose the right API connection in the logic app. So, I wrote the PowerShell Script to help manage API connections in the resource group. 

 

Important: this script will only handle API connections for Logic Apps Consumption, or saying API connections V1. It will ignore all Logic Apps Standard API connections (V2).

 

Yanbo_Deng_0-1629448773750.png

 

What this script could do:

  1. List all logic apps and their associated API connections in the resource group.
  2. List all API connections in the resource group.
  3. List all API connections that is used by any logic apps in the resource group.
  4. List all API connections that is not used by any logic apps in the resource group.
  5. Delete all orphan API connections if you would like to.

How I define an API connection is an orphan:

If an API connection is not associated with any logic apps, it will be identified as an orphan. Note that, if there is an API connection temporarily dissociated with logic apps, but might be used later, it will also be identified as an orphan. So, be careful to delete them.

How to use the script:

  1. In the script, you will need to change the subscription id and resource group name in the script to yours:
    $sub = “<your subscription id>”
    $rg = “<your resource group name>”

  2. If you have setup your computer to run Azure CLI before, you could run it locally; otherwise, I suggest you use Azure Portal Cloud Shell. Upload the script to Azure cloud shell and run the script directly.
    Yanbo_Deng_0-1629447008885.png

     

  3. Wait until the script listed all API connections, API connections in Use, and API connections NOT in use. You could type in DELETE to confirm deleting all orphan API connections, or type in any other keys to exit the script. 
    Yanbo_Deng_1-1629447008890.png

 

 

 

 

 

$sub = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #Your SubscriptionId
$rg = "<Your_Resource_Group_Name>"

#Choose specific subscription
Select-AzSubscription -SubscriptionId $sub

#Query all workflows and api connections
$workflows = Get-AzResource -ResourceGroupName $rg -ResourceType 'Microsoft.Logic/workflows'
$apiConnections = Get-AzResource -ResourceGroupName $rg -ResourceType 'Microsoft.Web/connections'
$apiInUseSet = New-Object System.Collections.Generic.HashSet[String]
$apiAllSet = New-Object System.Collections.Generic.HashSet[String]
$apis = @{}

#iterate through all workflows, pick all api connections that is currently using
foreach ($workflow in $workflows) {
    $workflowJson = az rest --method get --uri ($workflow.Id + '?api-version=2016-06-01')
    $apisInWorkflow = ($workflowJson | ConvertFrom-Json).properties.parameters.'$connections'.value.psobject.properties.value#.connectionName
    Write-Host ' '
    Write-Host 'Logic App: '  $workflow.Name -ForegroundColor Green
    Write-Host 'Has following API connections:'
    Write-Host $apisInWorkflow.connectionName -ForegroundColor Red
    foreach ($api in $apisInWorkflow) {
        $apiInUseSet.Add($api.connectionName) | Out-Null
    }
}

#Get all api connection names
foreach ($api in $apiConnections) {
    # handle API connections for Logic App V1 only
    if($api.Kind -ne "V1"){
        continue;
    }
    $apiAllSet.Add($api.Name) | Out-Null
    $apis.Add($api.Name, $api.ResourceId) | Out-Null
}

#Display API connection status
Write-Host ' '
Write-Host '=============================================='
Write-Host 'All API Connections:'
Write-Host $apiAllSet -ForegroundColor Green
Write-Host ' '
Write-Host 'API Connections In Use:'
Write-Host $apiInUseSet -ForegroundColor Red
Write-Host 'API details:'
$Tab = [char]9
foreach ($api in $apiInUseSet) {
    $apiProperties = (az rest --method get --uri ($apis[$api] + '?api-version=2016-06-01') | ConvertFrom-Json)#.properties.authenticatedUser.name
    Write-Host $apiProperties.name -ForegroundColor Red -NoNewline
    Write-Host $Tab $apiProperties.properties.authenticatedUser.name -ForegroundColor Red
}

#find all api connections that is not used
Write-Host ' '
Write-Host 'API Connections NOT In Use:'
$apiAllSet.ExceptWith($apiInUseSet)
Write-Host $apiAllSet -ForegroundColor Yellow

#Delete all unused API connections if required
Write-Host '=============================================='
Write-Host ' '
Write-Host 'Enter ' -NoNewline
Write-Host 'DELETE' -NoNewline -ForegroundColor Yellow
$flag = Read-Host ' to delete all unused API connections, or any other key to exit...'
switch ($flag) {
    DELETE {
        foreach ($api in $apiAllSet) {
            Write-Host 'Deleting API connection: [' $api '] ...' -NoNewline
            az rest --method delete --uri ($apis[$api] + '?api-version=2016-06-01')
            Write-Host ' Completed'
        }
    }
    Default {}
}
Write-Host 'All done! Thanks for using!'

 

 

 

 

 

 

5 Comments
Co-Authors
Version history
Last update:
‎Aug 23 2021 07:28 PM
Updated by: