5
votes

I am creating a PowerShell script to perform several steps and one of them involves an Azure Storage Container removal:

Remove-AzureStorageContainer ....

The next step is dependant of this removal is done.

How can I be made aware that the previous REMOVE was successfully performed so as to continue execution on the next step ?

Something like;

while(Test-AzureStorageContainerExist "mycontainer")
{
    Start-Sleep -s 100
}
<step2>

Unfortunately 'Test-AzureStorageContainerExist' doesn't seem available. :)

3
one way is to try and create the container again. whilst it is till being deleted you will get a 409 conflict responseSam Holder
Thanks @SamHolder but the final approach is a bit more complex because it is related to HdInsight and cluster creation itself. In summary I cannot create container by myself but yours is a good approach in other scenaries.ferpega

3 Answers

8
votes

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

0
votes

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
}
0
votes

This works

$containerDeleted = $false
while(!$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