mickl
mickl

Reputation: 49945

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

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

Upvotes: 9

Views: 44385

Answers (5)

Stefan Stranger
Stefan Stranger

Reputation: 150

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.

Upvotes: 0

Dávid Šilon
Dávid Šilon

Reputation: 136

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

Upvotes: 3

r3verse
r3verse

Reputation: 1070

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.

Upvotes: 4

mickl
mickl

Reputation: 49945

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

Upvotes: 12

Joy Wang
Joy Wang

Reputation: 42043

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

Upvotes: 7

Related Questions