简体   繁体   中英

Get Container list in one azure storage account and create the same in another storage account

Team,

Thanks in Advance, I'm an IT pro and also learning PowerShell. I have 2 storage account Storage A and Storage B in different diff region and I want to create identical containers in the secondary storage account.

I've found below command which can get the list of all the containers in the primary storage account and then I can use the foreach loop to create the container on secondary storage. But I want to make sure that in the secondary storage account if it has the container name already then my command should skip that container and move on to create the next storage container.

Get-AzureStorageContainer -Context $storageAccountContext | ForEach-object { New-AzureStorageContainer -Name $_.Name -Context $Destinationcontext }

Hope this helps!

$storageAccountContext ="Storage_Account_A"
$Destinationcontext ="Storage_Account_B"

$Vm_in_A =@(Get-AzureStorageContainer -Context $storageAccountContext )
$Vm_in_b =@(Get-AzureStorageContainer -Context $Destinationcontext)
$Vm_to_be_created =@(Compare-Object -ReferenceObject $Vm_in_A -DifferenceObject $Vm_in_b )
$Vm_to_be_created|Select-object -Property VM| ForEach-object { New-AzureStorageContainer -Name $_.Name -Context $Destinationcontext }

"VM" is incorrect argument against the "-Property" switch for the values we are getting from pipe(|). Please use the correct name of the property. I have used "VM" above only as an example. VM is property in Esxi(VC) environment.

Please run the below command & check the output.

Get-AzureStorageContainer -Context $Destinationcontext

It should give you output something like below: Property_ID Property_NAME Property_3


Value 1 Value_A Value_1A Value 2 Value_B Value_2A

So, now the correct syntax should be:

$Vm_to_be_created|Select-object -Property "Property_ID"| ForEach-object { New-AzureStorageContainer -Name $_.Name -Context $Destinationcontext }

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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