Create and delete routes and endpoints by using Azure PowerShell
Article
This article shows you how to create a route and endpoint in your hub in Azure IoT Hub and then delete your route and endpoint. Learn how to use Azure PowerShell to create routes and endpoints for Azure Event Hubs, Azure Service Bus queues and topics, and Azure Storage.
Currently, PowerShell doesn't support managed identity authentication types for creating endpoints. If you can't use SAS authentication in your scenario, use one of the other management tools to create endpoints.
Also, PowerShell currently doesn't support creating Cosmos DB endpoints.
Prerequisites
Review the prerequisites for this article based on the type of endpoint you want to route the messages to.
An Azure subscription. If you don't have an Azure subscription, create a free Azure account before you begin.
An IoT hub. If you don't have a hub, you can follow the steps to create an IoT hub.
Azure PowerShell. To use Azure PowerShell locally, install the Azure PowerShell module on your computer. Alternatively, to use Azure PowerShell in a web browser, enable Azure Cloud Shell.
An Event Hubs resource (with container). If you need to create a new Event Hubs resource, see Quickstart: Create an event hub.
An Azure subscription. If you don't have an Azure subscription, create a free Azure account before you begin.
An IoT hub. If you don't have a hub, you can follow the steps to create an IoT hub.
Azure PowerShell. To use Azure PowerShell locally, install the Azure PowerShell module on your computer. Alternatively, to use Azure PowerShell in a web browser, enable Azure Cloud Shell.
An Azure subscription. If you don't have an Azure subscription, create a free Azure account before you begin.
An IoT hub. If you don't have a hub, you can follow the steps to create an IoT hub.
Azure PowerShell. To use Azure PowerShell locally, install the Azure PowerShell module on your computer. Alternatively, to use Azure PowerShell in a web browser, enable Azure Cloud Shell.
An Azure subscription. If you don't have an Azure subscription, create a free Azure account before you begin.
An IoT hub. If you don't have a hub, you can follow the steps to create an IoT hub.
Azure PowerShell. To use Azure PowerShell locally, install the Azure PowerShell module on your computer. Alternatively, to use Azure PowerShell in a web browser, enable Azure Cloud Shell.
An Azure Storage resource. If you need to create a new Azure Storage account, see Create a storage account.
In IoT Hub, you can create a route to send messages or capture events. Each route has a data source and an endpoint. The data source is where messages or event logs originate. The endpoint is where the messages or event logs end up. You choose locations for the data source and endpoint when you create a new route in your IoT hub. Then, you use routing queries to filter messages or events before they go to the endpoint.
The service that you use to create your endpoint must first exist in your Azure account.
Create a new IoT hub endpoint to Event Hubs. Use your primary connection string from the preceding step. The value for EndpointType must be EventHub. For all other parameters, use the values for your scenario.
Create a new IoT hub endpoint to your Service Bus queue. Use your primary connection string from the preceding step with ;EntityPath=MyServiceBusQueue added to the end. The value for EndpointType must be ServiceBusQueue. For all other parameters, use the values for your scenario. For EndpointName, use a unique value.
Create a new IoT hub endpoint to your Service Bus topic. Use your primary connection string from the preceding step with ;EntityPath=MyServiceBusTopic added to the end. The value for EndpointType must be ServiceBusTopic. For all other parameters, use the values for your scenario. For EndpointName, use a unique value.
To create an endpoint to Azure Storage, you need your access key to construct a connection string. The connection string is then part of the IoT Hub command to create an endpoint.
Retrieve your access key from your storage account, and then copy it.
Construct a primary connection string for your storage account based on the following template. Replace mystorageaccount (in two places) with the name of your storage account. Replace mykey with your key from the preceding step.
This connection string is required in the step to create the endpoint.
Create your Azure Storage endpoint with the connection string you constructed. EndpointType must have the value azurestorage. For all other parameters, use the values for your scenario. For EndpointName, use a unique value.
When you're prompted for your container name, enter your container name, and then select Enter to create the endpoint.
Create an IoT Hub route
With your new endpoint in your IoT hub, you can create a new route.
The default fallback route in IoT Hub collects messages from DeviceMessages. Choose a different option for your custom route, such as DeviceConnectionStateEvents. For more information about source options, see Add-AzIotHubRoute. The Enabled parameter is a switch, so you don't need to use a value with the parameter.
Plan and execute an endpoint deployment strategy, using essential elements of modern management, co-management approaches, and Microsoft Intune integration.