使用Import-Clixml命令时Powershell脚本不工作

使用Import-Clixml命令时Powershell脚本不工作,powershell,powershell-4.0,powershell-v5.1,Powershell,Powershell 4.0,Powershell V5.1,我有一个在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 这是MyCredentia

我有一个在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>

System.Management.Automation.PSCredential
系统对象
System.Management.Automation.PSCredential
公司服务
01000000D08C9DDF0115D1118C7A00C04FC297EB01000000F64C30E2720CC64C970ED8972B8840000000000200000000000003660000C00000000000001000000BD0A6BF0E5025F1AE6BA8D5B9637DB040000000048
现在我很困惑,因为在我的旧机器(windows server 2012 R2)中,当我运行脚本时,我得到了以下信息:

如您所见,命令运行成功

但是,在我的Windows Server 2016计算机中,当我运行相同的脚本时,我得到以下结果:

我不明白为什么现在不行


有人能帮我吗?

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

为了解决这个问题,需要管理加密密钥。现在有一个密钥管理的问题。解密时需要它,但任何拥有密钥的人都可以看到秘密。这是一个众所周知的加密问题。无论如何,要快速解决问题,请参阅web上的。如果出现链接损坏,让我们看看代码:

# 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)

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

为了解决这个问题,需要管理加密密钥。现在有一个密钥管理的问题。解密时需要它,但任何拥有密钥的人都可以看到秘密。这是一个众所周知的加密问题。无论如何,要快速解决问题,请参阅web上的。如果出现链接损坏,让我们看看代码:

# 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)

Import-CliXml
隐式解密安全字符串,默认加密密钥绑定到最初运行
Export-CliXml
的计算机。知道如何找到加密密钥并将其复制到我的新机器上吗?我会尝试在旧机器上解密它,然后在新机器上重新加密。@marsze所说的^
Import CliXml
隐式解密安全字符串,默认加密密钥绑定到最初运行
Export-CliXml
的机器。知道我如何找到加密密钥并将其复制到我的新机器上吗?我会尝试在旧机器上解密它,然后在新机器上重新加密。什么@marsze说的^