Azure Logic Apps: how to run Powershell script or Azure CLI?
Asked Answered
C

5

9

I'm building my Azure Logic Apps worklow which is supposed to check some conditions and run following Powershell:

Stop-AzureWebsiteJob -Name MyWebsite -JobName MyWebJob
Start-AzureWebsiteJob -Name MyWebsite -JobName MyWebJob -JobType Continuous

The question is: what's the easiest way to invoke such script in Azure Logic Apps? It seems like there's no built in block/connector for Powershell so I'd like to know what are the possibilites. Or perhaps it might be easier to run az CLI command with similar operation

Cowardly answered 21/11, 2018 at 14:51 Comment(0)
C
3

actually nowdays Azure provide this option, without creating runbooks and automation accounts. It is still in preview mode, but seems to be working !

enter image description here

Carven answered 1/12, 2021 at 11:33 Comment(1)
This solution appears to require a paid license from a 3rd party, Ultima. See: ms documentationEmlyn
C
12

Finally I ended up with a solution which takes advantage of Azure Automation. From Azure Portal we can create new Resource typing in Automation:

enter image description here

Once the resource is created we can add new Runbook under runbooks tab:

enter image description here

Runbook can run Powershell Workflow and get authorized using AzureRunAsConnection option (details here). My sample Powershell which is supposed to restart WebJob an specific App Service looks like below:

Workflow RestartMyWebJob
{
    $Conn = Get-AutomationConnection -Name AzureRunAsConnection
    Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

    $AzureContext = Select-AzureRmSubscription -SubscriptionId $Conn.SubscriptionID

    $Apiversion = "2015-08-01"
    $ResourceGroupName = 'My-Resource-Group-Name'
    $ResourceName = 'My-Resource-Group-Name/My-AppService--WebJob-Name'


    Invoke-AzureRmResourceAction -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName $ResourceName -Action stop -ApiVersion $Apiversion -Force
    Invoke-AzureRmResourceAction -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName $ResourceName -Action start -ApiVersion $Apiversion -Force
}

Having this Workflow setup we can run it from Azure Logic Apps by adding new block to our logic.

enter image description here

Cowardly answered 16/2, 2019 at 19:4 Comment(0)
U
7

Currently, azure logic seems not support to run powershell and cli script, here is a voice in azure feedback, you could vote it.

Workaround:

If you want to start and stop the webjob, you can call the Kudu WebJobs API in the logic app.

You can follow the steps below.

1.Run the powershell command locally to generate the Authorization token of your web app.

$creds = Invoke-AzureRmResourceAction -ResourceGroupName joywebapp -ResourceType Microsoft.Web/sites/config -ResourceName joywebapp2/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
$username = $creds.Properties.PublishingUserName
$password = $creds.Properties.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))

The $base64AuthInfo is what we need, it should be like JGpveXdlYmFwcDI6NnJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxzRktSdXlUcU5acUUzdFhNb05j.

The token will never be changed except you reset the publish profile, so you just need to do this step once.

2.In the logic app, specific the Method, URI, Headers(The header should be like Authorization: Basic JGpveXdlYmFwcDI6NnJxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxzRktSdXlUcU5acUUzdFhNb05j, note use space to separate the Basic and token), for example , I start a triggered webjob in my web app.

enter image description here

Triggered result:

enter image description here

So you just need to follow the steps above, for your issue, refer to the APIS:

  1. Start a continuous job
  2. Stop a continuous job
Unrestrained answered 22/11, 2018 at 3:4 Comment(2)
In the meantime I've used Azure Automation and Runbooks however your answer is a great alternative and definitely needs to be accepted, thanks a lot !Cowardly
@Cowardly Glad to know it is helpful.;-)Unrestrained
A
4

Create an Azure Function with an http trigger with Powershell as the function language (or any other supported language). Then you call the Function easily in the Logic app by calling an Http endpoint.

Allrud answered 29/10, 2020 at 19:12 Comment(0)
C
3

actually nowdays Azure provide this option, without creating runbooks and automation accounts. It is still in preview mode, but seems to be working !

enter image description here

Carven answered 1/12, 2021 at 11:33 Comment(1)
This solution appears to require a paid license from a 3rd party, Ultima. See: ms documentationEmlyn
S
0

You can also have your PowerShell code run in an Azure Container Instance supporting PowerShell and create an new Container Group from the Logic App workflow.

Slender answered 25/12, 2022 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.