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

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install 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. 在本快速入门中,请使用 PowerShell 创建一个密钥保管库,In this quickstart, you use PowerShell to create a key vault. 然后即可在新创建的保管库中存储机密。You then store a secret in the newly created vault.

如果没有 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.

Key Vault 创建命令完成后的输出

向用户帐户授予管理 Key Vault 中的机密的权限Give your user account permissions to manage secrets in Key Vault

使用 Azure PowerShell Set-AzKeyVaultAccessPolicy cmdlet 来更新 Key Vault 访问策略并向用户帐户授予机密权限。Use the Azure PowerShell Set-AzKeyVaultAccessPolicy cmdlet to update the Key Vault access policy and grant secret permissions to your user account.

Set-AzKeyVaultAccessPolicy -VaultName 'Contoso-Vault2' -UserPrincipalName 'user@domain.com' -PermissionsToSecrets get,set,delete

向 Key Vault 添加机密Adding a secret to Key Vault

只需执行几个步骤即可向保管库添加机密。To add a secret to the vault, you just need to take a couple of steps. 在此示例中,请添加可供应用程序使用的密码。In this case, you add a password that could be used by an application. 此密码名为 ExamplePassword,其中存储的值为 hVFkk965BuUvThe password is called ExamplePassword and stores the value of hVFkk965BuUv in it.

先键入以下内容,将 hVFkk965BuUv 值转换成安全字符串:First convert the value of hVFkk965BuUv to a secure string by typing:

$secretvalue = ConvertTo-SecureString 'hVFkk965BuUv' -AsPlainText -Force

然后键入以下 PowerShell 命令,在 Key Vault 中创建名为 ExamplePassword 且值为 hVFkk965BuUv 的机密:Then, type the PowerShell commands below to create a secret in Key Vault called ExamplePassword with the value hVFkk965BuUv :

$secret = Set-AzKeyVaultSecret -VaultName 'Contoso-Vault2' -Name 'ExamplePassword' -SecretValue $secretvalue

若要查看机密中包含的纯文本形式的值,请执行以下命令:To view the value contained in the secret as plain text:

(Get-AzKeyVaultSecret -vaultName "Contoso-Vault2" -name "ExamplePassword").SecretValueText

现在,你已创建 Key Vault 并存储和检索了机密。Now, you have created a Key Vault, stored a secret, 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 other quickstarts and tutorials, you may want to leave these resources in place.

如果不再需要资源组、Key Vault 和所有相关的资源,可以使用 Remove-AzResourceGroup 命令将其删除。When no longer needed, you can use the Remove-AzResourceGroup command to remove the resource group, Key Vault, and all related resources.

Remove-AzResourceGroup -Name ContosoResourceGroup

后续步骤Next steps

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