快速入门:使用 Azure CLI 设置 IoT 中心设备预配服务Quickstart: Set up the IoT Hub Device Provisioning Service with Azure CLI

Azure CLI 用于从命令行或脚本创建和管理 Azure 资源。The Azure CLI is used to create and manage Azure resources from the command line or in scripts. 本快速入门详述了如何使用 Azure CLI 创建 IoT 中心和 IoT 中心设备预配服务并将两个服务链接到一起。This quickstart details using the Azure CLI to create an IoT hub and an IoT Hub Device Provisioning Service, and to link the two services together.

如果没有 Azure 订阅,可在开始前创建一个 试用帐户If you don't have an Azure subscription, create a trial account before you begin.

重要

在本快速入门中创建的 IoT 中心和预配服务将会充当可以公开发现的 DNS 终结点。Both the IoT hub and the provisioning service you create in this quickstart will be publicly discoverable as DNS endpoints. 如果决定更改用于这些资源的名称,请确保不使用任何敏感信息。Make sure to avoid any sensitive information if you decide to change the names used for these resources.

创建资源组Create a resource group

使用“az group create”命令创建资源组。Create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.

以下示例在 chinaeast 位置创建名为 my-sample-resource-group 的资源组。The following example creates a resource group named my-sample-resource-group in the chinaeast location.

az group create --name my-sample-resource-group --location chinaeast

提示

此示例在“中国东部”位置创建资源组。The example creates the resource group in the China East location. 可运行 az account list-locations -o table 命令,查看可用位置的列表。You can view a list of available locations by running the command az account list-locations -o table.

创建 IoT 中心Create an IoT hub

使用 az iot hub create 命令创建 IoT 中心。Create an IoT hub with the az iot hub create command.

以下示例在 chinaeast 位置创建名为 my-sample-hub 的 IoT 中心。The following example creates an IoT hub named my-sample-hub in the chinaeast location. IoT 中心名称在 Azure 中必须是全局唯一的,因此,你可能需要向示例名称添加一个唯一的前缀或后缀,或者选择一个新名称。An IoT hub name must be globally unique in Azure, so you may want to add a unique prefix or suffix to the example name, or choose a new name altogether. 请确保名称遵循 IoT 中心的正确命名约定:名称长度应为 3-50 个字符,只能包含大写或小写字母数字字符或连字符(“-”)。Make sure your name follows proper naming conventions for an IoT hub: it should be 3-50 characters in length, and can contain only upper or lower case alphanumeric characters or hyphens ('-').

az iot hub create --name my-sample-hub --resource-group my-sample-resource-group --location chinaeast

创建设备预配服务Create a Device Provisioning Service

使用 az iot dps create 命令创建设备预配服务。Create a Device Provisioning Service with the az iot dps create command.

以下示例在 chinaeast 位置创建名为 my-sample-dps 的预配服务****。The following example creates a provisioning service named my-sample-dps in the chinaeast location. 你还需要为自己的预配服务选择一个全局唯一的名称。You will also need to choose a globally unique name for your own provisioning service. 请确保名称遵循 IoT 中心设备预配服务的正确命名约定:名称长度应为 3-64 个字符,只能包含大写或小写字母数字字符或连字符(“-”)。Make sure it follows proper naming conventions for an IoT Hub Device Provisioning Service: it should be 3-64 characters in length and can contain only upper or lower case alphanumeric characters or hyphens ('-').

az iot dps create --name my-sample-dps --resource-group my-sample-resource-group --location chinaeast

提示

此示例在“中国东部”位置创建预配服务。The example creates the provisioning service in the China East location. 若要查看可用位置的列表,可以运行 az provider show --namespace Microsoft.Devices --query "resourceTypes[?resourceType=='ProvisioningServices'].locations | [0]" --out table 命令,也可以转到Azure 状态页,在其中搜索“设备预配服务”。You can view a list of available locations by running the command az provider show --namespace Microsoft.Devices --query "resourceTypes[?resourceType=='ProvisioningServices'].locations | [0]" --out table or by going to the Azure Status page and searching for "Device Provisioning Service". 在命令中,可以使用一单词或多单词格式来指定位置,例如:chinaeast、China East、China East,等等。该值不区分大小写。In commands, locations can be specified either in one word or multi-word format; for example: chinaeast, China East, China East, etc. The value is not case sensitive. 如果使用多个单词的格式来指定位置,请将值置于引号中,例如 -- location "China East"If you use multi-word format to specify location, enclose the value in quotes; for example, -- location "China East".

获取 IoT 中心的连接字符串Get the connection string for the IoT hub

