'Disable multiple logic apps across different Resource Groups

I have a use case where I want to disable the old Logic Apps that our company is no longer using. All the Logic App are present in different environments and needs to disabled from all, ex: dev, sit, uat, prod. Rather than going to different RGs and disabling them one by one I am trying to develop a script which takes the portion of the Logic App name and disables it across all the resource group.

I was following this article but it shows how to disable multiple logic apps under one resource group and not what I am trying to achieve. [https://www.integration-playbook.io/docs/properly-disable-logic-apps-in-powershell][1]

I tried making some changes in the script to solve my use case but not sure how to test it and make necessary adjustments. I am very new to Logic Apps and would appreciate if anyone can help me with this.

I have added the displayName parameter so that that particular logic app is disabled and not all under the resource group. But I am not sure how to move forward.

# This script disables or enables all Logic apps under given resource group
# ./DisableEnable-LogicApps.ps1 "FC-CUS-DEV-INT-RG" "Disabled"
# Parameters are resourceGroupName and Disabled | Enabled


param([string] $displayName, [string] $state)

function ChangeLogicAppState([string] $logicAppName, [string] $resourceGroupName, [string] $desiredState, [string] $displayName)
{
    Write-Host 'Checking logic app ' $logicAppName

    $logicApp = Get-AzLogicApp -displayName $displayName -Name $logicAppName
    Write-Host ($logicApp | Format-List | Out-String)
        
    $apiOperation = 'unknown'
    if($desiredState -eq 'Enabled')
    {
        $apiOperation = 'enable'    
    }
    elseif($desiredState -eq 'Disabled')
    {
        $apiOperation = 'disable'
    }
        
    if($logicApp -eq $null)
    {
        Write-Host 'Logic App does not exist: ' $logicAppName
    }
    else
    {
        Write-Host 'Logic App does exist: ' $logicAppName

        $currentState = $logicApp.State.ToLower()        
        if($currentState -eq $desiredState.ToLower())
        {
            Write-Host 'Logic App ' $logicApp.Name ' is already in the desired state'
            $script:Unchanged++
        }
        else
        {
            $url = 'https://management.azure.com/' + $logicApp.Id + '/' + $apiOperation + '?api-version=2016-06-01'
            $response = az rest --method post --uri $url 
            $script:Changed++
            Write-Host 'Logic App Changed'

            $logicApp = Get-AzLogicApp -ResourceGroupName $resourceGroupName -Name $logicAppName
            Write-Host ($logicApp | Format-List | Out-String)
        } 
                       
    }
}



Write-Host '================================='
Write-Host 'Starting the script to: ' $state
Write-Host '*********************************'

#Declare variables
$Total = 0
$Changed = 0
$Unchanged = 0

#Get each logic app in loop
Write-Host 'Looping logic apps in Resource Group: ' $resourcegroupName
Get-AzureRmResource -ResourceType "Microsoft.Logic/workflows" -ResourceGroupName $resourcegroupName | ForEach-Object {
ChangeLogicAppState -logicAppName $_.Name -desiredState $state  -resourceGroupName $resourcegroupName
$Total++

}

Write-Host 'Summary:'
Write-Host '#################################'
Write-Host 'Total:' $Total ' Changed:' $Changed  ' Unchanged:' $Unchanged
Write-Host '#################################'

Write-Host '*********************************'
Write-Host 'Ending the script.'
Write-Host '================================='


Solution 1:[1]

Okay so most of the function within your script is redundant since the functionality is already present within Set-AzLogicApp already.

https://docs.microsoft.com/en-us/powershell/module/az.logicapp/set-azlogicapp?view=azps-7.5.0#syntax

Below is the basic usage of Set-AzLogicApp

Set-AzLogicApp -ResourceGroupName $rg -Name $logicAppName -State Disabled

Get-AzLogicApp Name Filtering

Unfortunately Get-AzLogicApp doesn't allow you to filter for all logic apps by a particular name as it requires you to pass the mandatory resourceGroup and Name parameters.

What you can do though is utilise Get-AzResource cmdlet which does allow wildcard search of resources.

Get-AzResource -ResourceType 'Microsoft.Logic/workflows' -ResourceName testlog*

You can then use the output to disable the logic apps based on a displayName filter.

The below script has been simplified from the example you were looking at earlier by using the cmdlets from the Az.LogicApp module

param (
    [Parameter(Mandatory)]
    [string]$displayName,
    
    [Parameter(Mandatory)]
    [ValidateSet('Enabled', 'Disabled')]
    [string]$State
)

# This will return all logic apps that start with the name testlog*
$logicApp = Get-AzResource -ResourceType 'Microsoft.Logic/workflows' -ResourceName $displayName*

# Loop through all returned logic apps and set the state
foreach ($app in $logicApp) {
    'Attempting to update state of: {0}' -f $app.Name
    Set-AzLogicApp -ResourceGroupName $app.ResourceGroupName -Name $app.Name -State $State -Force
}

I would normally have just used the pipeline and done something like

$logicApp | Set-AzLogicApp -State Disabled

However this doesn't appear to work on the latest version of the module as Name parameter does not take pipeline input.

I raised an issue on the Azure PowerShell repo to see if that could be fixed in a future release:

https://github.com/Azure/azure-powershell/issues/18053

Solution 2:[2]

You can enable/disable it from the Azure portal as shown below, enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Explorer