Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell check for Azure Storage Container existence

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

like image 576
ferpega Avatar asked Apr 22 '14 14:04

ferpega


People also ask

How do I run AzCopy in PowerShell?

Run AzCopy That way you can type azcopy from any directory on your system. If you choose not to add the AzCopy directory to your path, you'll have to change directories to the location of your AzCopy executable and type azcopy or . \azcopy in Windows PowerShell command prompts.

How do I find my Azure container name?

You could access them via your storage account in the azure portal, refer to the screenshot. Choose a container, you could see the blobs, including names, blob type, size,etc. thx for your reply. However, I believe that would to this which under name column I believe that it is the "blob name"/"container" name.

How do I get Azure storage account in PowerShell?

PowerShell Script Save the file as script. ps1. Write-Host -ForegroundColor Green "Retrieving the storage accounts..." Write-Host -ForegroundColor Green "Retrieving the storage accounts from specific resource group..."


1 Answers

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

like image 87
MikeWo Avatar answered Sep 28 '22 22:09

MikeWo