使用PowerShell创建和删除Azure Cosmos数据库

When managing Azure Cosmos DB, we can use the Azure portal and create resources through the interface or use the command line in the portal and create resources. PowerShell also supports some functionality for creating and managing these resources, which can help development teams automate the creation of these databases for quick creations, unit and security tests, removals if the resources aren’t required following the tests. We can also use these scripts for creating templates that we may use in multi-scaling creations (like databases in a group designed for horizontal scale). Generally, in one-off situations, the Azure Portal will suffice for deployments if there is a cost to develop automation that is not required. In this tip, we’ll look at the process of creating a blank and removing the same Azure Cosmos DB.

在管理Azure Cosmos DB时,我们可以使用Azure门户并通过界面创建资源,也可以使用门户中的命令行来创建资源。 PowerShell还支持一些用于创建和管理这些资源的功能,这些功能可以帮助开发团队自动创建这些数据库,以进行快速创建,单元和安全性测试,以及在测试后不需要资源的情况下进行删除。 我们还可以使用这些脚本来创建可在多尺度创建中使用的模板(例如,为水平尺度设计的组中的数据库)。 通常,在一次性情况下,如果不需要开发自动化成本,则Azure门户足以满足部署要求。 在本技巧中,我们将研究创建空白并删除相同的Azure Cosmos DB的过程。

安装PowerShell Az (Installing PowerShell Az)

Before we begin creating and using PowerShell with Azure Cosmos DB, we’ll need to install the PowerShell Az module first. Microsoft recommends using this module for future PowerShell development in Azure – the AzureRm module will be supported through 2020, so this provides developers time to make the switch. This Az module requires PowerShell 5.1 or greater. Because we want to keep permissions restricted when doing this, we’ll scope this module to our current user (the alternative is to run PowerShell as an administrator). According to Microsoft, the below will allow us to install this module scoped to the current user, so that we can avoid installing with the administrator account.

在开始使用Azure Cosmos DB创建和使用PowerShell之前,我们需要首先安装PowerShell Az模块。 Microsoft建议将此模块用于Azure中将来的PowerShell开发– AzureRm模块将在2020年之前受支持,因此这为开发人员提供了进行转换的时间。 此Az模块需要PowerShell 5.1或更高版本。 因为我们要在执行此操作时限制权限,所以我们将此模块的作用域设置为当前用户(替代方法是以管理员身份运行PowerShell)。 根据Microsoft的说法,以下内容将使我们能够安装范围仅限于当前用户的该模块,从而避免使用管理员帐户进行安装。

Install-Module -Name Az -Scope CurrentUser

If we can install with the administrator account, we can remove the -Scope CurrentUser and run the script. We’ll be using this module to create our Cosmos database account and we’ll also see that there are alternatives provided in the portal that use the AzureRm module.

如果可以使用管理员帐户进行安装,则可以删除-Scope CurrentUser并运行脚本。 我们将使用此模块创建我们的Cosmos数据库帐户,并且还将看到门户中提供了使用AzureRm模块的替代方法。

从Azure门户获取参数 (Getting Parameters from the Azure Portal)

When creating an Azure Cosmos DB account, we can use the interface in Azure Portal to get all the information we’ll need – we can use contrived values if we want to see the full creation process, or we can actually create the account and remove it after creation so that we know what we’ll need for our script. The below images show what we’ll see when we set up a new Cosmos account resource and contrived values will work for progressing through the next screens to get the final validation – nothing will have been created yet until we confirm on the final step.

创建Azure Cosmos数据库帐户时,我们可以使用Azure门户中的界面来获取所需的所有信息–如果希望查看完整的创建过程,可以使用人为的值,或者我们可以实际创建该帐户并删除创建之后,它使我们知道脚本需要什么。 下图显示了在设置新的Cosmos帐户资源时将看到的内容,人为设计的值将在接下来的屏幕中进行操作以进行最终验证-在我们确认最后一步之前,尚未创建任何内容。

