Find Orphaned API Connectors
  • 04 Mar 2020
  • 2 Minutes to read
  • Contributors
  • Comment
  • Dark
    Light
  • PDF

Find Orphaned API Connectors

  • Comment
  • Dark
    Light
  • PDF

Article Summary

One of the pains in Logic App development is that you will add connectors which create a connector resource in Azure but over time as things change you end up with unused API Connections which are no longer used by a Logic App. It is not easy to workout which API Connection is used by which Logic App!

I have put together a powershell script which will look at all of the API Connections in your resource group and then inspect every Logic App in your resource group and check if the API Connections are used. You can then get the csv file that is produced to easily identify orphaned API Connections.

You could also modify the below script to remove those Azure API Connection resources if you wanted using the Remove-AzResource command.

Full Script

#This will check for SAP API Connections and look at where they are pointing to

$subscriptioName = 'My Subscription'
$resourceGroupName = 'My Resource Group'

#use the collection to build up objects for the table
$connectorDictionary = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.Object]" 

az login
az account set --subscription $subscriptioname

#Get Subscription Info
$subscription = Get-AzSubscription -SubscriptionName $subscriptioName
$subscriptionId = $subscription.Id
Write-Host 'Subscription Id: ' $subscriptionId

#Get Resource Group Info
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName
$resourceGroupPath = $resourceGroup.ResourceId
Write-Host 'Resource Group Path: '  $resourceGroupPath


Write-Host 'Looking up API Connectors'
$resourceName = ''
$resources = Get-AzResource -ResourceGroupName $resourcegroupName -ResourceType Microsoft.Web/connections
$resources | ForEach-Object { 
    
    $resourceName = $_.Name

    Write-Host 'Found connector: '$_.id
    $resourceIdLower = $_.id.ToLower()

    $azureConnector = New-Object -TypeName psobject
    $azureConnector | Add-Member -MemberType NoteProperty -Name 'IsUsed' -Value 'FALSE'
    $azureConnector | Add-Member -MemberType NoteProperty -Name 'Id' -Value $_.Id
    $azureConnector | Add-Member -MemberType NoteProperty -Name 'name' -Value $_.Name

    $connectorDictionary.Add($resourceIdLower, $azureConnector)  
}


#Check logic apps to find orphaned connectors
Write-Host ''
Write-Host ''
Write-Host ''
Write-Host 'Looking up Logic Apps'
$resources = Get-AzResource -ResourceGroupName $resourcegroupName -ResourceType Microsoft.Logic/workflows
$resources | ForEach-Object { 
    
    $resourceName = $_.Name    
    $logicAppName = $resourceName
    $logicApp = Get-AzLogicApp -Name $logicAppName -ResourceGroupName $resourceGroupName        
    $logicAppUrl = $resourceGroupPath + '/providers/Microsoft.Logic/workflows/' + $logicApp.Name + '?api-version=2018-07-01-preview'
    
    #Get Logic App Content
    $logicAppJson = az rest --method get --uri $logicAppUrl
    $logicAppJsonText = $logicAppJson | ConvertFrom-Json    

    #Check Logic App Connectors
    $logicAppParameters = $logicAppJsonText.properties.parameters
    $logicAppConnections = $logicAppParameters.psobject.properties.Where({$_.name -eq '$connections'}).value
    $logicAppConnectionValue = $logicAppConnections.value
    $logicAppConnectionValues = $logicAppConnectionValue.psobject.properties.name
    
    #Iterate through the connectors
    $logicAppConnectionValue.psobject.properties | ForEach-Object{

        $objectName = $_
        $connection = $objectName.Value             

        if($connection -ne $null)
        {
            Write-Host 'Logic App: ' $logicAppName ' uses connector: name='$connection.connectionName ', id=' $connection.connectionId        

            #Check if connector is in the connector dictionary
            $connectorIdLower = $connection.connectionId.ToLower()

            if($connectorDictionary.ContainsKey($connectorIdLower))
            {
                #Mark connector as being used                        
                $matchingConnector = $connectorDictionary[$connectorIdLower]
                $matchingConnector.IsUsed = 'TRUE'
                $connectorDictionary[$connectorIdLower] = $matchingConnector 
                Write-Host 'Marking connector as used: ' $connectorIdLower
            }
        }                                            
   }       
}

Write-Host ''
Write-Host ''
Write-Host ''
Write-Host 'Orphaned API Connectors'
$connectorDictionary.Values | ForEach-Object{
    $azureConnector = $_

    Write-Host $azureConnector.Id ' : ' $azureConnector.IsUsed
    if($azureConnector.IsUsed -eq 'FALSE')
    {
        Write-Host $azureConnector.Id ' : is an orphan'
    }
}

$connectorDictionary.Values | Export-Csv -Path C:\Temp\OrphanedConnectors.csv



Was this article helpful?