我想使用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-name
和collection-name
。其他参数因此错误而失败:
az : ERROR: az: error: unrecognized arguments: --url-connection:https://myaccount.documents.azure.com:443
--key:secretKey
答案 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
}