Creating an Azure Cosmos DB account in the Azure Portal

Creating an Azure Cosmos DB account in the Azure Portal.

在Azure门户中创建Azure Cosmos DB帐户。

Before the creation takes place in the portal, we see the Review + create with the values we’ve entered which we’ll be using for some of our parameters that will be required. On the bottom right of the below image, we’ll also see an option Download a template for automation that we can use to create this Cosmos account with languages like PowerShell, .NET, Ruby, etc (seen in the second image). We’ll be using the Az module, so we’ll be using a different route, but it is worth mentioning that the below template creation technique is an option that developers can use. Depending on how many options we want configured, it may be worth using the template, or it may be faster using the Az module. Since we’ll be customizing our own script using the Az module, we’ll use it.

在门户中进行创建之前,我们将看到带有已输入值的Review + create ,这些值将用于一些必需的参数。 在下图的右下方,我们还将看到一个选项“ 下载用于自动化的模板” ,我们可以使用它来使用诸如PowerShell,.NET,Ruby等语言创建该Cosmos帐户(在第二张图中可见)。 我们将使用Az模块,因此将使用不同的路由,但是值得一提的是,以下模板创建技术是开发人员可以使用的一种选择。 根据我们要配置的选项数量,可能值得使用模板,或者使用Az模块可能会更快。 由于我们将使用Az模块自定义自己的脚本,因此将使用它。

We can download template files for creating Azure Cosmos DBs as an alternative to the Az module or portal

We can download template files for creating Azure Cosmos DBs as an alternative to the Az module or portal.

我们可以下载用于创建Azure Cosmos数据库的模板文件,以替代Az模块或门户。

The options we see when we select the download template for automation

The options we see when we select the download template for automation.

当选择自动化下载模板时,会看到这些选项。

The above two images – the confirmation screen and template data – show some of the options we will use when executing our Az scripts to create our Azure Cosmos DB. We can use these screens if we’re customizing a Cosmos account to specific criteria and we need to find the values. We’ll note a few of these options along with their values that will be parameters later in our script:

上面的两个图像(确认屏幕和模板数据)显示了执行Az脚本创建Azure Cosmos DB时将使用的一些选项。 如果要根据特定条件自定义Cosmos帐户,并且需要查找值,则可以使用这些屏幕。 我们将注意到其中一些选项以及它们的值,这些值稍后将在脚本中用作参数:

  • Resource Group: OurResourceGroup 资源组 :OurResourceGroup
  • Location: Australia East 地点 :澳大利亚东部
  • apiVersion: 2015-04-08 apiVersion :2015-04-08
  • type: Microsoft.DocumentDb/databaseAccounts 类型 :Microsoft.DocumentDb / databaseAccounts
  • databaseAccountOfferType: Standard databaseAccountOfferType :标准

In addition, we see that we’re not writing to multiple locations and this isn’t part of a virtual network, so these options are not required for the later properties of this Cosmos account. If these were options we needed, we would specify them as properties. Depending on your needs, when you run through the above steps and select different options, you will see these options specified in these screens to help you know how to script these with the Az module.

此外,我们看到我们没有写到多个位置,这也不是虚拟网络的一部分,因此此Cosmos帐户的更高属性不需要这些选项。 如果这些是我们需要的选项,则将其指定为属性。 根据您的需要,当您执行上述步骤并选择其他选项时,您会在这些屏幕中看到指定的这些选项,以帮助您了解如何使用Az模块编写脚本。

创建和删除脚本 (The Create and Remove Script)

In the above images, we went through the process of selecting the options we wanted for our Azure Cosmos DB, but we didn’t create it. We will now create our database by executing a PowerShell script that uses many of the parameters we see above this. First, we will log into Azure and set our properties accordingly – using the resource group, location, API version, type, and the offer type. Before we start creating our re-usable function, we’ll look at the options we need to include to create our Cosmos account we have shown from the portal steps. We can run a test creation on this step, or wait until our script is wrapped in a function to test the creation.

