【问题标题】:Powershell Script not working using Import-Clixml commandPowershell 脚本无法使用 Import-Clixml 命令运行
【发布时间】:2019-02-28 20:30:59
【问题描述】:

我有一个在 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 机器上,当我运行相同的脚本时,我得到了这个:

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

谁能帮帮我?

【问题讨论】:

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

标签: powershell powershell-4.0 powershell-v5.1


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 2018-07-22
    • 2011-06-11
    相关资源
    最近更新 更多