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

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


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


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


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