Hola! Today let’s look at a simple way to get PowerShell scripts to run from a Logic App. It will involve a single extra tool, but this really adds versatility to an already versatile tool.
Start by creating a PowerShell script for your specific task. This script will be uploaded to an Azure Automation Runbook. For instance, if you aim to manage VMs, ensure the script includes Azure RM or Az module commands to start, stop, or monitor VM states. Here is an example:
# Sample PowerShell Script to Start a Specific Azure VM Param( [string]$vmName, [string]$resourceGroupName ) Connect-AzAccount -Identity Start-AzVM -Name $vmName -ResourceGroupName $resourceGroupName
Obviously this is a short script that we can do with just Logic Apps (and not involve pwsh at all), but you get the point.
Now – Upload and publish your PowerShell script in an Azure Automation Runbook.
- In your Azure Automation Account, create a new Runbook.
- Choose “PowerShell” as the Runbook type.
- Import your script and publish the Runbook.
Go ahead test the runbook if you want.
Next – create a Logic App to trigger the Runbook. You might use a schedule, an HTTP request, or another event in Azure as a trigger.
- In the Logic App Designer, add a new step and search for the “Azure Automation” connector.
- Select “Create job” action.
- Fill in the necessary details: Automation Account, Runbook Name, and parameters (if your script requires them). In our example we might dynamically pass the VM name, or maybe look for only VMs that are off and loop through them.
For more complex scenarios, you might need to integrate with other Azure services before or after executing your PowerShell script:
- Azure Functions: For custom logic that cannot be implemented directly in PowerShell or needs a specific runtime environment.
- Azure Event Grid: To trigger your Logic App based on events from various Azure services.
- Azure Monitor: To analyze logs and metrics from your Logic App and Automation Runbooks, enabling proactive management and optimization of your automated tasks.
And there you go! Go put PowerShell everywhere!