Reputation: 1
Facing some unexpected behavior in Azure Automation that I just can't figure out.
I have a runbook called "Test2" with just 1 line in it, so I can see if it runs: Write-Output "this works"
I have another runbook called Test1. I want to call Test2 from Test1.
This code works fine. It runs Test2 twice:
$test = 'Test2'
& .\Test2.ps1
& .\"$test".ps1
However, THIS code fails to run Test2:
$test = 'Test2'
#& .\Test2.ps1
& .\"$test".ps1
Only change is commenting out the first call. So if I call the runbook just using its name, I can subsequently call it with the variable. But if I don't, I can't.
The error message confirms that it evaluates the variable, but for some reason it just can't find the runbook? Any assistance would be appreciated.
The error: & : The term '.\Test2.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program.
Upvotes: 0
Views: 122
Reputation: 461
Take a look at this quote from the blog post regarding invoking a child runbook inline 'Azure Automation: Runbook Input, Output, and Nested Runbooks':
When you start a runbook that has child runbooks invoked inline, the child runbooks (and all descendants) get compiled into the parent runbook before execution starts. During this compilation phase the parent runbook is parsed for the names of child runbooks; this parsing happens recursively through all descendant runbooks. When the complete list of runbooks is obtained the scripts for these runbooks are retrieved from storage and assembled into a single file that is passed to the PowerShell workflow engine. For this reason, at the time a runbook job is submitted the parent and descendant runbooks must already be published otherwise an exception will occur during compilation. Currently, the order of publishing also matters: you must publish the child runbooks first and then publish the parent. Similarly, when testing the parent runbook in the Azure Automation authoring page you must first publish the child runbooks and then you can test the parent. Another consequence of this is that you cannot use a variable to pass the name of an child runbook invoked inline: you must always explicitly name the child runbook within the parent runbook.
So you can't use a variable to pass the name of a child runbook invoked inline; the answer from @Jahnavi probably worked because of the order in which they published the runbooks, but would be likely to break if the explicit name of the child runbook in the parent runbook was removed at a later date.
Maybe try starting the runbook using cmdlet-based Start-AzAutomationRunbook instead?
Upvotes: 0
Reputation: 8018
Firstly, When I execute the script without commenting the
& .\test2.ps1
, it worked as expected.
$test = 'test2'
& .\test2.ps1
& .\"$test".ps1
write-output "call success"
Output:
Once its done, I tried running the same script as you (commented
& .\test2.ps1
) and the call from test
to test2
runbook was successful as shown below.
Try checking the test2 file directory path once and re run the script.
If still the issue persists, as a workaround you can use Invoke-Expression
command to take the call expression instead of substituting it in the variable.
$test = 'test2'
#& .\test2.ps1
Invoke-Expression "& .\$test.ps1"
write-output "call success"
Output:
Upvotes: 0