Use PowerShell Script to Manage Your API Connection of Logic App (Consumption) Resources

Published Aug 23 2021 07:28 PM 2,046 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!'

 

 

 

 

 

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2668253%22%20slang%3D%22en-US%22%3EUse%20PowerShell%20Script%20to%20Manage%20Your%20API%20Connection%20of%20Logic%20App%20(Consumption)%20Resources%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2668253%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20you%20are%20developing%20Logic%20Apps%26nbsp%3B(Consumption)%20or%20testing%20existing%20Logic%20Apps%2C%20you%20might%20create%20many%20API%20connections%20which%20might%20be%20never%20used%20later.%20Those%20orphan%20resources%20could%20make%20your%20resource%20group%20a%20great%20mess%20and%20hard%20to%20choose%20the%20right%20API%20connection%20in%20the%20logic%20app.%20So%2C%20I%20wrote%20the%20PowerShell%20Script%20to%20help%20manage%20API%20connections%20in%20the%20resource%20group.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20color%3D%22%23FF0000%22%3EImportant%3C%2FFONT%3E%3A%20this%20script%20will%20only%20handle%20API%20connections%20for%20Logic%20Apps%20Consumption%2C%20or%20saying%20API%20connections%20V1.%20It%20will%20ignore%20all%20Logic%20Apps%20Standard%20API%20connections%20(V2).%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Yanbo_Deng_0-1629448773750.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F304604iDDD2A9DC24C8C974%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Yanbo_Deng_0-1629448773750.png%22%20alt%3D%22Yanbo_Deng_0-1629448773750.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20this%20script%20could%20do%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EList%20all%20logic%20apps%20and%20their%20associated%20API%20connections%20in%20the%20resource%20group.%3C%2FLI%3E%0A%3CLI%3EList%20all%20API%20connections%20in%20the%20resource%20group.%3C%2FLI%3E%0A%3CLI%3EList%20all%20API%20connections%20that%20is%20used%20by%20any%20logic%20apps%20in%20the%20resource%20group.%3C%2FLI%3E%0A%3CLI%3EList%20all%20API%20connections%20that%20is%20not%20used%20by%20any%20logic%20apps%20in%20the%20resource%20group.%3C%2FLI%3E%0A%3CLI%3EDelete%20all%20orphan%20API%20connections%20if%20you%20would%20like%20to.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EHow%20I%20define%20an%20API%20connection%20is%20an%20orphan%3A%3C%2FP%3E%0A%3CP%3EIf%20an%20API%20connection%20is%20not%20associated%20with%20any%20logic%20apps%2C%20it%20will%20be%20identified%20as%20an%20orphan.%20Note%20that%2C%20if%20there%20is%20an%20API%20connection%20temporarily%20dissociated%20with%20logic%20apps%2C%20but%20might%20be%20used%20later%2C%20it%20will%20also%20be%20identified%20as%20an%20orphan.%20So%2C%20be%20careful%20to%20delete%20them.%3C%2FP%3E%0A%3CP%3EHow%20to%20use%20the%20script%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EIn%20the%20script%2C%20you%20will%20need%20to%20change%20the%20subscription%20id%20and%20resource%20group%20name%20in%20the%20script%20to%20yours%3A%3CBR%20%2F%3E%24sub%20%3D%20%E2%80%9C%3CYOUR%20subscription%3D%22%22%20id%3D%22%22%3E%E2%80%9D%3CBR%20%2F%3E%24rg%20%3D%20%E2%80%9C%3CYOUR%20resource%3D%22%22%20group%3D%22%22%20name%3D%22%22%3E%E2%80%9D%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FYOUR%3E%3C%2FYOUR%3E%3C%2FLI%3E%0A%3CLI%3EIf%20you%20have%20setup%20your%20computer%20to%20run%20Azure%20CLI%20before%2C%20you%20could%20run%20it%20locally%3B%20otherwise%2C%20I%20suggest%20you%20use%20Azure%20Portal%20Cloud%20Shell.%20Upload%20the%20script%20to%20Azure%20cloud%20shell%20and%20run%20the%20script%20directly.%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Yanbo_Deng_0-1629447008885.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F304599iC09B85F30C076CEC%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Yanbo_Deng_0-1629447008885.png%22%20alt%3D%22Yanbo_Deng_0-1629447008885.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3EWait%20until%20the%20script%20listed%20all%20API%20connections%2C%20API%20connections%20in%20Use%2C%20and%20API%20connections%20NOT%20in%20use.%20You%20could%20type%20in%20%3CSTRONG%3EDELETE%3C%2FSTRONG%3E%20to%20confirm%20deleting%20all%20orphan%20API%20connections%2C%20or%20type%20in%20any%20other%20keys%20to%20exit%20the%20script.%26nbsp%3B%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Yanbo_Deng_1-1629447008890.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F304598iF3B9063E3F4B03E1%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Yanbo_Deng_1-1629447008890.png%22%20alt%3D%22Yanbo_Deng_1-1629447008890.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24sub%20%3D%20%22xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx%22%20%23Your%20SubscriptionId%0A%24rg%20%3D%20%22%3CYOUR_RESOURCE_GROUP_NAME%3E%22%0A%0A%23Choose%20specific%20subscription%0ASelect-AzSubscription%20-SubscriptionId%20%24sub%0A%0A%23Query%20all%20workflows%20and%20api%20connections%0A%24workflows%20%3D%20Get-AzResource%20-ResourceGroupName%20%24rg%20-ResourceType%20'Microsoft.Logic%2Fworkflows'%0A%24apiConnections%20%3D%20Get-AzResource%20-ResourceGroupName%20%24rg%20-ResourceType%20'Microsoft.Web%2Fconnections'%0A%24apiInUseSet%20%3D%20New-Object%20System.Collections.Generic.HashSet%5BString%5D%0A%24apiAllSet%20%3D%20New-Object%20System.Collections.Generic.HashSet%5BString%5D%0A%24apis%20%3D%20%40%7B%7D%0A%0A%23iterate%20through%20all%20workflows%2C%20pick%20all%20api%20connections%20that%20is%20currently%20using%0Aforeach%20(%24workflow%20in%20%24workflows)%20%7B%0A%20%20%20%20%24workflowJson%20%3D%20az%20rest%20--method%20get%20--uri%20(%24workflow.Id%20%2B%20'%3Fapi-version%3D2016-06-01')%0A%20%20%20%20%24apisInWorkflow%20%3D%20(%24workflowJson%20%7C%20ConvertFrom-Json).properties.parameters.'%24connections'.value.psobject.properties.value%23.connectionName%0A%20%20%20%20Write-Host%20'%20'%0A%20%20%20%20Write-Host%20'Logic%20App%3A%20'%20%20%24workflow.Name%20-ForegroundColor%20Green%0A%20%20%20%20Write-Host%20'Has%20following%20API%20connections%3A'%0A%20%20%20%20Write-Host%20%24apisInWorkflow.connectionName%20-ForegroundColor%20Red%0A%20%20%20%20foreach%20(%24api%20in%20%24apisInWorkflow)%20%7B%0A%20%20%20%20%20%20%20%20%24apiInUseSet.Add(%24api.connectionName)%20%7C%20Out-Null%0A%20%20%20%20%7D%0A%7D%0A%0A%23Get%20all%20api%20connection%20names%0Aforeach%20(%24api%20in%20%24apiConnections)%20%7B%0A%20%20%20%20%23%20handle%20API%20connections%20for%20Logic%20App%20V1%20only%0A%20%20%20%20if(%24api.Kind%20-ne%20%22V1%22)%7B%0A%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%7D%0A%20%20%20%20%24apiAllSet.Add(%24api.Name)%20%7C%20Out-Null%0A%20%20%20%20%24apis.Add(%24api.Name%2C%20%24api.ResourceId)%20%7C%20Out-Null%0A%7D%0A%0A%23Display%20API%20connection%20status%0AWrite-Host%20'%20'%0AWrite-Host%20'%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D'%0AWrite-Host%20'All%20API%20Connections%3A'%0AWrite-Host%20%24apiAllSet%20-ForegroundColor%20Green%0AWrite-Host%20'%20'%0AWrite-Host%20'API%20Connections%20In%20Use%3A'%0AWrite-Host%20%24apiInUseSet%20-ForegroundColor%20Red%0AWrite-Host%20'API%20details%3A'%0A%24Tab%20%3D%20%5Bchar%5D9%0Aforeach%20(%24api%20in%20%24apiInUseSet)%20%7B%0A%20%20%20%20%24apiProperties%20%3D%20(az%20rest%20--method%20get%20--uri%20(%24apis%5B%24api%5D%20%2B%20'%3Fapi-version%3D2016-06-01')%20%7C%20ConvertFrom-Json)%23.properties.authenticatedUser.name%0A%20%20%20%20Write-Host%20%24apiProperties.name%20-ForegroundColor%20Red%20-NoNewline%0A%20%20%20%20Write-Host%20%24Tab%20%24apiProperties.properties.authenticatedUser.name%20-ForegroundColor%20Red%0A%7D%0A%0A%23find%20all%20api%20connections%20that%20is%20not%20used%0AWrite-Host%20'%20'%0AWrite-Host%20'API%20Connections%20NOT%20In%20Use%3A'%0A%24apiAllSet.ExceptWith(%24apiInUseSet)%0AWrite-Host%20%24apiAllSet%20-ForegroundColor%20Yellow%0A%0A%23Delete%20all%20unused%20API%20connections%20if%20required%0AWrite-Host%20'%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D'%0AWrite-Host%20'%20'%0AWrite-Host%20'Enter%20'%20-NoNewline%0AWrite-Host%20'DELETE'%20-NoNewline%20-ForegroundColor%20Yellow%0A%24flag%20%3D%20Read-Host%20'%20to%20delete%20all%20unused%20API%20connections%2C%20or%20any%20other%20key%20to%20exit...'%0Aswitch%20(%24flag)%20%7B%0A%20%20%20%20DELETE%20%7B%0A%20%20%20%20%20%20%20%20foreach%20(%24api%20in%20%24apiAllSet)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Write-Host%20'Deleting%20API%20connection%3A%20%5B'%20%24api%20'%5D%20...'%20-NoNewline%0A%20%20%20%20%20%20%20%20%20%20%20%20az%20rest%20--method%20delete%20--uri%20(%24apis%5B%24api%5D%20%2B%20'%3Fapi-version%3D2016-06-01')%0A%20%20%20%20%20%20%20%20%20%20%20%20Write-Host%20'%20Completed'%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20Default%20%7B%7D%0A%7D%0AWrite-Host%20'All%20done!%20Thanks%20for%20using!'%3C%2FYOUR_RESOURCE_GROUP_NAME%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2668253%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20you%20are%20developing%20Logic%20Apps%20(Consumption)%20or%20testing%20existing%20Logic%20Apps%2C%20you%20might%20create%20many%20API%20connections%20which%20might%20be%20never%20used%20later.%20Those%20orphan%20resources%20could%20make%20your%20resource%20group%20a%20great%20mess%20and%20hard%20to%20choose%20the%20right%20API%20connection%20in%20the%20logic%20app.%20So%2C%20I%20wrote%20the%20PowerShell%20Script%20to%20help%20manage%20API%20connections%20in%20the%20resource%20group.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2668253%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ELogic%20Apps%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Aug 23 2021 07:28 PM
Updated by: