Search code examples
azureazure-automationazure-runbook

Azure Automation: Runbook: does it know it's TenantID?


Good afternoon

I guess that this is relation is true:

Azure Tenant
» Azure Subscription
  » Azure Automation
    » Runbook

Therefore I have this question:

Is there a PowerShell command which can be used to get the TenantID in which the Runbook is running?


Solution

  • If you want to run the command inside of the runbook, you could use this:

    $servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection"        
    Write-Output $servicePrincipalConnection.TenantId
    

    enter image description here

    Note: there is a prerequisite, when you create the automation account, you need to select Create Azure Run As account with Yes, otherwise the AzureRunAsConnection is not existing.

    enter image description here