How To Register An IoT Device To Azure IoT Hub Using PowerShell

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 which 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
    1. $RGName ="MyIOTRG"    
    2. $IoTHubName = "jsiotconnect"    
    3. $IoTKeyName = "iothubowner"    
    4. $IoTHub = Get-AzureRmIotHub -Name $IoTHubName -ResourceGroupName $RGName    

 

If you want to create the new IoT Hub, here is an article How to Create Azure IoT Hub Using Azure PowerShell .

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).
    1. $RGName ="MyIOTRG"    
    2. $IoTHubName = "jsiotconnect"    
    3. $IoTKeyName = "iothubowner"    
    4. $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.
    1. $IoTConnectionString = "HostName=$($IoTHubName).azure-devices.net;SharedAccessKeyName=$($IoTKeyName);SharedAccessKey=$($IoTHubKey.PrimaryKey)"#    
    2. New DeviceID    
    3. $newDeviceID = "jsiotdevice_004"    
    4. $deviceParams = @ {    
    5.     iotConnString = $IoTConnectionString    
    6.     deviceId = $newDeviceID    
    7. }    
    8. $device = Register - IoTDevice @deviceParams    
    9. $device   

 

Final Code

  1. Connect - AzureRmAccount  
  2. $RGName = "MyIOTRG"  
  3. $location = "East US"  
  4. $IoTHubName = "jsiotconnect"  
  5. $IoTKeyName = "iothubowner"  
  6. $IoTHub = Get - AzureRmIotHub - Name $IoTHubName - ResourceGroupName $RGName  
  7. $IoTHubKey = Get - AzureRmIotHubKey - ResourceGroupName $RGName - Name $IoTHubName - KeyName $IoTKeyName  
  8. $IoTConnectionString = "HostName=$($IoTHubName).azure-devices.net;SharedAccessKeyName=$($IoTKeyName);SharedAccessKey=$($IoTHubKey.PrimaryKey)"#  
  9. New DeviceID  
  10. $newDeviceID = "jsiotdevice_004"  
  11. $deviceParams = @ {  
  12.     iotConnString = $IoTConnectionString  
  13.     deviceId = $newDeviceID  
  14. }  
  15. $device = Register - IoTDevice @deviceParams  
  16. $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.