Skip to content

Commit 5adbaa4

Browse files
authored
Merge pull request #174028 from ShawnJackson/automation-child-runbooks
edit pass: automation-child-runbooks
2 parents 84a61c5 + 75be797 commit 5adbaa4

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed
Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Create modular runbooks in Azure Automation
3-
description: This article tells how to create a runbook that is called by another runbook.
3+
description: This article explains how to create a runbook that another runbook calls.
44
services: automation
55
ms.subservice: process-automation
66
ms.date: 09/22/2021
@@ -11,81 +11,84 @@ ms.custom: devx-track-azurepowershell
1111

1212
# Create modular runbooks in Automation
1313

14-
It's a recommended practice in Azure Automation to write reusable, modular runbooks with a discrete function that is called by other runbooks. A parent runbook often calls one or more child runbooks to perform required functionality.
14+
It's a good practice in Azure Automation to write reusable, modular runbooks with a discrete function that other runbooks call. A parent runbook often calls one or more child runbooks to perform required functionality.
1515

16-
There are two ways to call a child runbook, and there are distinct differences that you should understand to determine which is best for your scenario(s). The following table summarizes the differences between the two ways to call one runbook from another.
16+
There are two ways to call a child runbook: inline or through a cmdlet. The following table summarizes the differences to help you decide which way is better for your scenarios.
1717

1818
| | Inline | Cmdlet |
1919
|:--- |:--- |:--- |
2020
| **Job** |Child runbooks run in the same job as the parent. |A separate job is created for the child runbook. |
21-
| **Execution** |Parent runbook waits for the child runbook to complete before continuing. |Parent runbook continues immediately after child runbook is started *or* parent runbook waits for the child job to finish. |
22-
| **Output** |Parent runbook can directly get output from child runbook. |Parent runbook must retrieve output from child runbook job *or* parent runbook can directly get output from child runbook. |
23-
| **Parameters** |Values for the child runbook parameters are specified separately and can use any data type. |Values for the child runbook parameters have to be combined into a single hashtable. This hashtable can only include simple, array, and object data types that use JSON serialization. |
24-
| **Automation Account** |Parent runbook can only use child runbook in the same Automation account. |Parent runbooks can use a child runbook from any Automation account, from the same Azure subscription, and even from a different subscription to which you have a connection. |
25-
| **Publishing** |Child runbook must be published before parent runbook is published. |Child runbook is published anytime before parent runbook is started. |
21+
| **Execution** |The parent runbook waits for the child runbook to finish before continuing. |The parent runbook continues immediately after the child runbook is started, *or* the parent runbook waits for the child job to finish. |
22+
| **Output** |The parent runbook can directly get output from the child runbook. |The parent runbook must retrieve output from the child runbook job, *or* the parent runbook can directly get output from the child runbook. |
23+
| **Parameters** |Values for the child runbook parameters are specified separately and can use any data type. |Values for the child runbook parameters have to be combined into a single hashtable. This hashtable can include only simple, array, and object data types that use JSON serialization. |
24+
| **Automation Account** |The parent runbook can use only a child runbook in the same Automation account. |Parent runbooks can use a child runbook from any Automation account, from the same Azure subscription, and even from a different subscription to which you have a connection. |
25+
| **Publishing** |A child runbook must be published before the parent runbook is published. |A child runbook is published anytime before the parent runbook is started. |
2626

27-
## Invoke a child runbook using inline execution
27+
## Call a child runbook by using inline execution
2828

29-
To invoke a runbook inline from another runbook, use the name of the runbook and provide values for its parameters, just like you would use an activity or a cmdlet. All runbooks in the same Automation account are available to all others to be used in this manner. The parent runbook waits for the child runbook to complete before moving to the next line, and any output returns directly to the parent.
29+
To call a runbook inline from another runbook, use the name of the runbook and provide values for its parameters, just like you would use an activity or a cmdlet. All runbooks in the same Automation account are available to all others to be used in this way. The parent runbook waits for the child runbook to finish before moving to the next line, and any output returns directly to the parent.
3030

31-
When you invoke a runbook inline, it runs in the same job as the parent runbook. There's no indication in the job history of the child runbook. Any exceptions and any stream outputs from the child runbook are associated with the parent. This behavior results in fewer jobs and makes them easier to track and to troubleshoot.
31+
When you call a runbook inline, it runs in the same job as the parent runbook. There's no indication in the job history of the child runbook. Any exceptions and any stream outputs from the child runbook are associated with the parent. This behavior results in fewer jobs and makes them easier to track and to troubleshoot.
3232

33-
When a runbook is published, any child runbooks that it calls must already be published. The reason is that Azure Automation builds an association with any child runbooks when it compiles a runbook. If the child runbooks haven't already been published, the parent runbook appears to publish properly but generates an exception when it's started. If this happens, you can republish the parent runbook to properly reference the child runbooks. You don't need to republish the parent runbook if any child runbook is changed because the association has already been created.
33+
When a runbook is published, any child runbooks that it calls must already be published. The reason is that Azure Automation builds an association with any child runbooks when it compiles a runbook. If the child runbooks haven't already been published, the parent runbook appears to publish properly but generates an exception when it's started.
3434

35-
The parameters of a child runbook called inline can be of any data type, including complex objects. There's no [JSON serialization](start-runbooks.md#work-with-runbook-parameters), as there is when you start the runbook using the Azure portal or with the [Start-AzAutomationRunbook](/powershell/module/Az.Automation/Start-AzAutomationRunbook) cmdlet.
35+
If you get an exception, you can republish the parent runbook to properly reference the child runbooks. You don't need to republish the parent runbook if any child runbook is changed because the association has already been created.
3636

37-
### Runbook types
37+
The parameters of a child runbook called inline can be of any data type, including complex objects. There's no [JSON serialization](start-runbooks.md#work-with-runbook-parameters), as there is when you start the runbook by using the Azure portal or by using the [Start-AzAutomationRunbook](/powershell/module/Az.Automation/Start-AzAutomationRunbook) cmdlet.
3838

39-
Which runbook types can call each other?
39+
### Runbook types
4040

