快速入门:使用 Azure PowerShell 在 Azure Key Vault 中设置和检索密钥Quickstart: Set and retrieve a key from Azure Key Vault using Azure PowerShell

在本快速入门中,你将使用 Azure PowerShell 在 Azure Key Vault 中创建一个密钥保管库。In this quickstart, you create a key vault in Azure Key Vault with Azure PowerShell. Azure Key Vault 是一项云服务,用作安全的机密存储。Azure Key Vault is a cloud service that works as a secure secrets store. 可以安全地存储密钥、密码、证书和其他机密。You can securely store keys, passwords, certificates, and other secrets. 有关 Key Vault 的详细信息,可以参阅概述For more information on Key Vault you may review the Overview. Azure PowerShell 用于通过命令或脚本创建和管理 Azure 资源。Azure PowerShell is used to create and manage Azure resources using commands or scripts. 完成该操作后,你将存储密钥。Once that you have completed that, you will store a key.

如果没有 Azure 订阅,请在开始之前创建一个试用帐户If you don't have an Azure subscription, create a Trial account before you begin.

如果选择在本地安装并使用 PowerShell,则本教程需要 Azure PowerShell 模块 1.0.0 或更高版本。If you choose to install and use PowerShell locally, this tutorial requires Azure PowerShell module version 1.0.0 or later. 键入 $PSVersionTable.PSVersion 即可查找版本。Type $PSVersionTable.PSVersion to find the version. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Login-AzAccount -EnvironmentName AzureChinaCloud 来创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Login-AzAccount -EnvironmentName AzureChinaCloud to create a connection with Azure.

Login-AzAccount -EnvironmentName AzureChinaCloud

创建资源组Create a resource group

使用 New-AzResourceGroup 创建 Azure 资源组。Create an Azure resource group with New-AzResourceGroup. 资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed.

New-AzResourceGroup -Name ContosoResourceGroup -Location ChinaNorth

创建密钥保管库Create a Key Vault

接下来创建 Key Vault。Next you create a Key Vault. 执行此步骤时,需要一些信息:When doing this step, you need some information:

虽然在本快速入门中我们始终使用“Contoso KeyVault2”作为密钥保管库的名称,但你必须使用唯一的名称。Although we use "Contoso KeyVault2" as the name for our Key Vault throughout this quickstart, you must use a unique name.

  • 保管库名称 Contoso-Vault2。Vault name Contoso-Vault2.
  • 资源组名称 ContosoResourceGroupResource group name ContosoResourceGroup.
  • 位置:中国北部。Location China North.
New-AzKeyVault -Name 'Contoso-Vault2' -ResourceGroupName 'ContosoResourceGroup' -Location 'China North'

此 cmdlet 的输出显示新创建的密钥保管库的属性。The output of this cmdlet shows properties of the newly created key vault. 请记下下面列出的两个属性:Take note of the two properties listed below:

  • 保管库名称:在本示例中为 Contoso-Vault2Vault Name: In the example that is Contoso-Vault2. 将在其他密钥保管库 cmdlet 中使用此名称。You will use this name for other Key Vault cmdlets.
  • 保管库 URI:在本示例中为 https://Contoso-Vault2.vault.azure.cn/Vault URI: In this example that is https://Contoso-Vault2.vault.azure.cn/. 通过其 REST API 使用保管库的应用程序必须使用此 URI。Applications that use your vault through its REST API must use this URI.

创建保管库以后,你的 Azure 帐户是唯一能够对这个新的保管库执行任何操作的帐户。After vault creation your Azure account is the only account allowed to do anything on this new vault.

向密钥保管库中添加密钥Add a key to Key Vault

只需再执行几个步骤便可向保管库中添加密钥。To add a key to the vault, you just need to take a couple of additional steps. 此密钥可供应用程序使用。This key could be used by an application.

键入以下命令,创建名为 ExampleKey 的密钥:Type the commands below to create a called ExampleKey :

Add-AzKeyVaultKey -VaultName 'Contoso-Vault2' -Name 'ExampleKey' -Destination 'Software'

现在,可以通过 URI 来引用已添加到 Azure Key Vault 的此密钥。You can now reference this key that you added to Azure Key Vault by using its URI. 使用 https://Contoso-Vault2.vault.azure.cn/keys/ExampleKey 获取当前版本。Use https://Contoso-Vault2.vault.azure.cn/keys/ExampleKey to get the current version.

若要查看以前存储的密钥,请使用以下命令:To view previously stored key:

Get-AzKeyVaultKey -VaultName 'Contoso-Vault2' -KeyName 'ExampleKey'

现在,你已创建了一个密钥保管库,存储了一个密钥并检索了该密钥。Now, you have created a Key Vault, stored a key, and retrieved it.

清理资源Clean up resources

本系列中的其他快速入门和教程是在本快速入门的基础上制作的。Other quickstarts and tutorials in this collection build upon this quickstart. 如果打算继续使用后续的快速入门和教程,则可能需要保留这些资源。If you plan to continue on to work with subsequent quickstarts and tutorials, you may wish to leave these resources in place. 如果不再需要资源组和所有相关资源,可以使用 Remove-AzResourceGroup 命令将其删除。When no longer needed, you can use the Remove-AzResourceGroup command to remove the resource group, and all related resources. 可以删除资源,如下所示:You can delete the resources as follows:

Remove-AzResourceGroup -Name ContosoResourceGroup

后续步骤Next steps

在本快速入门中,你创建了一个密钥保管库并在其中存储了一个证书。In this quickstart you created a Key Vault and stored a certificate in it. 若要详细了解 Key Vault 以及如何将其与应用程序集成,请继续阅读以下文章。To learn more about Key Vault and how to integrate it with your applications, continue on to the articles below.