We are currently doing an ISE migration from Logic App ISE to a combo of Logic App Standard and Logic App Consumption in addition to doing a bunch of new projects. As part of this we end up with a bunch of Logic Apps which are no longer used and need to be deleted across all of our environments.

I wanted to script the deletion of the logic apps and there was quite a few of them, also Luciano said “I think Kurtis would like to take a hammer to one of our integrations” which has been a support pain for a while so I thought it might also be fun to call the script “hammer time”.

How I approached this is as follows:

  • We use Azure DevOps to track the logic apps for our ISE migration as requirements. Each one has the logic app name on it as a DevOps property
  • Each one has a field on the DevOps work item for the ISE migration action which we have set to either “move to standard”, “move to consumption”, “decommision now” or “decommission later”
  • We use the state property in DevOps and we have a State of “Ready for Deployment”

When we get the DevOps work item list with 1 requirement for each of the Logic App we need to decommission we can then go through out change process and get approval for removing the Logic Apps and we then run the script.

It will kick off with the nice little ascii-art for kurtis that we are sorting out the interface he hates

The script then queries all of the requirements to decommission logic apps which are ready for deployment and then loops over and deletes them. I also had a flag so we can check which ones will be deleted.

When it completes we have another fun message.

As a one off we dont need to do this, but when we will have to repeat this process a couple of times with some new projects introducing new systems which will require us to build new logic apps which will replaces ones which integrated with old systems then the decommissioning process will become a thing we need to do a few times so having this script and its link to devops gives us a bit of a structured way to manage this lifecycle.

I also have a field on the Work items for the change number so we will probably modify the script to query based on the change number too.

Running the Script

In order to run the script you will need to connect to Azure with Connect-AzAccount and also you will need a PAT token for DevOps (There are some other authentication options now too worth checking out).

If you want the ascii-art then thanks to Joakim and Andy for this powershell module

https://www.powershellgallery.com/packages/WriteAscii/1.2.2.1

Powershell Script

The script for this is below:


Write-Ascii "Logic App - Hammer Time" 

# do you want to delete the logic apps or just check what would be deleted
$enableDeleteLogicApp = $false
$devOpsPersonalToken = "[Add PAT Token]"

#DevOps Settings
$devOpsOrganization = "[Add organization name]"
$devOpsProject = "[DevOps project name]"

#Azure Settings
$azureResourceGroupName = "[Azure Resource Group]"


#DevOps Url
$devOpsProjectEncoded = [uri]::EscapeDataString($devOpsProject)
$devOpsBaseUrl = "https://dev.azure.com/$devOpsOrganization/$devOpsProjectEncoded"

Write-Host "Creating Header for DevOps PAT Token" -ForegroundColor Yellow
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($devOpsPersonalToken)"))
$header = @{authorization = "Basic $token"}


# Get DevOps Work Items via a WIGL query
$wiglQuery = Get-Content -Path 'LogicApps-Delete-DecommissionNow.wigl.txt'
Write-Host $wiglQuery

$devOpsQueryUrl = $devOpsBaseUrl + "/_apis/wit/wiql?api-version=7.0"
Write-Host $devOpsQueryUrl

$output = Invoke-RestMethod -Uri $devOpsQueryUrl -Method Post -ContentType "application/json" -Headers $header -Body $wiglQuery
Write-Host $output 
$output.workItems | ForEach-Object {
    $workItemId = $_.id
    
    # Get Work Item
    $devOpsQueryUrl = $devOpsBaseUrl + "/_apis/wit/workitems/" + $workItemId + "?api-version=7.0"
    $output = Invoke-RestMethod -Uri $devOpsQueryUrl -Method Get -ContentType "application/json" -Headers $header 

    # Get Logic App Name from Response
    $fields = $output.fields    
    Write-Host "Logic App Name" -ForegroundColor Green
    $logicAppName = $fields.{Custom.LogicAppName}
    Write-Host $logicAppName

    $logicApp = Get-AzLogicApp -ResourceGroupName $azureResourceGroupName -Name $logicAppName -ErrorAction SilentlyContinue
    if($logicApp -eq $null){
        Write-Host "Logic App does not exist in resource group" -ForegroundColor Yellow
    }
    else {      

        if($enableDeleteLogicApp -eq $true){
            Write-Host "Logic App will be deleted" -ForegroundColor Red
            
            Remove-AzLogicApp -ResourceGroupName $azureResourceGroupName -Name $logicAppName -Force
        }
        else {
            Write-Host "Logic App would be deleted, but delete function is disabled" -ForegroundColor DarkMagenta
        }
    }
    
}

Write-Ascii "Your logic apps"
Write-Ascii "have been hammered!"

DevOps Query

The Azure DevOps wigl query which I reference is below

{
    "query":"SELECT
    [System.Id],
    [System.WorkItemType],
    [System.Title],
    [System.State],
    [Custom.ISEMigrationPlan],
    [Custom.LogicAppName]
FROM workitems
WHERE
    [System.TeamProject] = @project
    AND [System.WorkItemType] = 'Requirement'
    AND [Custom.IsISEMigration] = 'YES'
    AND [Custom.ISEMigrationPlan] = 'Decommission Now'
    AND [System.State] = 'Ready for Deployment'"
}

 

Buy Me A Coffee