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

Posted

技术标签:

【中文标题】在新门户中通过 powershell 创建 azure 存储容器【英文标题】:Create azure storage container through powershell in new portal 【发布时间】:2016-01-02 11:58:38 【问题描述】:

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

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 存储容器?

【问题讨论】:

【参考方案1】:

如果可以选择使用StorageAccountContext,您可以尝试以下方法:

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

【讨论】:

这很有帮助。但希望有一种方法可以在不指定存储帐户密钥的情况下执行此操作。【参考方案2】:

Azure 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”仅适用于服务模式帐户,不适用于资源模式帐户。

【讨论】:

【参考方案3】:

这是幂等代码

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

【讨论】:

【参考方案4】:

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

【讨论】:

以上是关于在新门户中通过 powershell 创建 azure 存储容器的主要内容,如果未能解决你的问题,请参考以下文章

如何在Win10中通过powershell修改Jupyter notebook的默认目录

$ Azure 门户帐户创建的路径环境变量

在 Netbeans 中通过 SSH 连接到 MySql db

共同管理员在新的 Azure 门户中看不到订阅

powershell iSCSI门户和目标配置

Azure经典门户创建VM,如何设置使用静态IP地址?