如何为 Azure CLI 使用 Powershell splatting
How to use Powershell splatting for 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-name
和 collection-name
。其他参数失败并出现此错误:
az : ERROR: az: error: unrecognized arguments: --url-connection:https://myaccount.documents.azure.com:443
--key:secretKey
经过一番折腾,我最终使用了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
}
我想使用 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
经过一番折腾,我最终使用了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
}