8

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
  • 48,568
  • 9
  • 60
  • 89

5 Answers5

11

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

mickl
  • 48,568
  • 9
  • 60
  • 89
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
Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • 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 ! – mickl Nov 22 '18 at 06:10
  • @mickl Glad to know it is helpful.;-) – Joy Wang Nov 22 '18 at 06:11
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.

r3verse
  • 1,000
  • 8
  • 19
2

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

Dávid Šilon
  • 126
  • 1
  • 5
  • 1
    This solution appears to require a paid license from a 3rd party, Ultima. See: [ms documentation](https://learn.microsoft.com/en-us/connectors/iaconnectdynamiccode/) – L Robin Jan 11 '22 at 00:14
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.

Stefan Stranger
  • 130
  • 2
  • 10