Create a gist now

Instantly share code, notes, and snippets.

Export IoT Devices from IoT Hubs after enumerating Resource Groups from for IoT Hubs in an Azure Subscription
Import-Module Azure
$sub = Get-AzureRMSubscription | Get-AzureRMSubscription -SubscriptionName "Visual Studio Professional with MSDN"
Select-AzureRmSubscription -SubscriptionId $sub.Id[0]
#Get all Resource Groups in Subscription
$ResourceGroups = Get-AzureRmResourceGroup
$IoTHubs = @()
foreach ($RG in $ResourceGroups){
try {
# Find IoT Hubs
$IoTHub = Get-AzureRmIotHub -ResourceGroupName $RG.ResourceGroupName
$IoTHubs += $IoTHub
catch {
write-host "Something went wrong enumerating IoT Hub(s) in Resource Group $($RG.ResourceGroupName)"
#Enmerate the IoT Hubs to export the IoT Devices
$IoTKeyName = "iothubowner"
$IoTDevices = @()
ForEach($hub in $IoTHubs) {
# Temp Output File to download exported IoT Devices too
$outputFile = "C:\temp\$($"
# version 3.1.3 of AzureRM.IotHub doesn't return ResourceGroupName property. Derive from ID instead
[array]$Id = $hub.Id.split("/")
$rg = $Id[4]
# HubKey
$hubKey = Get-AzureRmIotHubKey -ResourceGroupName $rg -Name $hub.Name -KeyName $IoTKeyName
# IoT ConnectionString
$IoTConnectionString = "HostName=$($hub.Name);SharedAccessKeyName=$($IoTKeyName);SharedAccessKey=$($hubKey.PrimaryKey)"
$registry = Get-AzureRmIotHubRegistryStatistic -ResourceGroupName $rg -Name $hub.Name
# Get/Create a Storage Account to Export Devices to
try {
# Get Storage Account Details if it it exits
$storageAccount = Get-AzureRmStorageAccount -ResourceGroupName $rg -Name "$($hub.Name)export"
$storageAccountCTX = $storageAccount.Context
$storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName MongooseIoTHub -Name $storageAccount.StorageAccountName).Value[0]
$storageContainer = Get-AzureStorageContainer -Name "$($hub.Name.ToLower())export" -Context $storageAccountCTX
$containerSASToken = $storageContainer | New-AzureStorageContainerSASToken -Permission rwdl
} catch {
# Doesn't Exist. Create a new one.
# Create New Storage Account, Container and Generate Container SASToken
write-host "$($hub.Name)export doesn't exist. Creating new Storage Account."
$storageAccount = New-AzureRmStorageAccount -ResourceGroupName $rg -AccountName "$($hub.Name.ToLower())export" -Location $hub.Location -SkuName "Standard_GRS"
$storageAccountCTX = $storageAccount.Context
$storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $rg -Name $storageAccount.StorageAccountName).Value[0]
$storageContainer = New-AzureStorageContainer -Name "$($hub.Name.ToLower())export" -Permission Container -Context $storageAccountCTX
$containerSASToken = $storageContainer | New-AzureStorageContainerSASToken -Permission rwdl
# Export IOT Devices
New-AzureRmIotHubExportDevices -ResourceGroupName $rg -Name $hub.Name -ExportBlobContainerUri "$($storageContainer.CloudBlobContainer.Uri.AbsoluteUri)$($containerSASToken)" -Verbose
# Download the Blob Export File
$deviceBlobContents = $storageAccountCTX | Get-AzureStorageBlob -Container $($ -Blob devices.txt | Get-AzureStorageBlobContent -Destination $outputFile
# Open the file and import
$deviceContents = get-content -path $outputFile | convertfrom-json
foreach ($device in $deviceContents){
write-host "Device: $($ Status: $($device.status)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment