Azure IoT Hub : Get Connected Devices count using AZ CLI

Jignesh Vala 61 Reputation points
2022-05-12T14:07:18.253+00:00

Hi Team,

I need to Get Connected Devices count using AZ CLI.

Same thing I tried using power shell but we have to use AZ CLI.
For Powershell Reference link: https://learn.microsoft.com/en-us/powershell/module/az.iothub/get-aziothubdevice?view=azps-7.5.0

I tried "az iot hub device-identity list" this command but it's not working for me. If possible please provide details.

Thanks in advance.

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,218 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 33,716 Reputation points MVP
    2022-05-12T14:56:21.153+00:00

    Hello @Jignesh Vala ,

    The IoT Hub is accessible using the CLI using the AZ keyword.

    I expect you have installed it already.

    For IoT, there is a separate extension:

    az extension add --name azure-iot

    I also recommend checking if the CLI AZ command needs an update:

    az upgrade

    201437-image.png

    I used the 'interactive' version (which is still in preview):

    az interactive

    This generated for my test (the tool let me leave out 'az'):

    az iot hub device-identity list --top -1 --edge-enabled true --hub-name edgedemo-ih

    This gave me a JSON representation of all known (edge) devices, not limited in length:

    201466-image.png

    From here, you can parse the JSON to get the count.

    An alternative is the query language:

    az iot hub query --hub-name edgedemo-ih --output json --query-command 'select * from devices'

    this gives you more control over the output and returns:

    201551-image.png

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.