41-
* A [PowerShell runbook](automation-runbook-types.md#powershell-runbooks) and a [graphical runbook](automation-runbook-types.md#graphical-runbooks) can call each other inline, as both are PowerShell-based.
42-
* A [PowerShell Workflow runbook](automation-runbook-types.md#powershell-workflow-runbooks) and a graphical PowerShell Workflow runbook can call each other inline, as both are PowerShell Workflow-based.
43-
* The PowerShell types and the PowerShell Workflow types can't call each other inline, and must use `Start-AzAutomationRunbook`.
41+
Only certain runbook types can call each other:
4442

45-
When does publish order matter?
43+
* A [PowerShell runbook](automation-runbook-types.md#powershell-runbooks) and a [graphical runbook](automation-runbook-types.md#graphical-runbooks) can call each other inline, because both are PowerShell based.
44+
* A [PowerShell Workflow runbook](automation-runbook-types.md#powershell-workflow-runbooks) and a graphical PowerShell Workflow runbook can call each other inline, because both are PowerShell Workflow based.
45+
* The PowerShell types and the PowerShell Workflow types can't call each other inline. They must use `Start-AzAutomationRunbook`.
4646

47-
The publish order of runbooks only matters for PowerShell Workflow and graphical PowerShell Workflow runbooks.
47+
The publish order of runbooks matters only for PowerShell Workflow and graphical PowerShell Workflow runbooks.
4848

49-
When your runbook calls a graphical or PowerShell Workflow child runbook using inline execution, it uses the name of the runbook. The name must start with `.\\` to specify that the script is located in the local directory.
49+
When your runbook calls a graphical or PowerShell Workflow child runbook by using inline execution, it uses the name of the runbook. The name must start with `.\\` to specify that the script is in the local directory.
5050

5151
### Example
5252

53-
The following example starts a test child runbook that accepts a complex object, an integer value, and a boolean value. The output of the child runbook is assigned to a variable. In this case, the child runbook is a PowerShell Workflow runbook.
53+
The following example starts a test child runbook that accepts a complex object, an integer value, and a Boolean value. The output of the child runbook is assigned to a variable. In this case, the child runbook is a PowerShell Workflow runbook.
5454

5555
```powershell
5656
$vm = Get-AzVM -ResourceGroupName "LabRG" -Name "MyVM"
5757
$output = PSWF-ChildRunbook -VM $vm -RepeatCount 2 -Restart $true
5858
```
5959

60-
Here's the same example using a PowerShell runbook as the child.
60+
Here's the same example but using a PowerShell runbook as the child.
6161

6262
```powershell
6363
$vm = Get-AzVM -ResourceGroupName "LabRG" -Name "MyVM"
6464
$output = .\PS-ChildRunbook.ps1 -VM $vm -RepeatCount 2 -Restart $true
6565
```
6666

67-
## Start a child runbook using a cmdlet
67+
## Start a child runbook by using a cmdlet
6868

6969
> [!IMPORTANT]
70-
> If your runbook invokes a child runbook with the `Start-AzAutomationRunbook` cmdlet with the `Wait` parameter and the child runbook produces an object result, the operation might encounter an error. To work around the error, see [Child runbooks with object output](troubleshoot/runbooks.md#child-runbook-object) to learn how to implement the logic to poll for the results using the [Get-AzAutomationJobOutputRecord](/powershell/module/az.automation/get-azautomationjoboutputrecord) cmdlet.
70+
> If your runbook calls a child runbook by using the `Start-AzAutomationRunbook` cmdlet with the `Wait` parameter and the child runbook produces an object result, the operation might encounter an error. To work around the error, see [Child runbooks with object output](troubleshoot/runbooks.md#child-runbook-object). That article shows you how to implement the logic to poll for the results by using the [Get-AzAutomationJobOutputRecord](/powershell/module/az.automation/get-azautomationjoboutputrecord) cmdlet.
71+
72+
You can use `Start-AzAutomationRunbook` to start a runbook, as described in [Start a runbook with Windows PowerShell](start-runbooks.md#start-a-runbook-with-powershell). There are two modes of use for this cmdlet:
7173

72-
You can use `Start-AzAutomationRunbook` to start a runbook as described in [To start a runbook with Windows PowerShell](start-runbooks.md#start-a-runbook-with-powershell). There are two modes of use for this cmdlet. In one mode, the cmdlet returns the job ID when the job is created for the child runbook. In the other mode, which your script enables by specifying the *Wait* parameter, the cmdlet waits until the child job finishes and returns the output from the child runbook.
74+
- The cmdlet returns the job ID when the job is created for the child runbook.
75+
- The cmdlet waits until the child job finishes and returns the output from the child runbook. Your script enables this mode by specifying the `Wait` parameter.
7376

74-
The job from a child runbook started with a cmdlet runs separately from the parent runbook job. This behavior results in more jobs than starting the runbook inline, and makes the jobs more difficult to track. The parent can start more than one child runbook asynchronously without waiting for each to complete. For this parallel execution calling the child runbooks inline, the parent runbook must use the [parallel keyword](automation-powershell-workflow.md#use-parallel-processing).
77+
The job from a child runbook started with a cmdlet runs separately from the parent runbook job. This behavior results in more jobs than starting the runbook inline, and makes the jobs more difficult to track. The parent can start more than one child runbook asynchronously without waiting for each to finish. For this parallel execution calling the child runbooks inline, the parent runbook must use the [parallel keyword](automation-powershell-workflow.md#use-parallel-processing).
7578

76-
Child runbook output doesn't return to the parent runbook reliably because of timing. Also, variables such as `$VerbosePreference`, `$WarningPreference`, and others might not be propagated to the child runbooks. To avoid these issues, you can start the child runbooks as separate Automation jobs using `Start-AzAutomationRunbook` with the `Wait` parameter. This technique blocks the parent runbook until the child runbook is complete.
79+
Child runbook output doesn't return to the parent runbook reliably because of timing. Also, `$VerbosePreference`, `$WarningPreference`, and other variables might not be propagated to the child runbooks. To avoid these problems, you can start the child runbooks as separate Automation jobs by using `Start-AzAutomationRunbook` with the `Wait` parameter. This technique blocks the parent runbook until the child runbook is finished.
7780

78-
If you don't want the parent runbook to be blocked on waiting, you can start the child runbook using `Start-AzAutomationRunbook` without the `Wait` parameter. In this case, your runbook must use [Get-AzAutomationJob](/powershell/module/az.automation/get-azautomationjob) to wait for job completion. It must also use [Get-AzAutomationJobOutput](/powershell/module/az.automation/get-azautomationjoboutput) and [Get-AzAutomationJobOutputRecord](/powershell/module/az.automation/get-azautomationjoboutputrecord) to retrieve the results.
81+
If you don't want the parent runbook to be blocked on waiting, you can start the child runbook by using `Start-AzAutomationRunbook` without the `Wait` parameter. In this case, your runbook must use [Get-AzAutomationJob](/powershell/module/az.automation/get-azautomationjob) to wait for job completion. It must also use [Get-AzAutomationJobOutput](/powershell/module/az.automation/get-azautomationjoboutput) and [Get-AzAutomationJobOutputRecord](/powershell/module/az.automation/get-azautomationjoboutputrecord) to retrieve the results.
7982

80-
Parameters for a child runbook started with a cmdlet are provided as a hashtable, as described in [Runbook parameters](start-runbooks.md#work-with-runbook-parameters). Only simple data types can be used. If the runbook has a parameter with a complex data type, then it must be called inline.
83+
Parameters for a child runbook started with a cmdlet are provided as a hashtable, as described in [Runbook parameters](start-runbooks.md#work-with-runbook-parameters). You can use only simple data types. If the runbook has a parameter with a complex data type, it must be called inline.
8184

82-
The subscription context might be lost when starting child runbooks as separate jobs. For the child runbook to execute Az module cmdlets against a specific Azure subscription, the child must authenticate to this subscription independently of the parent runbook.
85+
The subscription context might be lost when you're starting child runbooks as separate jobs. For the child runbook to execute Az module cmdlets against a specific Azure subscription, the child must authenticate to this subscription independently of the parent runbook.
8386

8487
If jobs within the same Automation account work with more than one subscription, selecting a subscription in one job can change the currently selected subscription context for other jobs. To avoid this situation, use `Disable-AzContextAutosave -Scope Process` at the beginning of each runbook. This action only saves the context to that runbook execution.
8588

8689
### Example
8790

88-
The following example starts a child runbook with parameters and then waits for it to complete using the `Start-AzAutomationRunbook` cmdlet with the `Wait` parameter. Once completed, the example collects cmdlet output from the child runbook. To use `Start-AzAutomationRunbook`, the script must authenticate to your Azure subscription.
91+
The following example starts a child runbook with parameters and then waits for it to finish by using the `Start-AzAutomationRunbook` cmdlet with the `Wait` parameter. After the child runbook finishes, the example collects cmdlet output from the child runbook. To use `Start-AzAutomationRunbook`, the script must authenticate to your Azure subscription.
8992

9093
```powershell
9194
# Ensure that the runbook does not inherit an AzContext
@@ -115,4 +118,4 @@ If you want the runbook to execute with the system-assigned managed identity, le
115118
## Next steps
116119

117120
* To run your runbook, see [Start a runbook in Azure Automation](start-runbooks.md).
118-
* For monitoring of runbook operation, see [Runbook output and messages in Azure Automation](automation-runbook-output-and-messages.md).
121+
* To monitor runbook operation, see [Runbook output and messages in Azure Automation](automation-runbook-output-and-messages.md).

articles/automation/automation-runbook-output-and-messages.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The following table briefly describes each stream with its behavior in the Azure
2525

2626
## Use the output stream
2727

28-
The output stream is used for the output of objects created by a script or workflow when it runs correctly. Azure Automation primarily uses this stream for objects to be consumed by parent runbooks that call the [current runbook](automation-child-runbooks.md). When a parent [calls a runbook inline](automation-child-runbooks.md#invoke-a-child-runbook-using-inline-execution), the child returns data from the output stream to the parent.
28+
The output stream is used for the output of objects created by a script or workflow when it runs correctly. Azure Automation primarily uses this stream for objects to be consumed by parent runbooks that call the [current runbook](automation-child-runbooks.md). When a parent [calls a runbook inline](automation-child-runbooks.md#call-a-child-runbook-by-using-inline-execution), the child returns data from the output stream to the parent.
2929

3030
Your runbook uses the output stream to communicate general information to the client only if it is never called by another runbook. As a best practice, however, you runbooks should typically use the [verbose stream](#write-output-to-verbose-stream) to communicate general information to the user.
3131

0 commit comments

Comments
 (0)