如何使用PowerShell splatting来操作Azure CLI

5
我想使用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个回答

11

经过一些讨论,我最终使用了数组拆分

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接