-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogicAppUtility.ps1
More file actions
335 lines (284 loc) · 11.8 KB
/
LogicAppUtility.ps1
File metadata and controls
335 lines (284 loc) · 11.8 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<#
.SYNOPSIS
Copy the PowerShell script desired folder
Set up Azure Service Principal - Contributor access on the Subscription: https://blog.jongallant.com/2017/11/azure-rest-apis-postman/
Open PowerShell with 'Run As administrator Privileges'
Run the below command to bypass the execution policy and accept -Y
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
Change the directory to the PowerShell script copied folder in first step.
cd 'PowerShellScriptFolderPath'
Execute the command to perform the different mentioned operations on Logic App
.\LogicAppUtility.ps1 -ClientId 'Enter ClientId' -TenantId 'Enter TenantId' -Secret 'Enter Secret' -SubscriptionId 'Enter Subscription Id' -ResourceGroupName 'Enter resource Group Name' -LogicAppName 'Enter Logic App Name' -Operation 'BulkCancel' -StartTime '2020-11-02T16:33:00.000Z' -EndTime '2020-11-02T16:38:00.000Z’
Validate the text file gets created in the same folder for list of run ids executed.
.DESCRIPTION:
Manages below Logic App operations:
1.BulkCancel - Cancel running instances
2.BulkResubmitFailedRuns - Resubmits failed runs
3.BulkResubmitCancelledRuns - Resubmits cancelled runs
4.BulkResubmitSucceededRuns - Resubmits Succeeded runs
.PARAMETERs
S.No Parameter Name Mandatory Comments
1 Client Id No The application /Client Id of your App Service Principal
2 Tenant Id No Tenant Id of Azure AD or App Service Principal
3 Client Secret No Secret of your App Service Principal
4 Subscription Id Yes Subscription Id where Logic App present
5 ResourceGroupName Yes Resource group Name in which Logic App is present
6 Logic App Name Yes Name of your Logic App
7 Operation Yes Allowed Values are
• BulkCancel - Cancel running instances
• BulkResubmitFailedRuns - Resubmits failed runs
• BulkResubmitCancelledRuns - Resubmits cancelled runs
• BulkResubmitSucceededRuns - Resubmits Succeeded runs
8 StartTime No If present all above operations will be performed on the runs started from this time.
The Timestamp must be in UTC.
Ex: 2020-11-02T16:33:00.000Z
9 EndTime No You can include the EndTime along with StartTime if you want to perform above operations between specific timestamps.
Note:
It is invalid without StartTime.
.OUTPUTS
Log file gets created in PowqerShell script folder
.NOTES
Author: Veera Reddy Gangala
Creation Date: 2020-11-04
Purpose/Change: Initial script development
.EXAMPLE
.\LogicAppUtility.ps1 -ClientId 'Enter ClientId' -TenantId 'Enter TenantId' -Secret 'Enter Secret' -SubscriptionId 'Enter Subscription Id' -ResourceGroupName 'Enter resource Group Name' -LogicAppName 'Enter Logic App Name' -Operation 'BulkCancel' -StartTime '2020-11-02T16:33:00.000Z' -EndTime '2020-11-02T16:38:00.000Z’
Validate the text file gets created in the same folder for list of run ids executed.
#>
param ([Parameter(Mandatory=$false)] $ClientId ,[Parameter(Mandatory=$false)] $TenantId,[Parameter(Mandatory=$false)] $Secret,[Parameter(Mandatory=$true)] $SubscriptionId,[Parameter(Mandatory=$true)] $ResourceGroupName,[Parameter(Mandatory=$true)] $LogicAppName, [Parameter(Mandatory=$false)] $StartTime , [Parameter (Mandatory=$true)] $Operation,[Parameter (Mandatory=$false)] $EndTime )
#. OAuth Token Function
function GetOAuthToken
{
param( [string]$TenantId,[string] $ClientId, [String]$Secret)
#Get OAuth token using Service Principal
try
{
$url='https://login.microsoftonline.com/'+$TenantId+'/oauth2/token'
$body='grant_type=client_credentials&client_id='+$ClientId+'&resource=https://management.azure.com&client_secret='+$Secret
$ContentType='application/x-www-form-urlencoded'
#Call OAUth URL for token
$result= Invoke-RestMethod -Method 'POST' -Uri $url -Body $body -ContentType $ContentType
$OAuthtoken=$result.access_token
Write-Output $OAuthtoken
}
catch
{
New-Item '.\ErrorFile.txt'
Add-Content '.\ErrorFile.txt' "An error occured while generating OAuth token, error: $($PSItem.ToString())"
throw "An error occured while generating OAuth token, error: $($PSItem.ToString())"
}
}
#Get Run Ids of Logic App for given Criteria
function GetRunIds
{
param( [parameter (Mandatory= $true)] $Headers, [parameter (Mandatory= $false)] $nextlink, [parameter (Mandatory= $false)] $OperationName , [parameter (Mandatory= $false)] $StartTime )
try
{
if($nextlink -eq $null)
{
$TaskName
if($OperationName -eq 'BulkResubmitFailedRuns')
{
$TaskName='Failed'
}
elseIf($OperationName -eq 'BulkCancel')
{
$TaskName='Running'
}
elseIf($OperationName -eq 'BulkResubmitCancelledRuns')
{
$TaskName='Cancelled'
}
elseif($OperationName -eq 'BulkResubmitSucceededRuns')
{
$TaskName='Succeeded'
}
else
{
write-host Invalid Operation Name: Allowed values are [BulkResubmitFailedRuns/BulkResubmitCancelledRuns/BulkCancel/BulkResubmitSucceededRuns] -ForegroundColor Red
Exit $true
}
if(($StartTime -ne $null) -and ($EndTime -ne $null))
{
$url='https://management.azure.com/subscriptions/'+$SubscriptionId+'/resourceGroups/'+$ResourceGroupName+'/providers/Microsoft.Logic/workflows/'+$LogicAppName+'/runs?api-version=2016-06-01&$filter=(Status eq '''+$TaskName+''' and startTime ge '+$StartTime+' and startTime le '+$EndTime+' )'
}
elseif(($StartTime -ne $null) -and ($EndTime -eq $null))
{
$url='https://management.azure.com/subscriptions/'+$SubscriptionId+'/resourceGroups/'+$ResourceGroupName+'/providers/Microsoft.Logic/workflows/'+$LogicAppName+'/runs?api-version=2016-06-01&$filter=(Status eq '''+$TaskName+''' and startTime ge '+$StartTime+')'
}
else
{
$url='https://management.azure.com/subscriptions/'+$SubscriptionId+'/resourceGroups/'+$ResourceGroupName+'/providers/Microsoft.Logic/workflows/'+$LogicAppName+'/runs?api-version=2016-06-01&$filter=(Status eq '''+$TaskName+''')'
}
$result= Invoke-RestMethod -Method 'GET' -Uri $url -Headers $headers
}
else
{
$result= Invoke-RestMethod -Method 'GET' -Uri $nextlink -Headers $headers
}
Write-Output $result
}
Catch
{
New-Item '.\ErrorFile.txt'
Add-Content '.\ErrorFile.txt' "An error occured while fecthing the RunIds with error: $($PSItem.ToString())"
throw "An error occured while fecthing the RunIds with error: $($PSItem.ToString())"
}
}
#Cance Run
function CancelRun
{
param ($headers, $SubscriptionId, $ResourceGroupName, $LogicAppName, $RunId, $Filepath , $RunIdStartTime)
try
{
$cancelrunurl= 'https://management.azure.com/subscriptions/'+$SubscriptionId+'/resourceGroups/'+$ResourceGroupName+'/providers/Microsoft.Logic/workflows/'+$LogicAppName+'/runs/'+$RunId+'/cancel?api-version=2016-06-01'
$output=Invoke-RestMethod -Method 'POST' -Uri $cancelrunurl -Headers $headers
Add-Content $Filepath "$RunId $RunIdStartTime"
}
catch
{
Add-Content $Filepath "Error occured while cancelling the run: $RunId, StartTime:$RunIdStartTime with error: $($PSItem.ToString())"
throw "Error occured while cancelling the run $RunId with error: $($PSItem.ToString())"
}
}
#Resibmit the specified Run
function ResubmitRun
{
param ($headers, $SubscriptionId, $ResourceGroupName, $LogicAppName,$RunId, $Filepath , $RunIdStartTime)
try
{
$Resubmiturl= 'https://management.azure.com/subscriptions/'+$SubscriptionId+'/resourceGroups/'+$ResourceGroupName+'/providers/Microsoft.Logic/workflows/'+$LogicAppName+'/triggers/'+$TriggerName+'/histories/'+$RunId+'/resubmit?api-version=2016-06-01'
$output= Invoke-RestMethod -Method 'POST' -Uri $Resubmiturl -Headers $headers
Add-Content $Filepath "$RunId $RunIdStartTime"
}
catch
{
Add-Content $Filepath "An error occured while Resubmitting the RunId : $RunId, StartTime:$RunIdStartTime with error: $($PSItem.ToString())"
throw "An error occured while Resubmitting the RunId $RunId with error: $($PSItem.ToString())"
}
}
try
{
Write-Host 'Do you want to use User sign-in or Azure AD App registration details for authentication: Select 1 -for user sign-in and Select 2 -for App resgitration details' -ForegroundColor White
$confirmation = Read-Host " Confirm [1/2]"
if($confirmation -eq 1)
{
az login
$token = az account get-access-token
$OAuthtoken = az account get-access-token --query 'accessToken' -o tsv
}
elseif ($confirmation -eq 2)
{
$OAuthtoken= GetOAuthToken -TenantId $TenantId -ClientId $ClientId -Secret $Secret
}
else
{
write-host Invalid Operation Name: Allowed values are [BulkResubmitFailedRuns/BulkResubmitCancelledRuns/BulkCancel/BulkResubmitSucceededRuns] -ForegroundColor Red
Exit $true
}
# Construct Header for Authorization
$headers = @{
'Authorization' = 'Bearer '+$OAuthtoken}
#Set dummy for the first time to get in loop, however this is handled within loop to construct URl
$nextlink ='Dummy'
$i=0
$Filename
do
{
# Check if first iteration to make intial REST call.
if($i -eq 0)
{
$result=GetRunIds -Headers $headers -OperationName $Operation -StartTime $StartTime -nextlink $null
}
else
{
$result= GetRunIds -Headers $headers -nextlink $nextlink
}
$nextlink=$result.nextLink
#####Check If it is Bulk Cancel operation
if(($result.Value | Measure-Object).Count -ge 1)
{
if($Operation -eq 'BulkCancel')
{
if($i -eq 0)
{
Write-Host 'Confirm -Are you sure you want to perform this operation:'$Operation ',it cancels the running instances of the Logic App:'$LogicAppName 'as per the specified criteria..[Y] Yes,[N] No' -ForegroundColor White
$confirmation = Read-Host " Confirm [Y/N]"
}
#Verify if there are any running instances
####Cancel runs retrieved running runs
if($confirmation -eq "Y")
{
if($i -eq 0)
{
$Filename=".\Cancelled_RunIds_$(Get-Date -Format 'yyyyMMdd.HHmmss').txt"
New-Item $Filename
Write-Host $Operation 'is in progress. Please wait till it gets completed' -ForegroundColor Green
$i=1
}
foreach($Run in $result.Value)
{
CancelRun -headers $headers -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -RunId $Run.name -Filepath $Filename -RunIdStartTime $Run.properties.startTime
}
}
else
{
write-host 'You have disagreed to perform the operation:' $Operation -ForegroundColor Yellow
exit $true
}
}
####Executing Bulk Resbmit operation
else
{
if($i -eq 0)
{
Write-Host 'Confirm -Are you sure you want to perform this Operation' $Operation ',it resubmits runs as per specified Criteria..[Y] Yes,[N] No ' -ForegroundColor White
$confirmation = Read-Host " Confirm [Y/N]"
}
#Verify if there are any running instances
if(($result.Value | Measure-Object).Count -ge 1)
{
# Resubmit runs retrieved failed run Ids
if($confirmation -eq "Y")
{
if($i -eq 0)
{
$Filename=".\Resubmitted_RunIds_$(Get-Date -Format 'yyyyMMdd.HHmmss').txt"
New-Item $FileName
Write-Host $Operation 'is in progress.Please wait till it gets completed' -ForegroundColor Green
$i=1
}
foreach($Run in $result.Value)
{
$TriggerName= $Run.properties.trigger.name
ResubmitRun -headers $headers -SubscriptionId $SubscriptionId -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -RunId $Run.name -Filepath $Filename -RunIdStartTime $Run.properties.startTime
}
}
else
{
Write-Host 'You have disagreed to perform the operation' -ForegroundColor Yellow
Exit $true
}
}
else
{
Write-Host 'No Runs found for the specified operation:' $Operation ' and LogicApp Name:'$LogicAppName -ForegroundColor Yellow
}
}
}
else
{
if($i -eq 0)
{
Write-Host 'No Runs found for the specified operation:' $Operation ' and LogicApp Name:'$LogicAppName -ForegroundColor Yellow
}
}
if(($nextlink -eq $null) -and ($i -ne 0))
{
Write-Host 'The specified operation:'$Operation 'is succeeeded, you may verify it in the protal' -ForegroundColor Green
}
}While ($nextLink-ne $null)
}
catch{
Write-Host "An error occured while performing the specified Operaion $Operation. Error: $($PSItem.ToString())" -ForegroundColor Red
}