【问题标题】:Get Container list in one azure storage account and create the same in another storage account在一个 Azure 存储帐户中获取容器列表并在另一个存储帐户中创建相同的
【发布时间】:2018-08-10 21:06:49
【问题描述】:

团队,

提前致谢,我是一名 IT 专业人士,也在学习 PowerShell。我在不同的差异区域中有 2 个存储帐户 Storage A 和 Storage B,我想在辅助存储帐户中创建相同的容器。

我发现下面的命令可以获取主存储帐户中所有容器的列表,然后我可以使用 foreach 循环在辅助存储上创建容器。但我想确保在辅助存储帐户中,如果它已经具有容器名称,那么我的命令应该跳过该容器并继续创建下一个存储容器。

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

【问题讨论】:

  • 您是否测试过它在容器已经存在的情况下的行为?无论目标容器是否存在,都只为每个容器调用它是有意义的。如果它返回错误,您可以在 PowerShell 中处理它,不是吗?但首先...只是测试看看会发生什么。

标签: azure powershell azure-storage


【解决方案1】:

对于我们从管道(|)获得的值,“VM”是针对“-Property”开关的错误参数。 请使用正确的住宿名称。我仅以上面的“VM”为例。 VM 是 Esxi(VC) 环境中的属性。

请运行以下命令并检查输出。

Get-AzureStorageContainer -Context $Destinationcontext

它应该为您提供如下输出: Property_ID Property_NAME Property_3


值 1 值_A 值_1A 值 2 值_B 值_2A

所以,现在正确的语法应该是:

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

:)

【讨论】:

    【解决方案2】:

    希望这会有所帮助!

    $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 }
    

    【讨论】:

    • 收到以下错误。 ==================================== New-AzureStorageContainer:无法验证参数“名称”上的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后重试该命令。在 line:1 char:96 + ... VM| ForEach-object { New-AzureStorageContainer -Name $_.Name -Contex ... + ~~~~~~~ ======================== =========== 这是用于存储帐户的,它不包含任何名为 VM 的属性。
    猜你喜欢
    • 2016-07-17
    • 2015-10-22
    • 1970-01-01
    • 2021-12-23
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    相关资源
    最近更新 更多