【问题标题】:Alternative for Get-VICredentialStoreItem in Powershell 7.1Powershell 7.1 中 Get-VICredentialStoreItem 的替代方案
【发布时间】:2021-05-10 14:26:49
【问题描述】:

在 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 powercli


    【解决方案1】:

    更新答案

    我在 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。

    在你的 crosspost 中,我注意到你在使用 *nix。因此,您必须使用 SecretStore 之类的替代方案。


    问题是每当我使用 Get-Secret 时,它都会要求输入保险库密码。有什么方法可以存储秘密并无需询问即可使用?

    记住:

    [SecretStore] 可以配置为需要密码才能解锁商店,或者无需密码即可操作。 no-password 选项仍然对文件和内存中的秘密进行加密。但是解密的密钥存储在当前用户位置的文件中,安全性较低。

    您可以使用这个更新后的工作流程,其中包括 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.xmlpwd.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 正在使用中。

    如果您想对更多类型的日志记录隐藏您键入的秘密,您可以输入一个安全字符串。

    # 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 之间只有一些不明显的变化。

    如果您需要 Microsoft 的生产支持,PowerShell 7.0 将在 2022 年 12 月 3 日之前受支持,并且 PowerShell 5.1 作为 Windows 10 的一部分仍受支持。


    参考文献

    【讨论】:

    • 我在 powershell 7 Cmdlet Get-VICredentialStoreItem is not supported on PowerShell Core. 上收到此错误,如果有办法解决这个问题,那就太好了。
    • 好的,我设置了保管库,它似乎正在工作。但是,问题是每当我使用Connect-VIServer $vcenter_server -User (Get-Secret -Name User -AsPlainText) -Password (Get-Secret -Name Password -AsPlainText) 时,它都会要求提供保险库密码。有什么方法可以存储秘密并在不询问的情况下使用它?
    • 我更新了我的答案来解决你的问题。
    猜你喜欢
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多