我有一个 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/