问题描述
在 Powershell 5.1 上,我使用这个命令
New-VICredentialStoreItem -Host 10.1.1.2 -User "vsphere_admin" -Password 'PLAIN_PASSWORD' -File c:\cred.xml
<?xml version="1.0" encoding="UTF-8"?>
-<viCredentials>
<version>2.0</version>
-<passwordEntry>
<host>10.1.1.2</host>
<username>vsphere_admin</username>
<password>AQA.....OCOWcDLY=</password>
</passwordEntry>
</viCredentials>
然后我可以通过以下 powercli 命令使用它
$Credentials = Get-VICredentialStoreItem -Host $vcenter_server -File c:\pwd.xml
Connect-VIServer $vcenter_server -User $Credentials.User -Password $Credentials.Password
那很好,但是 Get-VICredentialStoreItem
在 powershell 7 中无效。我看到 Microsoft SecretManagement,但不知道这是否符合我的需要。对此有什么想法吗?
解决方法
更新答案
我在 powershell 7 Cmdlet Get-VICredentialStoreItem is not supported on PowerShell Core.
上遇到了这个错误,如果有办法解决,那就太好了。
无法让 Get-VICredentialStoreItem
在 PowerShell Core (PS 6.0+) 上工作。原因是 PowerShell Core 是跨平台的,而且 the encryption API is Windows-only (YouTube)。
您还可以在内置帮助中看到这一点的提示:
Get-Help Get-VICredentialStoreItem -Full
NOTES
This cmdlet is not supported on the Core edition of PowerShell.
在 Windows 上,您可以在 PowerShell 5.1 中使用 *-VICredentialStoreItem。
在您的交叉帖子中,我注意到您使用的是 *nix。因此,您必须使用像 SecretStore 这样的替代品。
问题是每当我使用 Get-Secret 时,它都会要求输入保管库密码。有什么方法可以存储秘密并在不询问的情况下使用它吗?
记住:
[SecretStore] 可以配置为需要密码才能解锁商店,或者无需密码即可操作。无密码选项仍然加密文件和内存中的机密。但是用于解密的密钥存储在当前用户位置的文件中,并且不太安全。
您可以使用此更新的工作流程,其中包括 Set-SecretStoreConfiguration
。
# Install SecretManagement and SecretStore from the PowerShell Gallery.
Install-Module Microsoft.PowerShell.SecretManagement,Microsoft.PowerShell.SecretStore
# Set no password for the vault.
Set-SecretStoreConfiguration -Scope CurrentUser -Authentication None -Interaction None -Confirm:$false
# Enter current password.
# Register a vault.
Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault
# Add secrets to vault.
Set-Secret -Name Host -Secret "10.1.1.2"
# No password prompt now.
Set-Secret -Name User -Secret "vsphere_admin"
Set-Secret -Name Password -Secret "PLAIN_PASSWORD"
# Use secrets.
# Connect-VIServer $vcenter_server -User (Get-Secret -Name User -AsPlainText) -Password (Get-Secret -Name Password -AsPlainText)
"Connect-VIServer $vcenter_server -User $(Get-Secret -Name User -AsPlainText) -Password $(Get-Secret -Name Password -AsPlainText)"
# Extra line to help with copying from Stack Overflow.
然后你可以尝试启动一个新的shell。从那里,您应该能够在不提示输入密码的情况下访问机密。
此解决方案类似于您的交叉帖子。您忘记在 -Authentication None
中包含 Set-SecretStoreConfiguration
。
撤消配置更改。
Get-SecretStoreConfiguration
Scope Authentication PasswordTimeout Interaction
----- -------------- --------------- -----------
CurrentUser None 900 None
Set-SecretStoreConfiguration -Scope CurrentUser -Authentication Password -Interaction Prompt
Confirm
Are you sure you want to perform this action?
Performing the operation "Changes local store configuration" on target
"SecretStore module local store".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):y
A password is now required for the local store configuration.
To complete the change please provide new password.
Enter password:
********
Enter password again for verification:
********
Get-SecretStoreConfiguration
Vault Microsoft.PowerShell.SecretStore requires a password.
Enter password:
********
Scope Authentication PasswordTimeout Interaction
----- -------------- --------------- -----------
CurrentUser Password 900 Prompt
参考文献
原答案
New-VICredentialStoreItem -Host 10.1.1.2 -User "vsphere_admin" -Password 'PLAIN_PASSWORD' -File c:\cred.xml
$Credentials = Get-VICredentialStoreItem -Host $vcenter_server -File c:\pwd.xml
您正在引用两个不同的文件:cred.xml
和 pwd.xml
请记住,在 PowerShell 中一切都是对象。
在您的示例中,Connect-VIServer 的 -User
和 -Password
参数需要字符串。
而你是 providing strings:$Credentials.User
和 $Credentials.Password
因此,替代品需要提供字符串。
SecretManagement 和 SecretStore
PowerShell 有一组用于管理机密的新模块。
机密管理的目的是为机密提供一个集中且安全的位置。然后,您可以将脚本中编写的密码替换为对机密管理器的引用。这允许您在一个地方更新密码,但会影响每个脚本。它还允许您共享脚本,因为敏感信息只会被引用而不会显示。
在 PowerShell 中,SecretManagement 是多个不同保管库的通用接口。这允许您使用一组通用的 cmdlet 来访问机密,但根据您的需要使用不同的后端。
SecretStore 是 PowerShell 团队提供的一个可用的演示保险库。 SecretStore 只是一种类型的保管库。还有其他扩展保险库可用。例如,HashiCorp Vault 或 KeePass。
因为 SecretManagement 只是一个界面而不是一个完整的解决方案,所以仔细选择适合您需求的保险库很重要。考虑以下几点:
- 我可以在系统出现故障后恢复我的保管库吗?
- 我的保管库会随着我的部署而扩展吗?
- 我的保险库是否供个人使用?
- 秘密会在多个用户之间共享吗?
请记住,如果您决定切换保管库,则必须使用 SecretManagement cmdlet 在保管库之间传输机密。这可能既复杂又耗时。
关于 VMware 的凭证存储,是否有其他人与您共享该存储?是否有任何依赖于它的脚本?虽然底层对象是字符串,但批量切换到新的秘密管理器可能会出现问题。
我们都已链接到 PowerShell 团队的正式发布公告。那篇文章详细介绍了 SecretManagement 和 SecretStore 的功能和限制。阅读该帖子并考虑您的需求是无可替代的。
SecretManagement 和 SecretStore 示例
这是一个使用 SecretManagement 和 SecretStore 的简单示例。
# Install SecretManagement and SecretStore from the PowerShell Gallery.
Install-Module Microsoft.PowerShell.SecretManagement,Microsoft.PowerShell.SecretStore
# Set up a vault.
Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault
# Add secrets to vault.
Set-Secret -Name Host -Secret "10.1.1.2"
# You will be prompted to enter a password for the new vault.
Set-Secret -Name User -Secret "vsphere_admin"
Set-Secret -Name Password -Secret "PLAIN_PASSWORD"
# Use secrets.
# Connect-VIServer $vcenter_server -User (Get-Secret -Name User -AsPlainText) -Password (Get-Secret -Name Password -AsPlainText)
"Connect-VIServer $vcenter_server -User $(Get-Secret -Name User -AsPlainText) -Password $(Get-Secret -Name Password -AsPlainText)"
# Extra line to help with copying from Stack Overflow.
命令历史
SecretManagement cmdlet 未出现在光盘的最后 command history 中。但是,可以在 PowerShell 中按下向上箭头来查看最近的命令。这会暴露你的秘密。可能还有其他类型的 PowerShell 登录正在使用中。
- Get-History (Microsoft.PowerShell.Core) - PowerShell | Microsoft Docs
- about_Logging - PowerShell | Microsoft Docs
- Auditing and Reporting on JEA - PowerShell | Microsoft Docs
如果您想在更多类型的日志记录中隐藏您键入的秘密,您可以输入一个安全字符串。
# Use Read-Host -AsSecureString.
Set-Secret -Name LoggingExample -SecureStringSecret (Read-Host "Enter a secret for LoggingExample. " -AsSecureString)
# Show plaintext.
Get-Secret -Name LoggingExample -AsPlainText
删除机密。
在以下部分中,我撤消了安装。
Get-SecretInfo
Name Type VaultName
---- ---- ---------
Host String SecretStore
Password String SecretStore
User String SecretStore
Get-SecretInfo | Remove-Secret
Vault SecretStore requires a password.
Enter password:
********
Get-SecretInfo
# Nothing is displayed.
移除保险库。
Get-SecretVault
Name ModuleName IsDefaultVault
---- ---------- --------------
SecretStore Microsoft.PowerShell.SecretStore True
Get-SecretVault | Unregister-SecretVault
Get-SecretVault
# Nothing.
删除模块。
# Close and reopen PowerShell.
Get-InstalledModule | Format-Table -AutoSize
Version Name Repository Description
------- ---- ---------- -----------
1.0.0 Microsoft.PowerShell.SecretManagement PSGallery This module provides …
1.0.0 Microsoft.PowerShell.SecretStore PSGallery This PowerShell modul…
Uninstall-Module Microsoft.PowerShell.SecretStore
Uninstall-Module Microsoft.PowerShell.SecretManagement
Get-InstalledModule | Format-Table -AutoSize
# Nothing.
PowerCLI 兼容性
Get-VICredentialStoreItem
在 powershell 7 中无效。
不清楚你为什么这么说。
此外,不清楚为什么替换 Get-VICredentialStoreItem
会有所帮助,因为我认为 Connect-VIServer
不会在相同的环境中工作。
VMware 使用 PowerShell 7 声明 PowerCLI is compatible。
支持的 PowerShell 版本
VMware PowerCLI 12.3.0 与以下 PowerShell 版本兼容:
- Windows PowerShell 5.1
- PowerShell 7
如果您的意思是说 PowerCLI 与 PowerShell 7.1 不兼容,那么是否有错误消息?
您是否向 VMware 报告了错误?可能有一个 compatibility problem 带有 VMware 可以解决的底层模块。
您尚未说明使用 PowerShell 7.1 的原因。
如果 PowerCLI 仅在早期版本的 PowerShell 中受支持,那么我建议使用兼容版本。 PowerShell 7.0 和 7.1 之间只有一些模糊的变化。
- What's New in PowerShell 7.1 - PowerShell | Microsoft Docs
- PowerShell/7.1.md at master · PowerShell/PowerShell · GitHub
如果您希望获得 Microsoft 的生产支持,PowerShell 7.0 的支持将持续到 2022 年 12 月 3 日,并且 PowerShell 5.1 作为 Windows 10 的一部分仍受支持。