I want to create an azure storage container in an existing storage account, through powershell.I have tried the following commands:
Set-AzureSubscription -CurrentStorageAccountName "storageaccountv1" -SubscriptionId $SubscriptionId
New-AzureStorageContainer -Name $ContainerName -Permission Off
For those who know, Azure has two types of storage accounts: v1, v2. v1 accounts are the ones that are available through the http://manage.windowsazure.com/ and v2 that can be created in http://portal.azure.com/. While the command New-AzureStorageContainer is working with a storage account in v1, the command is not working for a storage account in v2. It is giving the following error:
New-AzureStorageContainer : ResourceNotFound: The storage account 'storageaccountv2' was not found.
At CreateStorageContainer.ps1:31 char:1
+ New-AzureStorageContainer -Name $ContainerName -Permission Off
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureStorageContainer], CloudException
+ FullyQualifiedErrorId : CloudException,Microsoft.WindowsAzure.Commands.Storage.Blob.Cmdlet.NewAzureStorageContainerCommand
Can anyone tell how to create an azure storage container in v2 through powershell?
If using StorageAccountContext
is an option, you can try the following:
$ctx = New-AzureStorageContext -StorageAccountName "account-name" -StorageAccountKey "account-key"
New-AzureStorageContainer -Name "container-name" -Context $ctx
Azure PowerShell 1.0 Preview (https://azure.microsoft.com/en-us/blog/azps-1-0-pre/) is release.
With Azure PowerShell 1.0 Preview, you can run following cmdlets:
Login-AzureRMAccount -SubscriptionId [SubscriptionID]
Set-AzureRmCurrentStorageAccount -StorageAccountName [accountName] -ResourceGroupName [ResourceGroupName]
Then you can run "New-AzureStorageContainer" with resource mode account (create in new portal) without context.
BTW, "Set-AzureSubscription" is only for Service mode account, not applicable to resource mode account.
Here is the idempotent code
[System.String]$script:ResourceGroupNameVariable = 'resourcegroupnameone'
[System.String]$script:StorageAccountNameVariable = 'storacctnameone'
[System.String]$script:ContainerNameVariable = 'containernameone'
Write-Host "Start Container Create"
#Get/Set the AzureRmStorageAccountKey
# the below -Name parameter may be -AccountName according to documentation
[System.Object[]]$currentAzureRmStorageAccountKeys = Get-AzureRmStorageAccountKey -ResourceGroupName $script:ResourceGroupNameVariable -Name $script:StorageAccountNameVariable;
#Write-Host "about to currentAzureRmStorageAccountKeys.GetType"
#Write-Output $currentAzureRmStorageAccountKeys.GetType().FullName
### Create the AzureStorageContext (you always do this, regardless if the container itself exists or not)
[Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext]$currentAzureStorageContext = New-AzureStorageContext -StorageAccountName $script:StorageAccountNameVariable -StorageAccountKey $currentAzureRmStorageAccountKeys[0].Value;
#Write-Host "about to currentAzureStorageContext.GetType"
#Write-Output $currentAzureStorageContext.GetType().FullName
# Get/Set the AzureStorageContainer
[Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageContainer]$currentAzureStorageContainerCheck=Get-AzureStorageContainer -Context $currentAzureStorageContext -Name $script:ContainerNameVariable;
if(!$currentAzureStorageContainerCheck)
{
### The container does not already exist. Create a Blob Container in the Storage Account
New-AzureStorageContainer -Context $currentAzureStorageContext -Name $script:ContainerNameVariable;
}
else{
#Write-Host "about to currentAzureStorageContainerCheck.GetType"
#Write-Output $currentAzureStorageContainerCheck.GetType().FullName
#Write-Host "about to currentAzureStorageContainerCheck"
#$currentAzureStorageContainerCheck
}
Write-Host "End Container Create"
This runs against : Version 4.3.1 (of AzureRM )
get-module –listavailable -Name "AzureRM"
this helped but I needed the ARM command and not the classic method.
Hope it helps
$NewRGName = 'Lab'
$NewRGLocation = "West US"
$NewStrAccName ="Labstorage2"
$SkuName = 'Standard_LRS'
# Create new storage account
New-AzureRmStorageAccount -Location $NewRGLocation -Name $NewStrAccName -ResourceGroupName $NewRGName -SkuName $SkuName