需要提供 IoT 中心的连接字符串才能将其与设备预配服务链接到一起。You need your IoT hub's connection string to link it with the Device Provisioning Service. 使用 az iot hub show-connection-string 命令获取连接字符串,并使用其输出设置一个变量。链接这两个资源时,需要用到该变量。Use the az iot hub show-connection-string command to get the connection string and use its output to set a variable that you will use when you link the two resources.

以下示例将 hubConnectionString 变量设置为中心的 iothubowner 策略的主键连接字符串值(可以使用 --policy-name 参数指定其他策略) 。The following example sets the hubConnectionString variable to the value of the connection string for the primary key of the hub's iothubowner policy (the --policy-name parameter can be used to specify a different policy). 用“my-sample-hub”替换先前选择的唯一 IoT 中心名称 。Trade out my-sample-hub for the unique IoT hub name you chose earlier. 此命令使用 Azure CLI 查询输出选项从命令输出提取连接字符串。The command uses the Azure CLI query and output options to extract the connection string from the command output.

hubConnectionString=$(az iot hub show-connection-string --name my-sample-hub --key primary --query connectionString -o tsv)

可以使用 echo 命令查看连接字符串。You can use the echo command to see the connection string.

echo $hubConnectionString

备注

这两个命令适用于在 Bash 下运行的主机。These two commands are valid for a host running under Bash. 如果使用本地 Windows/CMD shell 或 PowerShell 主机,则需修改这些命令,以便使用该环境的正确语法。If you are using a local Windows/CMD shell or a PowerShell host, you need to modify the commands to use the correct syntax for that environment.

使用 az iot dps linked-hub create 命令将 IoT 中心与预配服务相链接。Link the IoT hub and your provisioning service with the az iot dps linked-hub create command.

以下示例将 chinaeast 位置中名为 my-sample-hub 的 IoT 中心与名为 my-sample-dps 的设备预配服务相链接 。The following example links an IoT hub named my-sample-hub in the chinaeast location and a Device Provisioning Service named my-sample-dps. 用这些名称替换先前选择的唯一 IoT 中心和设备预配服务名称。Trade out these names for the unique IoT hub and Device Provisioning Service names you chose earlier. 该命令使用上一步在 hubConnectionString 变量中存储的 IoT 中心的连接字符串 。The command uses the connection string for your IoT hub that was stored in the hubConnectionString variable in the previous step.

az iot dps linked-hub create --dps-name my-sample-dps --resource-group my-sample-resource-group --connection-string $hubConnectionString --location chinaeast

此命令可能需要花费几分钟时间完成。The command may take a few minutes to complete.

验证预配服务Verify the provisioning service

使用 az iot dps show 命令获取预配服务的详细信息。Get the details of your provisioning service with the az iot dps show command.

以下示例获取名为 my-sample-dps 的预配服务的详细信息。The following example gets the details of a provisioning service named my-sample-dps. 用此名称替换自己的设备预配服务名称。Trade out this name for your own Device Provisioning Service name.

az iot dps show --name my-sample-dps

链接的 IoT 中心显示在 properties.iotHubs 集合中。The linked IoT hub is shown in the properties.iotHubs collection.

验证预配服务

清理资源Clean up resources

本教程系列中的其他快速入门教程是在本文的基础上制作的。Other quickstarts in this collection build upon this quickstart. 如果打算继续学习后续的快速入门或相关教程,请不要清除在本快速入门中创建的资源。If you plan to continue on to work with subsequent quickstarts or with the tutorials, do not clean up the resources created in this quickstart. 如果不打算继续,可以使用以下命令删除预配服务、IoT 中心或资源组及其所有资源。If you do not plan to continue, you can use the following commands to delete the provisioning service, the IoT hub or the resource group and all of its resources. 用自己的资源名称替换下面写的资源名称。Replace the names of the resources written below with the names of your own resources.

若要删除预配服务,请运行 az iot dps delete 命令:To delete the provisioning service, run the az iot dps delete command:

az iot dps delete --name my-sample-dps --resource-group my-sample-resource-group

若要删除 IoT 中心,请运行 az iot hub delete 命令:To delete the IoT hub, run the az iot hub delete command:

az iot hub delete --name my-sample-hub --resource-group my-sample-resource-group

若要删除资源组及其所有资源,请运行 az group delete 命令:To delete a resource group and all its resources, run the az group delete command:

az group delete --name my-sample-resource-group

后续步骤Next steps

在本快速入门中,你已部署 IoT 中心和设备预配服务实例,并链接了这两个资源。In this quickstart, you’ve deployed an IoT hub and a Device Provisioning Service instance, and linked the two resources. 若要了解如何使用此设置来预配模拟设备,请继续学习本快速入门中关于如何创建模拟设备的内容。To learn how to use this setup to provision a simulated device, continue to the quickstart for creating a simulated device.