Unlock inline PowerShell capabilities to streamline Logic Apps workflows
The new inline PowerShell action, now in preview, adds flexibility to Logic Apps by enabling users to embed PowerShell scripts directly into workflows. This feature unlocks new possibilities for complex integrations, and I’m excited to demonstrate its potential with a bank reconciliation workflow demo.
What is the Inline PowerShell Action?
Just like the inline C# action recently released, the inline PowerShell action enables you to embed custom PowerShell scripts directly into your Logic Apps Standard workflows, allowing you to write and execute scripts within the workflow designer and eliminating the need for additional services. The action supports dynamic content from previous steps and returns the results from your PowerShell code for use in subsequent actions.
Key Benefits:
Management and cost: By embedding PowerShell scripts directly into the workflow, you reduce the complexity and cost associated with managing separate services.
Dedicated scripting Space: The action generates a dedicated .ps1 file, providing a personalized scripting environment.
Seamless Deployment: Scripts are deployed alongside your workflow, ensuring smooth and efficient execution.
Support for public and private modules: The inline PowerShell action supports up to 10 public modules from the PowerShell Gallery, such as Az or SqlServer, by enabling managed dependencies. Additionally, you can create and use private modules within your Logic Apps for custom functionality, improving code reuse and workflow maintainability.
Demonstrating the Power of Inline PowerShell: A Bank Reconciliation Workflow
To illustrate the capabilities of this new feature, I’ve developed a bank reconciliation workflow. This demo showcases how to automate the bank reconciliation process by starting and deallocating virtual machines (VMs) using inline PowerShell scripts. The workflow is triggered on a recurring schedule, where it starts the VM, checks for the necessary files, and uploads them to storage. If the files are not found, the workflow automatically notifies the relevant team to address the issue. Here is the sample demo on GitHub to try out.
Pre-requisites:
Azure VM with RDP set up. Read more about setting it up here
Azure Storage account with a configured blob
The Workflow Breakdown:
Here is what the workflow would look like:
Trigger the Workflow (Recurrence): The process starts with a scheduled trigger that initiates the bank reconciliation workflow at regular intervals. In our case, it is set to every day given new bank statements from partner bank are uploaded to the server every day for processing
Start the VM with PowerShell: The inline PowerShell action is used to start a virtual machine, setting up the necessary environment for the reconciliation tasks. Here is what my script looks like
Note: Do not forget to add “Connect-AzAccount -Identity” so managed identity is configured, and Logic App can access the VM.
List Files in a Folder: Once the VM is up and running, the workflow lists all files and subfolders in a specified directory, checking for the presence of necessary reconciliation files in defined folder path of the server.
Iterate Over Files (For Each Loop): The workflow loops through each file found in the folder to process them individually. Using the token picker, select the “Folder items” parameter.
Condition Check: A condition is evaluated to determine if the required files are present:
True Path: If the files are found, the workflow retrieves the file content and uploads it to a storage container for further processing or archival.
Use the File System connector’s Get file content operation to retrieve each file and its contents
Upload the file to storage account for the parent bank to process from
False Path: If the files are not found, the workflow sends an email notification to alert the appropriate team members that the files were not uploaded to the server by the partner bank.
Deallocate the VM with PowerShell: After completing the file processing, another inline PowerShell action deallocates the VM to optimize resource usage and reduce costs.
Running the workflow
The workflow will be triggered automatically every day. Keep in mind to:
Upload bank statements to the file share folder of the VM so the workflow can pick them up every day.
From the requirements.psd1 file, comment out the ‘Az’ versioning line to ensure that the script can use Az module with appropriate version. You can find this file using Advanced Tools from Poral in Kudu. More instructions can be found here.
Considerations and Limitations
While the inline PowerShell action is powerful, there are some considerations and limitations to keep in mind:
Script Run Duration: The maximum duration is 10 minutes. For longer scenarios, consider providing feedback to Microsoft for extended support.
Output Size: The output size is limited to 100 MB.
Script Management: The script is saved as a .ps1 file within the workflow folder and can be reused in other workflows by copying the file from the KuduPlus console.
Conclusion
The introduction of the inline PowerShell action in Azure Logic Apps is a significant enhancement, enabling more advanced and integrated workflows. Whether you need to manage Azure resources, perform administrative tasks, or automate complex processes, this new capability provides the tools you need.
For more detailed guidance, you can refer to the official documentation on adding and running PowerShell scripts in Azure Logic Apps.
Microsoft Tech Community – Latest Blogs –Read More