سؤال

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. :)

هل كانت مفيدة؟

المحلول

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 = $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

if you are here because of the title and not the problem OP had (it's google first hit), try this:

# crate a context of the storage account.
# this binds the following commands to a certain storage account.
$context = New-AzStorageContext -StorageAccountName $AccountName

# check whether the container exists.
Get-AzStorageContainer -Context $context -Name $containerName

# check the return value of the last call.
if ($? -eq $false) {
    # do what you need to do when it does not exist.
} else {
    # do what you need to do when it does exist.
}

For me at least catching the exception did not work, this however did.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top