【问题标题】:Create azure storage container through powershell in new portal在新门户中通过 powershell 创建 azure 存储容器
【发布时间】: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 存储容器?

【问题讨论】:

    标签: powershell azure azure-storage


    【解决方案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

        【讨论】:

          猜你喜欢
          • 2018-01-19
          • 2019-10-02
          • 2015-12-25
          • 2021-03-19
          • 1970-01-01
          • 2018-12-03
          • 2020-03-26
          • 2017-02-22
          • 1970-01-01
          相关资源
          最近更新 更多