代码之家  ›  专栏  ›  技术社区  ›  Pradeep

用于在每个Azure Cosmos数据库帐户中创建集合和数据库的PowerShell脚本

  •  0
  • Pradeep  · 技术社区  · 7 年前

    我使用ARM模板创建了AzureCosmosDB。但使用ARM模板,您可以在cosmos数据库中创建默认集合和数据库。

    我有多个Cosmos数据库帐户,对于每个Azure Cosmos数据库,我想创建一个带有数据库的集合,并在其中插入一些虚拟记录。

    因此,是否有人建议我如何编写PowerShell脚本,以便在每个Azure Cosmos数据库中创建默认集合和数据库。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jay Gong    7 年前

    请参阅以下PowerShell脚本以在Cosmos数据库中创建带有数据库的集合:

    $primaryKey = ConvertTo-SecureString -String '<your connectString>' -AsPlainText -Force
    $cosmosDbContext = New-CosmosDbContext -Account 'jaygongcosmos' -Database 'db' -Key $primaryKey
    New-CosmosDbCollection -Context $cosmosDbContext -Id 'MyNewCollection' -OfferThroughput 2500
    

    关于插入虚拟文档:

    0..9 | Foreach-Object {
        $document = @"
    {
        `"id`": `"$([Guid]::NewGuid().ToString())`",
        `"content`": `"Some string`",
        `"more`": `"Some other string`"
    }
    "@
    New-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'MyNewCollection' -DocumentBody $document
    }
    

    详情请参阅 article .


    对于多个帐户,请使用以下代码:

    $array= '<your cosmos db account name>',.......
    foreach($item in $array){
    
        $key = Get-CosmosDbAccountMasterKey -Name $item -ResourceGroupName 'jaygong'
        $cosmosDbContext = New-CosmosDbContext -Account $item -Key $key
        New-CosmosDbCollection -Context $cosmosDbContext -Id 'Mytest1' -OfferThroughput 2500 -Database 'db'
    
    }
    
    推荐文章