如何通过PowerShell获取eventhub实例的端点?

时间:2019-07-18 20:21:56

标签: powershell azure-powershell

我正在尝试创建一个Powershell脚本,该脚本将创建一个新的事件中心,使用者组和共享访问策略,因此我可以创建一个事件网格订阅,然后将该事件中心用作端点。 使用:

$eventHubResource = New-AzureRmEventHub 
-ResourceGroupName $RG.Name 
-NamespaceName $eventHubNameSpace.Name 
-Name $eventHubName 
-MessageRetentionInDays $eventHubMessageRetention 
-PartitionCount $eventHubPartitionCount

我可以创建事件中心,SAP和使用者组,但是在尝试使用以下方法创建EventGridSubscription时:     New-AzureRmEventGridSubscription

它要求一个-Endpoint参数

https://docs.microsoft.com/en-us/powershell/module/azurerm.eventgrid/new-azurermeventgridsubscription?view=azurermps-6.13.0

    -Endpoint
    Event subscription destination endpoint. This can be a webhook URL or the Azure resource ID of an EventHub.

如何通过powershell获取EventHub资源ID?

Get-AzureRmEventHub不返回要使用的资源ID New-AzureRmEventHub似乎返回与Get-AzureRmEventHub相同的对象

我在Get-AzureRmResource上没有取得任何成功,因为它似乎仅列出父级资源,而不列出给定资源本身,但是我可能使用不正确。

我愿意尝试一些建议。

1 个答案:

答案 0 :(得分:1)

实际上,New-AzureRmEventHubGet-AzureRmEventHubGet-AzureRmResource都返回resource ID,请参考以下命令。

注意:我的示例使用了新的Az powershell模块,您也可以使用旧的AzureRm命令。

$event = New-AzEventHub -ResourceGroupName "<ResourceGroupName>" -NamespaceName "joyeventhub" -Name "joyevent1"
$event.Id
$event | ConvertTo-Json

您可以使用$event | ConvertTo-Json检查资源ID,$event.Id是您想要的resource ID

enter image description here

然后使用命令创建事件网格订阅

New-AzEventGridSubscription -ResourceGroupName "<ResourceGroupName>" -EventSubscriptionName EventSubscription1 -EndpointType "eventhub" -Endpoint $event.Id 

enter image description here


此外,如果要通过resource IDGet-AzEventHub获取Get-AzResource,请参考以下命令。

#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