在上面的图像中,我们经历了为Azure Cosmos DB选择所需选项的过程,但是我们没有创建它。 现在,我们将通过执行PowerShell脚本来创建数据库,该脚本使用上面我们看到的许多参数。 首先,我们将登录到Azure并使用资源组,位置,API版本,类型和商品类型相应地设置属性。 在开始创建可重用功能之前,我们将查看创建门户网站所显示的Cosmos帐户所需的选项。 我们可以在此步骤上运行测试创建,或者等到脚本包含在函数中以测试创建时再进行测试。

Connect-AzAccount
 
$api = "2015-04-08"
$rGroup = "OurResourceGroup"
$location = "Australia East"
$cosmosdb = "testscriptcosmos"
 
 
### We must specify all properties we want - in this example a standard account
$properties = @{"databaseAccountOfferType"="Standard"}
 
 
New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion $api `
    -ResourceGroupName $rGroup -Location $location -Name $cosmosdb -PropertyObject $properties

After we execute the script, our Azure Cosmos DB is created

After we execute the script, our Azure Cosmos DB is created.

执行脚本后,将创建Azure Cosmos数据库。

After we run the above script (or if we run the below script to create the account), we’ll note that it takes a few minutes before the account is created. Once it’s created, we’ll see testscriptcosmos (or the name you chose) in our list within the Azure Portal. Now, for re-usability, we’ll wrap our functionality within a function that we can re-use that starts with a Cosmos account creation. Because properties may differ by environment, we will leave it outside the function – if an environment always specifies the same properties, we can keep that within the process block.

运行上面的脚本后(或者如果我们运行下面的脚本来创建帐户),我们将注意到创建帐户需要花费几分钟。 创建完成后,我们将在Azure门户的列表中看到testscriptcosmos (或您选择的名称)。 现在,为了实现可重用性,我们将把功能包装在一个可以重用的功能中,该功能从Cosmos帐户创建开始。 因为属性可能因环境而异,所以我们将其保留在函数之外–如果环境始终指定相同的属性,则可以将其保留在流程块中。

$properties = @{"databaseAccountOfferType"="Standard";}
 
Function Create-AzureCosmosDB {
    Param(
        [Parameter(Mandatory=$true)][string]$api
        , [Parameter(Mandatory=$true)][string]$rGroup
        , [Parameter(Mandatory=$true)][string]$location
        , [Parameter(Mandatory=$true)][string]$cosmosdb
    )
    Process
    {
        Connect-AzAccount
 
        New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion $api `
            -ResourceGroupName $rGroup -Location $location -Name $cosmosdb -PropertyObject $properties
    }
}
 
 
Create-AzureCosmosDB -api "2015-04-08" -rGroup "OurResourceGroup" -location "Australia East" -cosmosdb "testscriptcosmos"

Now that we have our function to create the Azure Cosmos DB, we also want the ability to remove it. Because creating and removing in testing often take place together in increments – we create an account, we run unit and security tests, and we remove the account, we’ll take the same above function and add the ability to specify removing the Cosmos account. In other contexts like creating the database account for use, we may not need to include a remove step and the same is true if we use the script to automatically create another database for scaling.

既然我们具有创建Azure Cosmos DB的功能,我们还希望能够删除它。 因为在测试中创建和删除通常是一起进行的-我们创建一个帐户,运行单元测试和安全测试,然后删除该帐户,因此我们将采用上述相同功能,并添加指定删除Cosmos帐户的功能。 在其他情况下(例如创建要使用的数据库帐户),我们可能不需要包括删除步骤,如果我们使用脚本自动创建另一个数据库进行扩展,则情况同样如此。

