繁体   English   中英

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

[英]Create azure storage container through powershell in new portal

我想通过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存储容器?

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

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

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

这是幂等代码

[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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM