Guest User

Untitled

a guest
Jan 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. ## Standard disclaimer : This code is provided as a sample and is in no way warranted for production use.
  2.  
  3. ## Set the storage context.
  4. $AccountName = ""
  5. $AccountKey = ""
  6. $ctx = New-AzureStorageContext -StorageAccountName $AccountName -StorageAccountKey $AccountKey
  7.  
  8. ## Fetch a container to list. Note that I use the analytical logs folder. You are free to choose your own.
  9. $container = Get-AzureStorageContainer -Context $ctx -Name "`$logs"
  10.  
  11. ## To fetch blob list as a flat structure (equivalent to dir /s /b)
  12.  
  13. # 1. Listing the blobs with use flat listing set to true
  14. $container.CloudBlobContainer.ListBlobs($null,$true) | Foreach-Object {Write-Host $_.Uri.ToString()}
  15.  
  16. ## To fetch blob list as folder structures
  17.  
  18. # 1. Create a stack to walk the folder tree
  19. $folderstack = New-Object "System.Collections.Stack"
  20. $folderstack.Clear();
  21.  
  22. # 2. Push in the top container
  23. $folderstack.Push($container)
  24.  
  25. # 3. Start loop until stack is empty
  26. do{
  27. # 3.1. Pop entries from the stack to enumerate
  28. $c = $folderstack.Pop()
  29. # 3.2. The list method to call is different for an AzureStorageContainer object
  30. if ($c.GetType().Name -eq "AzureStorageContainer"){
  31. # 3.2.1. If the entries are directory objects, push them into the stack to enumerate again
  32. $c.CloudBlobContainer.ListBlobs($null,$false)| ForEach-Object {if ($_.GetType().Name -eq "CloudBlobDirectory") { $folderstack.Push($_); Write-Host $_.Uri.ToString()} else {Write-Host $_.Uri.ToString()}}
  33. }
  34. # 3.3. And different again for a CloudBlobDirectory object
  35. if ($c.GetType().Name -eq "CloudBlobDirectory")
  36. {
  37. # 3.3.1. If the entries are directory objects, push them into the stack to enumerate again
  38. $c.ListBlobs($false)| ForEach-Object {if ($_.GetType().Name -eq "CloudBlobDirectory") { $folderstack.Push($_); Write-Host $_.Uri.ToString()} else {Write-Host $_.Uri.ToString()}}
  39. }
  40. }while ($folderstack.Count -gt 1)
  41. # 4. Stack is empty, all entries have been enumerated!
  42. # All Done!
Add Comment
Please, Sign In to add comment