Сценарий Powershell не работает с помощью команды Import-Clixml - PullRequest
0 голосов
/ 28 февраля 2019

У меня был скрипт, который работал в Powershell 4.0 в Windows Server 2012 R2.Теперь я установил 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), когда я запускаю скрипт, я получил это:

enter image description here

Каквы можете видеть, что команда выполняется с успехом.

Однако на моем компьютере с Windows Server 2016, когда я запускаю тот же сценарий, я получаю следующее:

enter image description here

Я не понимаю, почему это не работает сейчас.

Кто-нибудь может мне помочь?

1 Ответ

0 голосов
/ 28 февраля 2019

Как заметил Матиас Р. Дженсен, *-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)
...