如何对Azure CLI使用Powershell splatting

时间:2018-10-17 11:44:11

标签: azure powershell azure-cli

我想使用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

1 个答案:

答案 0 :(得分:3)

经过一番来回,我最终使用了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
}