0

我有一个在 Windows Server 2012 R2 的 Powershell 4.0 中运行的脚本。现在我安装了 Windows Server 2016 并拥有 Powershell V5.1.17134.590。

在我的脚本中,我有这个:

$credFile = "c:\Program Files\Scripts\MyCredentials.xml"
$credentials = Import-Clixml $credFile

return $credentials

这是我的 MyCredentials.xml:

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Management.Automation.PSCredential</T>
      <T>System.Object</T>
    </TN>
    <ToString>System.Management.Automation.PSCredential</ToString>
    <Props>
      <S N="UserName">corp\tfsservice</S>
      <SS N="Password">01000000d08c9ddf0115d1118c7a00c04fc297eb01000000f64c30e2720cc64c970ed0d8972b88400000000002000000000003660000c000000010000000bd0a6bf0e5025f1ae6ba8d5b9637db0400000000048</SS>
    </Props>
  </Obj>
</Objs>

现在我很困惑,因为在我的旧机器(windows server 2012 R2)中,当我运行脚本时,我得到了这个:

在此处输入图像描述

如您所见,该命令已成功运行。

但是,在我的 Windows Server 2016 机器上,当我运行相同的脚本时,我得到了这个:

在此处输入图像描述

我不明白为什么这现在不起作用。

谁能帮我?

4

1 回答 1

3

正如 Mathias R. Jenssen 所评论的,*-CliXml正在使用 DPAPI,它将加密密钥管理与特定计算机上的特定用户帐户联系起来。

要解决此问题,需要管理加密密钥。现在有一个密钥管理问题。解密需要它,但任何拥有密钥的人都可以看到秘密。这是加密中的一个众所周知的问题。无论如何,有关快速解决方案,请参阅网络上的文章。如果链接失效,让我们看看代码:

# Pre-generate a key and save it for later use.
$Key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | out-file C:\passwords\aes.key
# Copy the key file into remote computer

# Get a credential and use pre-generated key for encryption
# Save the encrypted password on a file
(get-credential).Password | ConvertFrom-SecureString -key (get-content C:\passwords\aes.key) | set-content "C:\Passwords\password.txt"
# Copy the password file into remote computer

# On remote computer, read the key from file
# and decrypt the password file too. 
# Generate a SecureString and credential object
$password = Get-Content C:\Passwords\password.txt | ConvertTo-SecureString -Key (Get-Content C:\Passwords\aes.key)
$credential = New-Object System.Management.Automation.PsCredential("Luke",$password)
于 2019-02-28T12:30:59.810 回答