Introduction
In this article, we are going to see how to add an IoT device to Azure IoT Hub using Azure PowerShell cmdlets. IoT hub is a cloud platform service that supports multiple protocols and open-source SKD’s. It is used to connect, manage, and monitor billions of IoT devices and securely connects the devices to develop IoT applications.
Prerequisites
Before you begin to utilize PowerShell to oversee the Azure PowerShell, ensure that the Azure PowerShell has been installed. If not installed, here is an article on
how to install the Azure PowerShell module and also,
Install the Azure IoT module. You need to do this only once for each computer from which you are running the Azure PowerShell commands.
Connecting to Azure Portal
Connect to Azure Portal using Connect-AzureRmAccount cmdlet.
Connect-AzureRmAccount
Select the Azure IoT Hub
Select Azure IoT Hub where we want to register the device. You can select the IoT Hub that was already created by using the “Get-AzureRmIotHub” cmdlets. The required parameters are,
- ResourceGroupName - Specify the name of the resource group.
- Name - Specify the name of the IoT Hub
- $RGName ="MyIOTRG"
- $IoTHubName = "jsiotconnect"
- $IoTKeyName = "iothubowner"
- $IoTHub = Get-AzureRmIotHub -Name $IoTHubName -ResourceGroupName $RGName
Get Azure IoT Hub Key
You can get the Azure IoT Hub Key using the “Get-AzureRmIotHubKey” cmdlets. The required parameters are,
- ResourceGroupName - Specify the name of the resource group.
- Name - Specify the name of the IoT Hub.
- KeyName - Specify the IoT Hub Key name (iothubowner, service, device).
- $RGName ="MyIOTRG"
- $IoTHubName = "jsiotconnect"
- $IoTKeyName = "iothubowner"
- $IoTHubKey = Get-AzureRmIotHubKey -ResourceGroupName $RGName -Name $IoTHubName -KeyName $IoTKeyName
Register New IoT Device
You can register the new Azure IoT device using the “Register-IoTDevice” cmdlets. The required parameters are,
- iotConnString - Specify the IoT Hub Connection string.
- DeviceId - Specify the device Id.
- $IoTConnectionString = "HostName=$($IoTHubName).azure-devices.net;SharedAccessKeyName=$($IoTKeyName);SharedAccessKey=$($IoTHubKey.PrimaryKey)"#
- New DeviceID
- $newDeviceID = "jsiotdevice_004"
- $deviceParams = @ {
- iotConnString = $IoTConnectionString
- deviceId = $newDeviceID
- }
- $device = Register - IoTDevice @deviceParams
- $device
Final Code
- Connect - AzureRmAccount
- $RGName = "MyIOTRG"
- $location = "East US"
- $IoTHubName = "jsiotconnect"
- $IoTKeyName = "iothubowner"
- $IoTHub = Get - AzureRmIotHub - Name $IoTHubName - ResourceGroupName $RGName
- $IoTHubKey = Get - AzureRmIotHubKey - ResourceGroupName $RGName - Name $IoTHubName - KeyName $IoTKeyName
- $IoTConnectionString = "HostName=$($IoTHubName).azure-devices.net;SharedAccessKeyName=$($IoTKeyName);SharedAccessKey=$($IoTHubKey.PrimaryKey)"#
- New DeviceID
- $newDeviceID = "jsiotdevice_004"
- $deviceParams = @ {
- iotConnString = $IoTConnectionString
- deviceId = $newDeviceID
- }
- $device = Register - IoTDevice @deviceParams
- $device
I hope you have learned how to register a new IoT device in Azure IoT Hub using Azure PowerShell programmatically. Feel free to fill up the comment box below should you need any assistance.