Examine device management using the Azure IoT extension for Azure CLI

Completed

The IoT extension for Azure CLI gives developers command-line access to IoT device maintenance capabilities.

An organization may decide to use Azure CLI for IoT device management in situations where scripting is required for repeatable or scriptable tasks such as with periodic device updates that a run on a timer. CLI scripting retains the flexibility and power of a coding environment versus using the Azure IoT Hub portal interface or Visual Studio Code and Azure IoT Hub extension menu-based interface.

Recall that as an electrical power system operator, you had to take a faulty device offline using the Azure portal. You are concerned that other devices may become erratic due to a recent firmware update that may have some faulty code. You ask a programmer to check the code for errors. The programmer finds and fixes the faulty code. You run a script within the IoT extension for Azure CLI that updates all applicable device firmware with the new firmware version through a wireless network.

Here, you learn about Azure CLI scripting capabilities that you can use to manage IoT devices.

Management option

Task

Direct methods

Make a device act such as starting or stopping sending messages or rebooting the device.

Twin desired properties

Put a device into certain states, such as setting an LED to green or setting the telemetry send interval to 30 minutes.

Twin reported properties

Get the reported state of a device. For example, the device reports the LED is blinking now.

Twin tags

Store device-specific metadata in the cloud. For example, the deployment location of a vending machine.

Device twin queries

Query all device twins to retrieve those twins that match specified conditions, such as identifying the devices that are available for use.

Device identity updates

Modify device identity information in the IoT Hub identity registry.

Device Update for IoT Hub

Publish, distribute, and manage over-the-air updates (OTA) to IoT devices. For example, update firmware on a group of devices.

Tip

If you don't have the IoT extension for Azure CLI installed, the simplest way to install it is to run az extension add --name azure-iot in the Azure CLI.

Before you can enter any device management commands, you need to sign in to your Azure account:

az login

Note

The commands listed as follows are an introductory sampling and not meant to be a complete reference.

Direct methods

To invoke a direct method on a device, use the following command:

az iot hub invoke-device-method --device-id <your device id> \
  --hub-name <your hub name> \
  --method-name <the method name> \
  --method-payload <the method payload>

Device twin desired properties

Set a desired property interval = 3000 by running the following command:

az iot hub device-twin update --hub-name <your hub name> \
  --device-id <your device id> \
  --set properties.desired.interval = 3000

This property can be read from your device.

Device twin reported properties

Get the reported properties of the device by running the following command:

az iot hub device-twin show --hub-name <your hub name> --device-id <your device id>

One of the twin reported properties is $metadata.$lastUpdated, which shows the last time the device app updated its reported property set.

Device twin tags

Display the tags and properties of the device by running the following command:

az iot hub device-twin show --hub-name <your hub name> --device-id <your device id>

Add a field role = temperature&humidity to the device by running the following command:

az iot hub device-twin update \
  --hub-name <your hub name> \
  --device-id <your device id> \
  --tags '{"role":"temperature&humidity"}'

Device twin queries

Query devices with a tag of role = 'temperature&humidity' by running the following JMESPath query language command:

az iot hub query --hub-name <your hub name> \
  --query-command "SELECT * FROM devices WHERE tags.role = 'temperature&humidity'"

Query all devices except those with a tag of role = 'temperature&humidity' by running the following command:

az iot hub query --hub-name <your hub name> \
  --query-command "SELECT * FROM devices WHERE tags.role != 'temperature&humidity'"

Device identity

Create an edge enabled IoT device with default authorization (shared private key).

az iot hub device-identity create --hub-name <your hub name> -device-id <your device id> --ee

Device Update for IoT Hub

Initialize a minimum content import manifest.

az iot du update init v5 --update-provider Microsoft --update-name myAptUpdate --update-version 1.0.0 --description "My minimum update" --compat manufacturer=Contoso model=Vacuum --step handler=microsoft/apt:1 properties="@/path/to/file" --file path=/my/apt/manifest/file