Adam Levitt
Adam Levitt

Reputation: 10476

Azure Runbooks - Import Commands From Separate Runbook

I have created Runbook A that has some declared variables and some functions.

// Runbook A
$myvar = "test"
Function MyFunc($var1) {
  Write-Output $var1
}

// Runbook B
Write-Output $myvar
MyFunc

How do I import the code from Runbook A into Runbook B so I can make the code in Runbook A reusable?

Upvotes: 0

Views: 478

Answers (3)

Shadmehr
Shadmehr

Reputation: 278

Use Invoke-Expression

# Runbook_A
$global:FX = @'
$myvar = "test"
Function MyFunc($var1) {
  Write-Output $var1
}
'@

And in parent module:

# RunBook_B
.\Runbook_A.ps1
Invoke-Expression $global:FX

Write-Output $myvar
MyFunc "Hello $myvar"

Output:

test
Hello test

Note: Runbook_A should be published and located in the same Automation Account

Upvotes: 0

Adam Levitt
Adam Levitt

Reputation: 10476

In order to solve this, I created a powershell module .psm1 file. It allows me to call the functions, but it's not letting me print the variables. Still working on that.

Upvotes: 0

4c74356b41
4c74356b41

Reputation: 72141

Basically, there is no direct way of doing what you ask for. You cant use runbooks as functions. But you could create your runbooks to be modular and invoke them.

You don't have to login to Azure to invoke a runbook. Webhook looks like a much better option.

Upvotes: 0

Related Questions