$properties = @{"databaseAccountOfferType"="Standard";}
 
  Function CreateDrop-AzureCosmosDB {
    Param(
        [Parameter(Mandatory=$true)][string]$api
        , [Parameter(Mandatory=$true)][string]$rGroup
        , [Parameter(Mandatory=$true)][string]$location
        , [Parameter(Mandatory=$true)][string]$cosmosdb
        , [Parameter(Mandatory=$true)][ValidateSet("Create","Remove")]$option
    )
    Process
    {
        Connect-AzAccount
        
        switch ($option)
        {
            "Create" {
            New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion $api `
                -ResourceGroupName $rGroup -Location $location -Name $cosmosdb -PropertyObject $properties
            }
            "Remove" {
            Remove-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion $api `
                -ResourceGroupName $rGroup -Name $cosmosdb
            }
        }
    }
}
 
 
CreateDrop-AzureCosmosDB -api "2015-04-08" -rGroup "OurResourceGroup" -location "Australia East" -cosmosdb "testscriptcosmos" -option Remove

We could demarcate the create or remove on its own function, but the above gives us the ability to re-use our function and its parameters on the same object that was created for testing situations where we don’t want to keep what we’ve created. Re-use in creation and removals tend to come up frequently in temporary testing situations. In a similar manner for different creation contexts, we may not want to wrap any functionality because we may customize each account with significant differences.

我们可以根据自己的功能来划分create或remove,但是上面的功能使我们能够在为测试我们不想保留自己所拥有的东西而创建的同一对象上重用我们的函数及其参数创建。 在临时测试情况下,经常会重复使用创建和删除操作。 对于不同的创建上下文,采用类似的方式,我们可能不希望包装任何功能,因为我们可能会自定义具有明显差异的每个帐户。

Like creating an Azure Cosmos DB account takes a few minutes, removing it can also take some time.

就像创建Azure Cosmos数据库帐户需要花费几分钟,删除它也可能需要一些时间。

Our Azure Cosmos DB no longer appears in the portal once it’s removed

Our Azure Cosmos DB no longer appears in the portal once it’s removed.

删除后,我们的Azure Cosmos DB将不再出现在门户中。

结论 (Conclusion)

As we see, we can create or remove an Azure Cosmos DB quickly with PowerShell’s Az module. This may help us quickly validate a unit and security test without committing to a Cosmos account (create, test, remove), or we may use this as a template to automate the creation of resources or scaled resources. Finally, there may be situations where the alternatives are faster or more appropriate. We’ve also seen that we can use templates provided through the Azure Portal that are supported in several languages and we can create the Azure Cosmos DB account through the portal if that is the fastest route, such as situations where we only need on database account.

如我们所见,我们可以使用PowerShell的Az模块快速创建或删除Azure Cosmos数据库。 这可以帮助我们快速验证单元和安全性测试,而无需提交Cosmos帐户(创建,测试,删除),或者可以将其用作自动创建资源或扩展资源的模板。 最后,在某些情况下,替代方法可能更快或更合适。 我们还看到,我们可以使用通过Azure门户提供的受多种语言支持的模板,并且如果这是最快的途径,则可以通过门户创建Azure Cosmos DB帐户,例如仅需要数据库帐户的情况。

目录 (Table of contents)

Creating and Removing Azure Cosmos DBs with PowerShell
Getting and Updating Connection Information for Azure Cosmos DB
Creating and Removing Databases with PowerShell In Azure Cosmos DB
Increasing or Decreasing Scale for Azure Cosmos DB
Creating Containers with PowerShell For Azure Cosmos DB
使用PowerShell创建和删除Azure Cosmos数据库
获取和更新Azure Cosmos DB的连接信息
在Azure Cosmos DB中使用PowerShell创建和删除数据库
增加或减少Azure Cosmos DB的规模
使用PowerShell为Azure Cosmos DB创建容器

翻译自: https://www.sqlshack.com/creating-and-removing-azure-cosmos-dbs-with-powershell/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值