azure - 如何使用 Azure CLI 从 Azure IoT 中心删除所有设备?

标签 azure powershell azure-iot-hub azure-cli

我有一个 Azure IoT 中心,其中包含我们团队 E2E​​ 测试生成的一堆设备。我想使用 Azure CLI 每隔一段时间清除一次集线器。

我正在 Powershell 上本地运行 Azure CLI,使用 Azure IoT extension .

根据我的研究,有一种方法可以获取集线器中所有设备的列表,以 JSON 格式打印到控制台:

az iot hub device-identity list --hub-name "test-hub"

有一种方法可以删除单个设备标识:

az iot hub device-identity delete --device-id "test-id" --hub-name "test-hub"

如何使用 cli 界面和一些 powershell 命令删除集线器中的所有设备?

最佳答案

只需在 PowerShell 中运行 For 循环即可。

首先安装适用于 Powershell 的 Azure CLI:

Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'

然后添加适用于 PowerShell 的 Azure IoT 扩展模块,登录到 Azure,并更改为适当的订阅(更改 <subscription_id> ):

az extension add --name azure-cli-iot-ext
az login
az account set -s <subscription_id>

之后,运行以下 Foreach 循环,这将删除所有设备(更改 test-hub ):

$iotHubName = "test-hub"
$json = az iot hub device-identity list --hub-name $iotHubName
$devices = $json | ConvertFrom-Json
Foreach ($device in $devices)
{
  az iot hub device-identity delete --device-id $device.deviceId --hub-name $iotHubName
}

注意:截至 2019 年,这是一个极其缓慢的过程。您可以通过在主 Portal.azure UI 中查找 IoT 设备来跟踪进度。

关于azure - 如何使用 Azure CLI 从 Azure IoT 中心删除所有设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56567310/

相关文章:

node.js - 您可以根据事件中心消息 header 属性有条件地触发 azure 函数吗?

c# - 加密 azure 网站的数据库连接字符串

powershell - 如何在 Powershell 中返回格式化为表格的自定义对象?

具有 IoT 中心触发器的 Java Azure 函数未启动

Azure IOT HUB 带有空格或点的消息路由标记

azure - 无法导出我的模型以在 Azure Custom Vision 上进行部署

powershell - Azure - 使用queuetrigger从webjob添加自定义域

powershell - 如何使用 powershell 删除所有 Azure 资源

azure - X509 配置需要什么设备才能将遥测数据发送到 IoTHub

azure - 如何使用路由查询将消息路由到服务总线队列?