2015-10-05 55 views
1

PowerShell中創建的Azure存儲容器,我想在現有的存儲帳戶創建Azure存儲容器,通過powershell.I曾嘗試以下命令:通過新的門戶

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

對於那些誰知道,有天青兩種類型的存儲帳戶: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中創建一個蔚藍存儲容器?

回答

6

如果使用StorageAccountContext是一個選項,你可以嘗試以下方法:

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

這幫助。但希望有一種方法可以在不指定存儲帳戶密鑰的情況下執行此操作。 – Romonov

3

Azure中的PowerShell 1.0預覽版(https://azure.microsoft.com/en-us/blog/azps-1-0-pre/)是釋放。沒有上下文

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

然後你就可以用資源模式帳戶運行「新AzureStorageContainer」(創建新門戶):

隨着Azure中的PowerShell 1.0預覽,您可以以下cmdlet運行。

順便說一下,「Set-AzureSubscription」僅適用於服務模式帳戶,不適用於資源模式帳戶。

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" 

這種情況實在有違:(AzureRM)版本4.3.1

get-module –listavailable -Name "AzureRM"