You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/automation/automation-child-runbooks.md
+42-40Lines changed: 42 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,111 +8,113 @@ ms.topic: conceptual
8
8
---
9
9
# Child runbooks in Azure Automation
10
10
11
-
It is a recommended practice in Azure Automation to write reusable, modular runbooks with a discrete function that is by other runbooks. A parent runbook often calls one or more child runbooks to perform required functionality. There are two ways to call a child runbook, and each has distinct differences that you should understand so that you can determine which is best for your different scenarios.
11
+
It is 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. There are two ways to call a child runbook, and there are distinct differences that you should understand so that you can determine which is best for your scenarios.
12
+
13
+
>[!NOTE]
14
+
>This article has been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see [Introducing the new Azure PowerShell Az module](https://docs.microsoft.com/powershell/azure/new-azureps-module-az?view=azps-3.5.0). For Az module installation instructions on your Hybrid Runbook Worker, see [Install the Azure PowerShell Module](https://docs.microsoft.com/powershell/azure/install-az-ps?view=azps-3.5.0). For your Automation account, you can update your modules to the latest version using [How to update Azure PowerShell modules in Azure Automation](automation-update-azure-modules.md).
12
15
13
16
## Invoking a child runbook using inline execution
14
17
15
-
To invoke a runbook inline from another runbook, you use the name of the runbook and provide values for its parameters exactly like you would use an activity or cmdlet. All runbooks in the same Automation account are available to all others to be used in this manner. The parent runbook will wait for the child runbook to complete before moving to the next line, and any output is returned directly to the parent.
18
+
To invoke a runbook inline from another runbook, you use the name of the runbook and provide values for its parameters exactly like you would use an activity or 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 is returned directly to the parent.
16
19
17
-
When you invoke a runbook inline, it runs in the same job as the parent runbook. There is no indication in the job history of the child runbook that it ran. Any exceptions and any stream output from the child runbook is associated with the parent. This behavior results in fewer jobs and makes them easier to track and to troubleshoot since any exceptions thrown by the child runbook and any of its stream outputs are associated with the parent job.
20
+
When you invoke a runbook inline, it runs in the same job as the parent runbook. There is 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.
18
21
19
-
When a runbook is published, any child runbooks that it calls must already be published. This is because Azure Automation builds an association with any child runbooks when a runbook is compiled. If they aren’t, the parent runbook appears to publish properly, but will generate an exception when it’s started. If this happens, you can republish the parent runbook to properly reference the child runbooks. You do not need to republish the parent runbook if any of the child runbooks are changed because the association has already been created.
22
+
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 have not already been published, the parent runbook appears to publish properly but generates an exception when it is started. If this happens, you can republish the parent runbook to properly reference the child runbooks. You do not need to republish the parent runbook if any child runbook is changed because the association has already been created.
20
23
21
-
The parameters of a child runbook called inline can be any data type including complex objects. There is no [JSON serialization](start-runbooks.md#runbook-parameters) as there is when you start the runbook using the Azure portal or with the Start-AzureRmAutomationRunbook cmdlet.
24
+
The parameters of a child runbook called inline can be of any data type, including complex objects. There is no [JSON serialization](start-runbooks.md#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.
22
25
23
26
### Runbook types
24
27
25
-
Which types can call each other:
28
+
Which runbook types can call each other?
26
29
27
-
* A [PowerShell runbook](automation-runbook-types.md#powershell-runbooks) and [Graphical runbooks](automation-runbook-types.md#graphical-runbooks) can call each other inline (both are PowerShellbased).
28
-
* A [PowerShell Workflow runbook](automation-runbook-types.md#powershell-workflow-runbooks) and Graphical PowerShell Workflow runbooks can call each other inline (both are PowerShell Workflowbased)
29
-
* The PowerShell types and the PowerShell Workflow types can’t call each other inline, and must use Start-AzureRmAutomationRunbook.
30
+
* 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.
31
+
* 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.
32
+
* The PowerShell types and the PowerShell Workflow types can’t call each other inline, and must use **Start-AzAutomationRunbook**.
30
33
31
-
When does publish order matter:
34
+
When does publish order matter?
32
35
33
-
*The publish order of runbooks only matters for PowerShell Workflow and Graphical PowerShell Workflow runbooks.
36
+
The publish order of runbooks only matters for PowerShell Workflow and Graphical PowerShell Workflow runbooks.
34
37
35
-
When you call a Graphical or PowerShell Workflow child runbook using inline execution, you use the name of the runbook. When you call a PowerShell child runbook, you must start its name with *.\\* to specify that the script is located in the local directory.
38
+
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.
36
39
37
40
### Example
38
41
39
-
The following example starts a test child runbook that accepts three parameters, a complex object, an integer, and a boolean. The output of the child runbook is assigned to a variable. In this case, the child runbook is a PowerShell Workflow runbook.
42
+
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.
> If you are invoking a child runbook with the `Start-AzureRmAutomationRunbook` cmdlet with the `-Wait` switch and the results of the child runbook is an object, you may encounter errors. 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 and use the [Get-AzureRmAutomationJobOutputRecord](/powershell/module/azurerm.automation/get-azurermautomationjoboutputrecord)
59
+
> 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.
57
60
58
-
You can use the [Start-AzureRmAutomationRunbook](/powershell/module/AzureRM.Automation/Start-AzureRmAutomationRunbook) cmdlet 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 child job is created for the child runbook. In the other mode, which you enable by specifying the **-wait** parameter, the cmdlet waits until the child job finishes and returns the output from the child runbook.
61
+
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 child 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.
59
62
60
-
The job from a child runbook started with a cmdlet runs in a separate job from the parent runbook. This behavior results in more jobs than starting the runbook inline and makes them more difficult to track. The parent can start more than one child runbook asynchronously without waiting for each to complete. For that same kind of parallel execution calling the child runbooks inline, the parent runbook would need to use the [parallel keyword](automation-powershell-workflow.md#parallel-processing).
63
+
The job from a child runbook started with a cmdlet runs in a separate job 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#parallel-processing).
61
64
62
-
The output of child runbooks aren't returned to the parent runbook reliably because of timing. Also certain variables like $VerbosePreference, $WarningPreference, and others may not be propagated to the child runbooks. To avoid these issues, you can start the child runbooks as separate Automation jobs using the `Start-AzureRmAutomationRunbook` cmdlet with the `-Wait` switch. This blocks the parent runbook until the child runbook is complete.
65
+
Child runbook output is not returned to the parent runbook reliably because of timing. In addition, 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.
63
66
64
-
If you don’t want the parent runbook to be blocked on waiting, you can start the child runbook using `Start-AzureRmAutomationRunbook` cmdlet without the `-Wait` switch. You would then need to use `Get-AzureRmAutomationJob` to wait for job completion, and `Get-AzureRmAutomationJobOutput` and `Get-AzureRmAutomationJobOutputRecord` to retrieve the results.
67
+
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.
65
68
66
-
Parameters for a child runbook started with a cmdlet are provided as a hashtable as described in [Runbook Parameters](start-runbooks.md#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.
69
+
Parameters for a child runbook started with a cmdlet are provided as a hashtable, as described in [Runbook Parameters](start-runbooks.md#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.
67
70
68
-
The subscription context might be lost when starting child runbooks as separate jobs. In order for the child runbook to execute Azure RM cmdlets against a specific Azure subscription, the child runbook must authenticate to this subscription independently of the parent runbook.
71
+
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 runbook must authenticate to this subscription independently of the parent runbook.
69
72
70
-
If jobs within the same Automation account work with more than one subscription, selecting a subscription in one job may change the currently selected subscription context for other jobs. To avoid this problem, use `Disable-AzureRmContextAutosave –Scope Processsave` at the beginning of each runbook. This action only saves the context to that runbook execution.
73
+
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.
71
74
72
75
### Example
73
76
74
-
The following example starts a child runbook with parameters and then waits for it to complete using the Start-AzureRmAutomationRunbook -wait parameter. Once completed, its output is collected from the child runbook. To use `Start-AzureRmAutomationRunbook`, you must authenticate to your Azure subscription.
77
+
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.
75
78
76
79
```azurepowershell-interactive
77
-
# Ensures you do not inherit an AzureRMContext in your runbook
78
-
Disable-AzureRmContextAutosave –Scope Process
80
+
# Ensure that the runbook does not inherit an AzContext
## Comparison of methods for calling a child runbook
102
105
103
-
The following table summarizes the differences between the two methods for calling a runbook from another runbook.
106
+
The following table summarizes the differences between the two ways to call a runbook from another runbook.
104
107
105
108
|| Inline | Cmdlet |
106
109
|:--- |:--- |:--- |
107
110
| Job |Child runbooks run in the same job as the parent. |A separate job is created for the child runbook. |
108
111
| 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. |
109
112
| 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. |
110
113
| 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. |
111
-
| 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 a different subscription that you have a connection to. |
112
-
| Publishing |Child runbook must be published before parent runbook is published. |Child runbook must be published anytime before parent runbook is started. |
114
+
| 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. |
115
+
| Publishing |Child runbook must be published before parent runbook is published. |Child runbook is published any time before parent runbook is started. |
113
116
114
117
## Next steps
115
118
116
119
*[Starting a runbook in Azure Automation](start-runbooks.md)
117
-
*[Runbook output and messages in Azure Automation](automation-runbook-output-and-messages.md)
118
-
120
+
*[Runbook output and messages in Azure Automation](automation-runbook-output-and-messages.md)
0 commit comments