PowerShell Workflow Runbooks
4 MIN READPowerShell Workflow runbooks are text runbooks based on Windows PowerShell Workflow. You directly edit the code of the runbook using the text editor in the Azure portal. You can also use any offline text editor and import the runbook into Azure Automation.
Advantages
- Implement all complex logic with PowerShell Workflow code.
- Use checkpoints to resume operation if there's an error.
- Use parallel processing to do multiple actions in parallel.
- Can include other graphical runbooks and PowerShell Workflow runbooks as child runbooks to create high-level workflows.
Limitations
- You must be familiar with PowerShell Workflow.
- Runbooks must deal with the additional complexity of PowerShell Workflow, such as deserialized objects.
- Runbooks take longer to start than PowerShell runbooks since they must be compiled before running.
- You can only include PowerShell runbooks as child runbooks by using the Start-AzAutomationRunbook cmdlet.
- Runbooks can't run on a Linux Hybrid Runbook Worker.
Prerequisites
- An Azure Automation account with at least one user-assigned managed identity.
- Az modules: Az.Accounts and Az.Compute imported into the Automation account.
- PowerShell/otherrequired modules imported into the Automation account. You can do this by performing following steps:
- Sign in to the Azure portal and navigate to your Automation account.
- Under Shared resources, select modules.
- Click on browse gallery
- Select + Add role assignment (Preview) to open the Add role assignment (Preview) page.
- Select Save, and then close the Azure role assignments page to return to the System assigned tab.
- Select the User assigned tab.
- Select your user-assigned managed identity from the list to open the ManagedIdentity page.
- Take note of the Client ID for later use.
- Using AD app registration : -
- Sign in to the Azure portal and navigate to AzureActive Directory.
- Under Manage, select App registration.
- Click on New registration, to open registration page.
- Input required details and select register.
- Select Create.
Add code to the runbook
You can either type code directly into the runbook, or you can select cmdlets, runbooks, and assets from the library control and add them to the runbook.
For example, we will try to connect SharePoint Online using App Registration.
- Select Overview and then Edit to open the textual editor.
- Here is another method to provide ClientID, Tenant and Thumbprint.(Most Secure)
- Navigate to Automation Account, under Shared Resources, select Credentials.
- When the runbook job completes, the Test page displays its output.
Publish and start the runbook
- Select Publish to publish the runbook and then Yes when prompted.
- The Status field now shows Published. Review the options across the top that allow you to start the runbook now, schedule a future start time, or create a webhook so that the runbook can be started through an HTTP call. Select Start and then Yes when prompted to start the runbook.
How to get file from SharePoint directory or any cloud storage
- For example, we are fetching file from local directory
- Search for the module you want to add
Methods to connect Automation account with resources:
- Using managed identities : -
- Sign in to the Azure portal and navigate to your Automation account.
- Under Account Settings, select Identity.
- From the System assigned tab, under Permissions, select Azure role assignments to open the Azure role assignments page.
- Now open the newly created app registration, under Manage select API permissions and give required permissions by selecting Add permission.
- Under Manage, select Certificates & secrets and upload certificate.
- Take note of the Thumbprint for later use.
Create new runbook
Start by creating a simple PowerShell Workflow runbook. One advantage of Windows PowerShell Workflows is the ability to perform a set of commands in parallel instead of sequentially as with a typical script.
- From your open Automation account page, under Process Automation, select Runbooks.
- Select + Create a runbook.
- Name the runbook. For example, test.
- From the Runbook type drop-down menu, select PowerShell Workflow.
- From the Runtimeversion drop-down, select 5.1.
- Enter applicable Description.
- Select Add Connection, input required details.
- Select Create.
- You can get this details using Get-AutomationConnection(See below cmd)
Test the Runbook
- Select Test pane to open the Test page.
- Select Start to start the test. A runbook job is created and its status is displayed in the pane.
- But in runbooks, we can’t fetch files from local directory so can put it on SharePoint directory or any cloud storage. For example, we have stored our file on SharePoint site – “/sites/AK/Shared Documents/SiteCreation/” then we can get it using Get-PnPFile.
- We can also store output in runbook directory using path as “.\filename”.
Table of Contents
ToggleContact Us