Lista och hämta databaser och containrar för Azure Cosmos DB – API för NoSQL

GÄLLER FÖR: NoSQL

Anteckning

Vi rekommenderar att du använder Azure Az PowerShell-modulen för att interagera med Azure. Se Installera Azure PowerShell för att komma igång. Information om hur du migrerar till Az PowerShell-modulen finns i artikeln om att migrera Azure PowerShell från AzureRM till Az.

Det här exemplet kräver Azure PowerShell Az 5.4.0 eller senare. Kör Get-Module -ListAvailable Az för att se vilka versioner som är installerade. Om du behöver installera kan du läsa Installera Azure PowerShell modul.

Kör Connect-AzAccount för att logga in på Azure.

Exempelskript

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Show list and get operations for accounts, databases, and containers
# --------------------------------------------------
# Variables - ***** SUBSTITUTE YOUR VALUES *****
$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "myaccount" # Must be all lower case
$databaseName = "myDatabase"
$containerName = "myContainer"
# --------------------------------------------------

Write-Host "List all accounts in a resource group"
Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName

Write-Host "Get an account in a resource group"
Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName `
    -Name $accountName

Write-Host "List all databases in an account"
Get-AzCosmosDBSqlDatabase -ResourceGroupName $resourceGroupName `
    -AccountName $accountName

Write-Host "Get a database in an account"
Get-AzCosmosDBSqlDatabase -ResourceGroupName $resourceGroupName `
    -AccountName $accountName -Name $databaseName

Write-Host "List all containers in an database"
Get-AzCosmosDBSqlContainer -ResourceGroupName $resourceGroupName `
    -AccountName $accountName -DatabaseName $databaseName 

Write-Host "Get a container in a database"
Get-AzCosmosDBSqlContainer -ResourceGroupName $resourceGroupName `
    -AccountName $accountName -DatabaseName $databaseName `
    -Name $containerName

Rensa distribution

När skriptexemplet har körts kan följande kommando användas för att ta bort resursgruppen och alla resurser som är kopplade till den.

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

Förklaring av skript

Det här skriptet använder följande kommandon. Varje kommando i tabellen länkar till kommandospecifik dokumentation.

Kommando Kommentarer
Azure Cosmos DB
Get-AzCosmosDBAccount Visar en lista över Azure Cosmos DB-konton eller ett angivet Azure Cosmos DB-konto.
Get-AzCosmosDBSqlDatabase Visar en lista över Azure Cosmos DB-databaser i ett konto eller hämtar en angiven Azure Cosmos DB-databas i ett konto.
Get-AzCosmosDBSqlContainer Visar en lista över Azure Cosmos DB-containrar i en databas eller hämtar en angiven Azure Cosmos DB-container i en databas.
Azure-resursgrupper
Remove-AzResourceGroup Tar bort en resursgrupp, inklusive alla kapslade resurser.

Nästa steg

Mer information om Azure PowerShell finns i Azure PowerShell-dokumentationen.