How can we get the list of all parent edge devices mapped to an iothub using az cli?

PRIYANKA S NAIR 20 Reputation points
2023-09-29T11:59:30.4033333+00:00

I would like to get the list of all parent devices in a iot hub using az cli to perform some updates specifically on parent devices.

Thanks in advance

Azure IoT Edge
Azure IoT Edge
An Azure service that is used to deploy cloud workloads to run on internet of things (IoT) edge devices via standard containers.
547 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,133 questions
0 comments No comments
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 13,781 Reputation points
    2023-09-29T21:26:29.2866667+00:00

    Hi @PRIYANKA S NAIR Greetings! Welcome to Microsoft Q&A forum. Thank you for posting this question here.

    I am not sure if there is any out of the box command/query you can execute to achieve this. There is a property called parentScopes which will be a part of the child device twin and will contain a reference to the parent device. Here is the sample of the value the property withholds in the child device twin JSON.

    "parentScopes": [       
        "ms-azure-iot-edge://Parent1-638316034265745064"     
    ]
    

    Parent1 is the device ID of the parent Edge device which can be extracted from the sample response.

    You can execute the below query Azure CLI to get the parent scopes from all the child devices which provide you the details of the parent devices

    az iot hub query --hub-name <your-IoTHub-name> --query-command "SELECT parentScopes FROM devices WHERE parentScopes != null"
    
    

    Please make sure to replace the IoT Hub name in the above command

    Alternatively, you can add tag on all the parent devices and query upon this tag to fetch the correct results. Here is an example of how you can do that. Find a parent Edge device on the Azure portal and edit the tags section and add a custom tag of your choice. In the example below, I have added a tag with a name isParent and provided a value true

    User's image

    You can save this tag and repeat the same to other parent devices. After that, run the following command to fetch the parent devices details.

    az iot hub query --hub-name <your-IoTHub-nam>--query-command "SELECT deviceId FROM devices WHERE capabilities.iotEdge = true AND tags.isParent = true"
    
    

    This will give you the deviceId's of all the parent devices. Please also refer to the article Queries for IoT Hub device and module twins to understand how the queries work.

    Hope this helps. Please let us know if you have any additional questions.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful