【问题标题】:How to use Powershell splatting for Azure CLI如何为 Azure CLI 使用 Powershell splatting
【发布时间】:2018-10-17 11:44:11
【问题描述】:

我想使用Powershell splatting 有条件地控制用于某些 Azure CLI 调用的参数。专门用于创建 CosmosDb 集合。

目标是这样的:

$params = @{
    "db-name" = "test";
    "collection-name"= "test2";
    # makes no difference if I prefix with '-' or '--'
    "-key" = "secretKey";
    "url-connection" = "https://myaccount.documents.azure.com:443"
    "-url-connection" = "https://myaccount.documents.azure.com:443"
}

az cosmosdb collection create @params

很遗憾,这只适用于db-namecollection-name。其他参数因此错误而失败:

az : ERROR: az: error: unrecognized arguments: --url-connection:https://myaccount.documents.azure.com:443 
--key:secretKey

【问题讨论】:

    标签: azure powershell azure-cli


    【解决方案1】:

    经过一番折腾,我最终使用了array splatting

    $params = "--db-name", "test", "--collection-name", "test2", 
        "--key", "secretKey",
        "--url-connection", "https://myaccount.documents.azure.com:443"
    
    az cosmosdb collection create @params 
    

    现在我可以这样做了:

    if ($collectionExists) {
        az cosmosdb collection update @colParams @colCreateUpdateParams
    } else {
        # note that the partition key cannot be changed by update
        if ($partitionKey -ne $null) {
            $colCreateUpdateParams += "--partition-key-path", $partitionKey
        }
        az cosmosdb collection create @colParams @colCreateUpdateParams
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多