In one of my automation, I need to store the reports on daily basis in Azure storage account in automated way so i need to have separate container to store all my reports. So i need to ensure my container is available or need to be create before pushing my report to the container. This article will have detail explain about How to Create a Container in Azure Storage Account using PowerShell.
To create containers with PowerShell, call the New-AzStorageContainer cmdlet. There are no limits to the number of blobs or containers that can be created within a storage account. Containers cannot be nested within other containers.
New to storage account?
One of the core services within Microsoft Azure is the Storage Account service. There are many service that utilize Storage Accounts for storing data, such as Virtual Machine Disks, Diagnostics logs (specially application log), SQL backups and others. You can also use the Azure Storage Account service to store your own data; such as blobs or binary data.
As per MSDN, Azure blob storage allows you to store large amounts of unstructured object data. You can use blob storage to gather or expose media, content, or application data to users. Because all blob data is stored within containers, you must create a storage container before you can begin to upload data.
How to Create Containers in Azure Storage Account using powershell
Step: 1 Get the prerequisite inputs
1 2 3 4 5 6 |
# prerequisite Parameters $resourceGroupName="rg-strg-we-01" $storageAccountName="sadgtlautomationshrd01" $storageContainerName="sql" |
Step: 2 Connect to your Azure subscription
1 2 3 4 |
# Connect to your Azure subscription az login --service-principal -u "c80ff721-049c-48e0-6b95-366d6422f464" -p "c2B8Q~MTCrPrNks6Lr3Daz95WF32jXBAtXACccAV" --tenant "2cf8ba3-a403-423b-ba39-f7883c211637" |
Step: 3 Get the storage account to Check the container exit or not
1 2 3 4 5 6 7 |
# Get the storage account to check container exist or need to be create $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName # Get the storage account context $context = $storageAccount.Context |
Step: 4 Check the container exist before creating
We need to use New-AzStorageContainer cmdlet creates an Azure storage container
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## Check if the storage container exists if(Get-AzStorageContainer -Name $storageContainerName -Context $context -ErrorAction SilentlyContinue) { Write-Host -ForegroundColor Magenta $storageContainerName "- container already exists." } else { Write-Host -ForegroundColor Green $storageContainerName "the requested container does not exist." ## Create a Container in Azure Storage Account New-AzStorageContainer -Name $storageContainerName -Context $context -Permission off } |
Full Code for Create a Container in Azure Storage Account
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
## Input Parameters $resourceGroupName="rg-strg-we-01" $storageAccountName="sadgtlautomationshrd01" $storageContainerName="sql" ## Connect to your Azure subscription az login --service-principal -u "0ff7c821-049c-48e0-96b5-d6366422f464" -p "c2B8Q~MTCrPrNks6Lr3Daz95WF32jXBAtXACccAV" --tenant "f8ba2c23-a403-423b-ba39-f7883c211637" ## Function to create the storage container Function CreatenewStorageContainer { ## Get the storage account to check container exist or need to be create $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName ## Get the storage account context $context = $storageAccount.Context write-host $context.BlobEndPoint ## Check if the storage container exists if(Get-AzStorageContainer -Name $storageContainerName -Context $context -ErrorAction SilentlyContinue) { Write-Host -ForegroundColor Magenta $storageContainerName "- container already exists." } else { Write-Host -ForegroundColor Green $storageContainerName "the requested container does not exist." ## Create a new Azure Storage container New-AzStorageContainer -Name $storageContainerName -Context $context -Permission off } } ## Call the function to execute CreatenewStorageContainer |
Output:
Note:
To list down all the containers present in the specific storage account, you can use
Get-AzStorageContainer -Name $containerName -Context $context
Leave A Comment