Properly Disable Logic Apps in Powershell
  • 13 May 2020
  • 1 Minute to read
  • Contributors
  • Comment
  • Dark
    Light
  • PDF

Properly Disable Logic Apps in Powershell

  • Comment
  • Dark
    Light
  • PDF

Article Summary

Recently we needed to do a script to disable/enable some logic apps for maintenance activities to happen and we wanted to stop any message processing. If you search online then typically you will see this powershell script.

Set-AzLogicApp -ResourceGroupName "MyResourceGroup" -Name "MyLogicApp" -State "Disabled" -Force

On the face of it this seems pretty straightforward but we are using ISE and when I ran it I get a problem where it is complaining that we were trying to set the ISE environment to null. On investigation I was suspicious of what this actually does and I think this is really intended to support updating the logic app as a whole and was cautious of any side effects. For example if my Logic App has an integration account set and I use Set-AzLogicApp th default value for the integration account parameter is none. Does this mean it will remove my integration account association in the logic app?

There is an alternative was you can disable/enable the logic app using the Resource API so i used the below script which will combine Az Cli and Powershell to check the state and if its not in the desired state it will call the API to execute the disable operation. This seems a safer way to do this.

Using the below script I can just list the logic apps I need to manage in the maintenance window and update them to the desired state when needed.



$recourceGroupName = 'MyResourceGroup'
$desiredState = 'Disabled'


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

    $logicApp = Get-AzLogicApp -ResourceGroupName $resourceGroupName -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'
        }
        else
        {
            $url = 'https://management.azure.com/' + $logicApp.Id + '/' + $apiOperation + '?api-version=2016-06-01'
            $response = az rest --method post --uri $url 
            Write-Host 'Logic App Changed'

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


#Logic Apps with Recurrence Trigger
ChangeLogicAppState -logicAppName 'MyLogicApp' -desiredState $desiredState  -resourceGroupName $recourceGroupName

Was this article helpful?