-
Notifications
You must be signed in to change notification settings - Fork 129
/
Find-WhoAmI
47 lines (40 loc) · 2.31 KB
/
Find-WhoAmI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<#
.SYNOPSIS
Queries the automation accounts in the subscription to find the automation account, runbook and resource group that the job is running in.
.DESCRIPTION
Queries the automation accounts in the subscription to find the automation account, runbook and resource group that the job is running in.
It then prints out the subscription, region, automation account name, runbook name, resource group name, and job id.
Useful if doing some additional logging or other scenarios where you need to know this information within a running job.
This uses the RunAs connection that is set up during automation account creation for authentication against the Azure subscription. You
could remove this authentication if the parent runbook has already authencticated against the subscription.
.EXAMPLE
$AccountInfo = .\Find-WhoAmI.ps1
$AccountInfo
.NOTES
AUTHOR: Azure/OMS Automation Team
LASTEDIT: June 30, 2016
#>
$ServicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection"
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $ServicePrincipalConnection.TenantId `
-ApplicationId $ServicePrincipalConnection.ApplicationId `
-CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint | Write-Verbose
Select-AzureRmSubscription -SubscriptionId $ServicePrincipalConnection.SubscriptionID | Write-Verbose
$AutomationResource = Get-AzureRmResource -ResourceType Microsoft.Automation/AutomationAccounts -ExtensionResourceName Microsoft.Automation
foreach ($Automation in $AutomationResource)
{
$Job = Get-AzureRmAutomationJob -ResourceGroupName $Automation.ResourceGroupName -AutomationAccountName $Automation.Name -Id $PSPrivateMetadata.JobId.Guid -ErrorAction SilentlyContinue
if (!([string]::IsNullOrEmpty($Job)))
{
$AutomationInformation = @{}
$AutomationInformation.Add("SubscriptionId",$Automation.SubscriptionId)
$AutomationInformation.Add("Location",$Automation.Location)
$AutomationInformation.Add("ResourceGroupName",$Job.ResourceGroupName)
$AutomationInformation.Add("AutomationAccountName",$Job.AutomationAccountName)
$AutomationInformation.Add("RunbookName",$Job.RunbookName)
$AutomationInformation.Add("JobId",$Job.JobId.Guid)
$AutomationInformation
break;
}
}