Reputation: 163
I'm trying to create a powershell script that will create a new event hub, consumer group, and Shared Access Policies, all so I can then create an Event Grid Subscription that then uses the event hub as the endpoint. Using:
$eventHubResource = New-AzureRmEventHub
-ResourceGroupName $RG.Name
-NamespaceName $eventHubNameSpace.Name
-Name $eventHubName
-MessageRetentionInDays $eventHubMessageRetention
-PartitionCount $eventHubPartitionCount
I can create the event hub, SAP's and consumer groups, but when trying to create the EventGridSubscription using: New-AzureRmEventGridSubscription
it asks for an -Endpoint parameter
-Endpoint
Event subscription destination endpoint. This can be a webhook URL or the Azure resource ID of an EventHub.
How can I get the EventHub resource Id via powershell?
Get-AzureRmEventHub does not return the resource id to be used New-AzureRmEventHub seems to return the same object as the Get-AzureRmEventHub
I haven't had any success with Get-AzureRmResource as it seems to only list the resources from a parent level and not for a given resource itself, but I may be using it incorrectly.
I'm open to suggestions on what to try.
Upvotes: 1
Views: 1220
Reputation: 42043
Actually, the New-AzureRmEventHub
, Get-AzureRmEventHub
, Get-AzureRmResource
all return the resource ID
, refer to the command as below.
Note: My sample uses the new Az
powershell module, you can also the old AzureRm
command.
$event = New-AzEventHub -ResourceGroupName "<ResourceGroupName>" -NamespaceName "joyeventhub" -Name "joyevent1"
$event.Id
$event | ConvertTo-Json
You could check the resource ID with $event | ConvertTo-Json
, $event.Id
is the resource ID
you want.
Then use the command to create the Event Grid Subscription
New-AzEventGridSubscription -ResourceGroupName "<ResourceGroupName>" -EventSubscriptionName EventSubscription1 -EndpointType "eventhub" -Endpoint $event.Id
Besides, if you want to get the resource ID
via Get-AzEventHub
and Get-AzResource
, refer to the commands as below.
#use Get-AzEventHub
$id = (Get-AzEventHub -ResourceGroupName "<ResourceGroupName>" -NamespaceName "joyeventhub" -Name "joyevent").Id
#use Get-AzResource
$id = (Get-AzResource -ResourceGroupName "<ResourceGroupName>" -ResourceType "Microsoft.EventHub/namespaces/eventhubs" -ResourceName "<NamespaceName>/<InstanceName>" -ApiVersion 2015-08-01).ResourceId
Upvotes: 2