Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Last active November 13, 2019 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenjrobinson/28153eba111d3d53a71a40f7ee295431 to your computer and use it in GitHub Desktop.
Save darrenjrobinson/28153eba111d3d53a71a40f7ee295431 to your computer and use it in GitHub Desktop.
Export IoT Devices from IoT Hubs after enumerating Resource Groups from for IoT Hubs in an Azure Subscription
Import-Module Azure
Login-AzureRmAccount
$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
$IoTHub
$IoTHubs += $IoTHub
}
catch {
write-host "Something went wrong enumerating IoT Hub(s) in Resource Group $($RG.ResourceGroupName)"
}
}
$IoTHubs
#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\$($hub.name)-devices.txt"
# 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).azure-devices.net;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 $rg -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 $($storageContainer.name) -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: $($device.id) Status: $($device.status)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment