快速入门:使用 PowerShell 在 Azure Key Vault 中设置和检索机密Quickstart: Set and retrieve a secret from Azure Key Vault using 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 Subscription before you begin.

如果选择在本地安装并使用 PowerShell,则本教程需要 Azure PowerShell 模块 5.0.0 版或更高版本。If you choose to install and use PowerShell locally, this tutorial requires Azure PowerShell module version 5.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

A resource group is a logical container into which Azure resources are deployed and managed. Use the Azure PowerShell New-AzResourceGroup cmdlet to create a resource group named myResourceGroup in the chinanorth location.

New-AzResourceGroup -Name "myResourceGroup" -Location "ChinaNorth"

创建密钥保管库Create a key vault

使用 Azure PowerShell New-AzKeyVault cmdlet 在上一步骤的资源组中创建 Key Vault。Use the Azure PowerShell New-AzKeyVault cmdlet to create a Key Vault in the resource group from the previous step. 需要提供某些信息:You will need to provide some information:

  • Key Vault 名称:由 3 到 24 个字符构成的字符串,只能包含数字 (0-9)、字母(a-z、A-Z)和连字符 (-)Key vault name: A string of 3 to 24 characters that can contain only numbers (0-9), letters (a-z, A-Z), and hyphens (-)

    重要

    每个密钥保管库必须具有唯一的名称。Each key vault must have a unique name. 在以下示例中,将 替换为密钥保管库的名称。Replace with the name of your key vault in the following examples.

  • 资源组名称:myResourceGroup。Resource group name: myResourceGroup.

  • 位置:ChinaNorth。The location: ChinaNorth.

New-AzKeyVault -Name "<your-unique-keyvault-name>" -ResourceGroupName "myResourceGroup" -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:

  • 保管库名称:为上面的 --name 参数提供的名称。Vault Name: The name you provided to the --name parameter above.
  • 保管库 URI:在本示例中为 https://<your-unique-keyvault-name>.vault.azure.cn/。Vault URI: In the example, this is https://<your-unique-keyvault-name>.vault.azure.cn/. 通过其 REST API 使用保管库的应用程序必须使用此 URI。Applications that use your vault through its REST API must use this URI.

目前,只有你的 Azure 帐户才有权对这个新保管库执行任何操作。At this point, your Azure account is the only one authorized to perform any operations on this new 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 "<your-unique-keyvault-name>" -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

然后,使用 Azure PowerShell Set-AzKeyVaultSecret cmdlet 在密钥保管库中创建一个机密,名为 ExamplePassword,且值为 hVFkk965BuUvThen, use the Azure PowerShell Set-AzKeyVaultSecret cmdlet to create a secret in Key Vault called ExamplePassword with the value hVFkk965BuUv :

$secret = Set-AzKeyVaultSecret -VaultName "<your-unique-keyvault-name>" -Name "ExamplePassword" -SecretValue $secretvalue

从 Key Vault 检索机密Retrieve a secret from Key Vault

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

$secret = Get-AzKeyVaultSecret -VaultName "<your-unique-keyvault-name>" -Name "ExamplePassword" -AsPlainText

现在,你已创建 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.