【问题标题】:PowerShell : Problem with ConvertTo-SecureString and ConvertFrom-SecureString INSIDE a PSSessionPowerShell:ConvertTo-SecureString 和 ConvertFrom-SecureString 在 PSSession 内的问题
【发布时间】:2020-03-24 18:43:34
【问题描述】:

我尝试在 2 个 Powershell 脚本中使用 ConvertTo- 和 ConvertFrom-SecureString,但在使用动态配置文件生成加密密码,然后在同一台机器上使用 SAME 配置文件读取/解密它们时遇到问题。

我的脚本要求交互式用户 (IU) 输入任务用户 (TU) 的凭据。 逻辑是在 IU 的 powershell 会话中为 TU 创建一个 PSSession,从纯文本(或读取主机)生成安全字符串,并将其转换为常规字符串以将其导出到文件。

$scriptUser = Read-Host "Entrez le nom de l'utilisateur qui va chiffrer/utiliser le fichier de mot de passe (domain\user)"
$scriptCredential = Get-Credential -Message "Veuillez entrer le mot de passe de l'utilisateur $scriptUser" -User $scriptUser

$Crypt = New-PSSession -Credential $scriptCredential -ComputerName localhost
if ($Crypt)
{
    Enter-PSSession $Crypt
    $Texte = "JULIEN"
    ConvertTo-SecureString $Texte -AsPlainText -Force | ConvertFrom-SecureString | Set-Content -Path C:\PathToFile\testcrypt_adm_task.txt
    Exit-PSSession
}

但是当我尝试使用带有 TU(交互式或任务调度程序)的 ConvertFrom-SecureString 读取文件内容时:没门!

> PS C:\PathToFile> Get-Content .\testcrypt_adm_task.txt |
> convertto-securestring convertto-securestring : Key not valid for use
> in specified state. At line:1 char:40
> + Get-Content .\testcrypt_adm_task.txt | convertto-securestring
> +                                        ~~~~~~~~~~~~~~~~~~~~~~
>     + CategoryInfo          : InvalidArgument: (:) [ConvertTo-SecureString], CryptographicException
>     + FullyQualifiedErrorId : ImportSecureString_InvalidArgument_CryptographicError,Microsoft.PowerShell.Commands.Conv    ertToSecureStringCommand

如果我对我的 IU 执行完全相同的命令,没问题,好像用于加密我的 Securestring 的密钥绑定到我的 IU 而不是我的 TU。

关键是我在真正理解 PSSession 真正加载的内容时遇到了问题,因为我的 IU 而不是我的 TU 可以读取安全字符串,即使加密操作已经在带有 TU 的 PSSession 中完成信用...

Enter-PSSession 是不是意味着真正进入一个会话,所有环境都带有它? 关于使用 Enter-PSSession 加载的环境,我是否遗漏了什么?

感谢您的帮助

【问题讨论】:

    标签: powershell session profile securestring


    【解决方案1】:

    Import-clixml 是将 PowerShell 对象保存到磁盘的唯一方法。 Export-clixml 将允许您将文件导入 PowerShell 对象。

    尝试将“| Set-Content -Path C:\PathToFile\testcrypt_adm_task.txt”更改为“| Export-Clixml -Path C:\PathToFile\testcrypt_adm_task.txt”

    当你准备好阅读它时,使用 $mysavedpassword = import-clixml c:\PathToFile\testcrypt_adm_task.txt

    【讨论】:

    • 写入磁盘并不是真正的问题。更多的是 PSSession 没有完全加载 tak 用户的环境,因此文件中的 SecureString 在同一用户的计划任务中不可用。我想我会选择一个肮脏的“Start-Process PowerShell.exe -ArgumentList { Script-Block }”...
    【解决方案2】:

    可能您使用不同的用户创建了安全字符串,并尝试使用不同的用户转换回纯文本。请使用相同的用户来确保其安全并将其转换回纯文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多