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

Create azure storage container through powershell in new portal

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

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

对于那些知道的人来说,Azure 有两种类型的存储帐户:v1、v2。 v1 帐户是通过 http://manage.windowsazure.com/ and v2 that can be created in 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 存储容器?

如果可以使用 StorageAccountContext,您可以尝试以下操作:

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

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

使用 Azure PowerShell 1.0 预览版,您可以 运行 以下 cmdlet:

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

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

顺便说一句,"Set-AzureSubscription"仅适用于服务模式帐户,不适用于资源模式帐户。

这里是幂等代码

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

这有帮助,但我需要 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