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

20,698

Solution 1

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

Solution 2

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

Solution 3

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

Solution 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.

Share:
20,698
mickl
Author by

mickl

Senior Technical Consultant Microsoft Certified Solutions Developer: Web Applications Microsoft Certified: Azure Solutions Architect "Debugging is like being the detective in a crime movie where you are also the murderer" - Filipe Fortes "There's nothing more permanent than a temporary hack." - Kyle Simpson "The strength of JavaScript is that you can do anything. The weakness is that you will." - Reg Braithwaite

Updated on December 02, 2021

Comments

  • mickl
    mickl over 2 years

    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

  • mickl
    mickl over 5 years
    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 !
  • Joy Wang
    Joy Wang over 5 years
    @mickl Glad to know it is helpful.;-)
  • L Robin
    L Robin over 2 years
    This solution appears to require a paid license from a 3rd party, Ultima. See: ms documentation