SecretStore 模块入门

SecretManagementSecretStore 模块可从 PowerShell 库获取,可以使用 PowerShellGet 命令进行安装。

# Install with PowerShellGet 2.x
Install-Module Microsoft.PowerShell.SecretManagement
Install-Module Microsoft.PowerShell.SecretStore

# Install with PSResourceGet 1.x
Install-PSResource Microsoft.PowerShell.SecretManagement
Install-PSResource Microsoft.PowerShell.SecretStore

安装模块后,可以加载模块并开始使用或创建新机密。

Import-Module Microsoft.PowerShell.SecretManagement
Import-Module Microsoft.PowerShell.SecretStore

创建保管库并添加机密

首先,必须注册保管库。 Name 参数是友好名称,可以是任何有效的字符串。

Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault

DefaultVault 参数将此参数设为默认保管库。

现在可以创建机密。

Set-Secret -Name TestSecret -Secret "TestSecretPassword"

此示例传递机密值的纯文本字符串。 机密值可以是五种受支持的类型之一:

  • byte[]
  • 字符串
  • SecureString
  • PSCredential
  • Hashtable

首次访问保管库时,必须为新保管库提供密码。 此密码用于锁定和解锁保管库。

Vault SecretStore requires a password.
Enter password:
********
Enter password again for verification:
********

运行 Get-Secret 以检索机密。 使用 AsPlainText 开关会将机密作为未加密的字符串返回。

PS> Get-Secret -Name TestSecret -AsPlainText
TestSecretPassword

若要获取所有机密的列表,可以运行:

PS> Get-SecretInfo

Name       Type   VaultName
----       ----   ---------
TestSecret String SecretStore

备注

使用 Name 参数运行Set-Secret指定机密名称时,cmdlet 会调用GetSecret()由保管库扩展实现的 。 Set-Secret 传递用户提供的名称。 保管库扩展按该名称查找机密。 如果 GetGecret() 返回匹配项, Set-Secret 则覆盖机密,除非使用 NoClobber 参数。 保管库扩展始终写入收到的机密信息。

由保管库扩展实现决定是否对名称使用区分大小写的比较。 例如, Microsoft.PowerShell.SecretStore 扩展保管库中的机密名称不区分大小写。 如果传递给 Set-Secret 的名称仅根据大小写与 SecretStore 保管库中现有机密的名称不同,则名称将被你提供的新值覆盖。