powershellazureazure-automationrunbook

How do I pass parameters from Child to Parent in Azure RunBooks


So, one would think this would be pretty simple, but I've been dealing with this for a few days now.

Basically it's like this:

Parent.ps1

  #calling the childrunbook
./childrunbook.ps1 -FirstName 'John'-LastName 'Snow'
 $newGreeting = $greeting + 'John Snow'
 Write-Output $newGreeting

Child.ps1

param(
   [string]$Firstname, 
   [string]$Lastname
)

$greeting = 'Hello from the Child Runbook'
Write-Output $greeting

Result

#I was hoping to get 
"Hello from the Child Runbook John Snow"
#But all I'm getting is:
"John Snow"  :-( 

I can do this easily in Powershell, but once I put this same code in Powershell Runbooks on Azure, it's a no go. I thought it might have been a single quote/double quote issue but that hasn't led to any progress. Any ideas?

Thanks in advance!


Solution

  • When you just run a script like this:

    ./childrunbook.ps1 -FirstName 'John'-LastName 'Snow'
    

    it executes in it's own scope - meaning that anything written to variables inside the script will only modify a local copy of that variable, and the changes won't touch anything in the parent scope.

    In order to execute the script in the calling scope, use the dot source operator .:

    . ./childrunbook.ps1 -FirstName 'John'-LastName 'Snow'
    

    Alternatively you'd just assign the output from the child script to a variable in the calling scope:

    $greeting = ./childrunbook.ps1 -FirstName 'John'-LastName 'Snow'
    $newGreeting = $greeting + 'John Snow'
    Write-Output $newGreeting