Scripts/Disable-AzLogicAppsFromConfig.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
param(
    [string][Parameter(Mandatory = $true)]$ResourceGroupName,
    [string][Parameter(Mandatory = $true)]$DeployFileName,
    [string][Parameter(Mandatory = $false)]$ResourcePrefix = "",
    [string][Parameter(Mandatory = $false)]$EnvironmentName = "AzureCloud",
    [string][Parameter(Mandatory = $false)]$ApiVersion = "2016-06-01"
)

$Global:accessToken = "";
$Global:subscriptionId = "";

function ExecuteStopType() {
    [CmdletBinding()]
    param
    (
        [string][parameter(Mandatory = $true)]$ResourceGroupName,
        [string][parameter(Mandatory = $true)]$LogicAppName,
        [string][parameter(Mandatory = $true)]$stopType
    )
    BEGIN {
        Write-Host("> Executing StopType '$stopType' for Logic App '$LogicAppName' in resource group '$ResourceGroupName'")
        If ($stopType -Match "Immediate") {
            try {
                Disable-AzLogicApp -EnvironmentName $EnvironmentName -SubscriptionId $Global:subscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -ApiVersion $ApiVersion -AccessToken $Global:accessToken
            }
            catch {
                Write-Warning "Failed to disable $LogicAppName"
                $ErrorMessage = $_.Exception.Message
                Write-Warning "Error: $ErrorMessage"
            }            
        }
        ElseIf ($stopType -Match "None") {
            Write-Host "Executing Stop 'None' => peforming no stop"
        }
        else {
            Write-Warning "StopType '$stopType' has no known implementation, doing nothing.." 
        }        
    }
}

function ExecuteCheckType() {
    [CmdletBinding()]
    param
    (
        [string][parameter(Mandatory = $true)]$ResourceGroupName,
        [System.Array][parameter(Mandatory = $true)]$batch
    )
    BEGIN {
        Write-Host("> Executing CheckType '$($batch.checkType)' for batch '$($batch.description)' in resource group '$ResourceGroupName'")
        If ($batch.checkType -Match "NoWaitingOrRunningRuns") {
            Write-Host "Executing Check 'NoWaitingOrRunningRuns'"
            If ($batch.logicApps.Length -gt 0 ) {
                $batch.logicApps | ForEach-Object {
                    $logicApp = $_;
                    if($ResourcePrefix.Length -gt 0){
                        $logicApp = "$ResourcePrefix$_"
                    }

                    try {
                        $RunningRunsCount = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $logicApp -ErrorAction Stop | Where-Object Status -eq "Running" | Measure-Object | ForEach-Object { $_.Count }
                        $WaitingRunsCount = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $logicApp -ErrorAction Stop | Where-Object Status -eq "Waiting" | Measure-Object | ForEach-Object { $_.Count }
                        if ($RunningRunsCount -ne 0 -and $WaitingRunsCount -ne 0) {
                            while ($RunningRunsCount -ne 0 -and $WaitingRunsCount -ne 0) {
                                Write-Host "Logic App '$logicApp' has Running and/or Waiting Runs, waiting 10 seconds and checking again.."
                                Write-Host "Number of running runs: $RunningRunsCount"
                                Write-Host "Number of waiting runs: $WaitingRunsCount"
                                Start-Sleep -Second 10                               
                                $RunningRunsCount = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $logicApp -ErrorAction Stop | Where-Object Status -eq "Running" | Measure-Object | ForEach-Object { $_.Count }
                                $WaitingRunsCount = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $logicApp -ErrorAction Stop | Where-Object Status -eq "Waiting" | Measure-Object | ForEach-Object { $_.Count }
                                if ($RunningRunsCount -eq 0 -and $WaitingRunsCount -eq 0) {
                                    Write-Host("Found no more waiting or running runs for '$logicApp', executing stopType for Logic App")
                                    ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppName $logicApp -stopType $batch.stopType
                                }
                            }
                        }else{
                            Write-Host("Found no more waiting or running runs for '$logicApp', executing stopType for Logic App")
                            ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppName $logicApp -stopType $batch.stopType
                        }                    
                        Write-Host("> Check 'NoWaitingOrRunningRuns' executed successfully on '$logicApp'")   
                    }
                    catch {
                        Write-Warning "Failed to perform check 'NoWaitingOrRunningRuns' for $logicApp"
                        $ErrorMessage = $_.Exception.Message
                        Write-Warning "Error: $ErrorMessage"
                    }                         
                }
            }
            else {
                Write-Warning "No Logic Apps specified."
            }
        }
        ElseIf ($batch.checkType -Match "None") {
            Write-Host "Executing Check 'None' => peforming no check and executing stopType"
            $batch.logicApps | ForEach-Object {
                $logicApp = $_;
                if($ResourcePrefix.Length -gt 0){
                    $logicApp = "$ResourcePrefix$_"
                }
                ExecuteStopType -resourceGroupName $ResourceGroupName -LogicAppName $logicApp -stopType $batch.stopType
            }
        }
        else {
            Write-Warning "CheckType '$batch.checkType' has no known implementation, performing no check or stop on the Logic App.." 
        }
    }
}

$json = Get-Content $DeployFileName | Out-String | ConvertFrom-Json

if ($json -is [array]) {
    [array]::Reverse($json)
}

if($json.Length -gt 0){
    # Request accessToken in case the script contains records
    $token = Get-AzCachedAccessToken -AssignGlobalVariables
}

$json | ForEach-Object { 
    $batch = $_;    
    $batchDescription = $batch.description
    Write-Host("Executing batch: '$batchDescription'")
    Write-Host("====================================")
    #Call the ExecuteCheckType function which will call the ExecuteStopType function after a check for a Logic App has completed
    ExecuteCheckType -resourceGroupName $ResourceGroupName -batch $batch

    ## Wrap up
    Write-Host("-> Batch: '$batchDescription' has been executed")
    Write-Host("")
}