PowerShell check for Azure Storage Container existence PowerShell check for Azure Storage Container existence powershell powershell

PowerShell check for Azure Storage Container existence


You can request the list of storage containers and look for a specific one and wait until that isn't returned anymore. This works okay if the account doesn't have a ton of containers in it. If it does have a lot of containers then this won't be efficient at all.

while (Get-AzureStorageContainer | Where-Object { $_.Name -eq "mycontainer" }){    Start-Sleep -s 100    "Still there..."}

The Get-AzureStorageContainer cmdlet also takes a -Name parameter and you could do a loop of asking for it to be returned; however, when the container doesn't exist it throws an error (Resource not found) instead of providing an empty result, so you could trap for that error and know it was gone (make sure to explicitly look for Reource Not found vs a timeout or something like that).

Update: Another option would be to make a call to the REST API directly for the get container properties until you get a 404 (not found). That would mean the container is gone. http://msdn.microsoft.com/en-us/library/dd179370.aspx


A try/catch approach:

try {    while($true){        Get-AzureStorageContainer -Name "myContainer -ErrorAction stop        sleep -s 100    }} catch {      write-host "no such container"      # step 2 action}


This works

$containerDeleted = $falsewhile(!$containerDeleted) {    Try {          Write-Host "Try::New-AzureStorageContainer"        New-AzureStorageContainer -Name $storageContainerName -Permission Off -Context $context -Verbose -ErrorAction Stop        $containerDeleted = $true    } catch [Microsoft.WindowsAzure.Storage.StorageException] {        Start-Sleep -s 5    }}

If you look into the error message being returned the exception code it is container being deleted