在新门户中通过powershell创建azure存储容器

时间:2015-10-05 18:16:20

标签: powershell azure azure-storage

我想通过powershell在现有存储帐户中创建一个azure存储容器。我尝试了以下命令:

Set-AzureSubscription -CurrentStorageAccountName "storageaccountv1" -SubscriptionId $SubscriptionId
New-AzureStorageContainer -Name $ContainerName -Permission Off

对于那些知道的人,Azure有两种类型的存储帐户:v1,v2。 v1帐户是http://manage.windowsazure.com/和v2中可用的帐户,可以在http://portal.azure.com/中创建。虽然命令New-AzureStorageContainer正在使用v1中的存储帐户,但该命令不适用于v2中的存储帐户。它出现以下错误:

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

任何人都可以告诉如何通过powershell在v2中创建azure存储容器?

4 个答案:

答案 0 :(得分:7)

如果使用StorageAccountContext是一个选项,您可以尝试以下操作:

$ctx = New-AzureStorageContext -StorageAccountName "account-name" -StorageAccountKey "account-key" 
New-AzureStorageContainer -Name "container-name" -Context $ctx

答案 1 :(得分:4)

Azure PowerShell 1.0预览版(https://azure.microsoft.com/en-us/blog/azps-1-0-pre/)即将发布。

使用Azure PowerShell 1.0 Preview,您可以运行以下cmdlet:

 Login-AzureRMAccount -SubscriptionId [SubscriptionID]
 Set-AzureRmCurrentStorageAccount -StorageAccountName [accountName] -ResourceGroupName [ResourceGroupName]

然后,您可以在没有上下文的情况下使用资源模式帐户(在新门户中创建)运行“New-AzureStorageContainer”。

BTW,“Set-AzureSubscription”仅适用于服务模式帐户,不适用于资源模式帐户。

答案 2 :(得分:0)

这是幂等代码

[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"

这是针对:版本4.3.1(AzureRM的)

  

get-module -listavailable -Name" AzureRM"

答案 3 :(得分:0)

这有帮助,但我需要ARM命令而不是经典方法。 希望它有所帮助

$NewRGName = 'Lab' $NewRGLocation = "West US" $NewStrAccName ="Labstorage2" $SkuName = 'Standard_LRS'
 # Create new storage account New-AzureRmStorageAccount -Location $NewRGLocation -Name $NewStrAccName -ResourceGroupName $NewRGName -SkuName $SkuName