automation account - get currently running runbook name

208 Views Asked by At

I would like to print out the name of the currently running runbook.

"Printing runbook name"
$MyInvocation.MyCommand.Name

"Printing call stack info"
$MyInvocation.MyCommand

enter image description here the above does not work and just returns blank. Has anyone else got this to work?

Thanks

1

There are 1 best solutions below

1
On

$Myinvocation automated variable contains mainly the current call stack information as detailed in the given MS Doc. Make sure that you are not using it in an inline script. Because $MyInvocation is not available in InlineScript blocks, so you can't use it within an InlineScript.

Sometimes it doesn't work if you are using it along with the PowerShell function scripts and also PowerShell workflows in an automation account. And also check that you have PowerShell version updated to the latest releases.

I tried below command in my automation environment and was able to retrieve the current runbook name as shown.

$MyInvocation.MyCommand.Name

enter image description here

If still the issue persists, try using below alternative approach using Get-AzAutomationJob command. You will be able to retrieve the runbook name with the help of JobID as explained below.

$runbook=Get-AzAutomationJob -AutomationAccountName "newauto" -ResourceGroupName "xxxxx" -Id "04465b4a-3081-4851-b055-7ce540e6af9a"
write-output $runbook.RunbookName