2011-08-18 4 views
22

보안 된 파일에 자격 증명을 저장하기 위해이 게시물에서 설명한 것과 같은 작업을 수행 했으므로 자동화 된 프로세스에서 원격 PS를 실행할 수 있습니다 스크립트를 통해 Invoke-command : http://blogs.technet.com/b/robcost/archive/2008/05/01/powershell-tip-storing-and-using-password-credentials.aspxpowershell에서 재사용 할 자격 증명을 저장하고 오류 ConvertTo-SecureString : 지정된 상태에서 사용하기에 키가 올바르지 않음

이것은 내 계정에서 실행하면 좋습니다 - 암호는 암호화 된 파일에서 읽은 후 Invoke-Command로 전달되고 모든 항목이 정상입니다.

오늘 내 스크립트가 황금 시간대에 준비되었을 때 자동화 된 프로세스에서 사용할 Windows 계정에서 스크립트를 실행하려고 시도했지만 스크립트에서 파일에서 보안 된 암호를 읽으려고 시도하는 동안이 오류가 발생했습니다 :

ConvertTo-SecureString : Key not valid for use in specified state. 
At \\remoted\script.ps1:210 char:87 
+ $password = get-content $PathToFolderWithCredentials\pass.txt | convertto-sec 
urestring <<<< 
    + CategoryInfo   : InvalidArgument: (:) [ConvertTo-SecureString], C 
    ryptographicException 
    + FullyQualifiedErrorId : ImportSecureString_InvalidArgument_Cryptographic 
    Error,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand 

내 직장 동료가 자신의 계정으로 실행하도록 요청했는데 동일한 오류가 발생했습니다.

$login= get-content $PathToFolderWithCredentials\login.txt 
$password = get-content $PathToFolderWithCredentials\pass.txt | convertto-securestring 
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $login,$password 
:이 호출-명령에 사용하도록 읽을 수있는 자동화 된 프로세스로 실행하는 스크립트 코드가

$PathToFolderWithCredentials = "\\path\removed" 

write-host "Enter login as domain\login:" 
read-host | out-file $PathToFolderWithCredentials\login.txt 

write-host "Enter password:" 
read-host -assecurestring | convertfrom-securestring | out-file $PathToFolderWithCredentials\pass.txt 

write-host "*** Credentials have been saved to $pathtofolder ***" 

입니다 :

내가 자격 증명을 저장하기 위해 사용하고있는 코드입니다

줄에 오류가 발생했습니다. $ password = get-content $ PathToFolderWithCredentials \ pass.txt | convertto-securestring

아이디어가 있으십니까?

+1

"ConvertFrom-SecureString cmdlet은 Windows 표준 Data Protection API를 사용하여이 데이터를 암호화하므로 사용자 계정 만 해당 내용을 적절히 해독 할 수 있습니다." 그것이 작동하지 않는 이유는 무엇입니까 ... 어떤 아이디어가 다른 Windows 계정에 의해 해독 될 수있는 암호를 저장하는 가장 좋은 방법이 될 것이라고 생각하십니까? – mishkin

+1

이 블로그 게시물도 매우 유용했습니다 http://powertoe.wordpress.com/2011/06/05/storing-passwords-to-disk-in-powershell-with-machine-based-encryption/ – mishkin

+2

그리고 저는 초보자와 고급 사용자 모두를위한 "Powershell Cookbook"- goob book을 읽는 것이 좋습니다. 그것은 이것과 많은 다른 것들을 다룹니다. – manojlds

답변

19

ConvertFrom-SecureStringKey (및 SecureKey) 매개 변수를 취합니다. 키를 지정하여 암호화 된 표준 문자열을 저장 한 다음 ConvertTo-SecureString에 키를 다시 사용하여 사용 계정과 관계없이 보안 문자열을 복원 할 수 있습니다. 사람들이 공개 키와를 사용하여 암호를 암호화함으로써 프로젝트에서

http://technet.microsoft.com/en-us/library/dd315356.aspx

, 나는, 비대칭 암호화를 구현 한 자동화 프로세스는 암호를 해독하기 위해 개인 키를 가지고 또 다른 방법은하는 것 Handling passwords in production config for automated deployment

+1

감사합니다. 작동합니다! 너는 나를 다시 구했다!:) 그냥 질문 (아마도 바보)하지만 만약 내가 내 스크립트에 열쇠를 넣어, 그냥 거기에 암호를 입력 같은 일이되지 않을까요? – mishkin

+1

키 방법을 많이 사용하지는 않았지만 일반적으로 스크립트를 실행하는 컴퓨터의 특정 위치 (키 저장소)에 스크립트를 설치하고 거기에서 스크립트를 읽고 해독합니다. 비대칭 버전에서는 공개 키가 알려져 있으므로 사람들은 암호를 암호화 할 수 있지만 개인 키를 가진 사람 만 암호를 해독 할 수 있습니다. 개인 키는 스크립트를 실행하는 시스템에 있습니다. 스크립트는 어디에서 찾을 지 알 것입니다. 희망이 의미가 있습니다. – manojlds

+1

일단 내 프로젝트가 완료되면 맥주 빚 져야 해! 고마워요! – mishkin

1

을 ConvertFrom-SecureString이 사용하는 'CurrentUser'대신 'LocalMachine'범위를 사용하여 데이터를 보호하십시오.

public static string Protect(SecureString input, DataProtectionScope dataProtectionScope = DataProtectionScope.CurrentUser, byte[] optionalEntropy = null) 
{ 
    byte[] data = SecureStringToByteArray(input); 
    byte[] data2 = ProtectedData.Protect(data, optionalEntropy, dataProtectionScope); 
    for (int i = 0; i < data.Length; i++) 
    { 
     data[i] = 0; 
    } 

    return ByteArrayToString(data2); 
} 
private static byte[] SecureStringToByteArray(SecureString s) 
{ 
    var array = new byte[s.Length * 2]; 
    if (s.Length > 0) 
    { 
     IntPtr intPtr = Marshal.SecureStringToGlobalAllocUnicode(s); 
     try 
     { 
      Marshal.Copy(intPtr, array, 0, array.Length); 
     } 
     finally 
     { 
      Marshal.FreeHGlobal(intPtr); 
     } 
    } 

    return array; 
} 
private static string ByteArrayToString(byte[] data) 
{ 
    var stringBuilder = new StringBuilder(); 
    for (int i = 0; i < data.Length; i++) 
    { 
     stringBuilder.Append(data[i].ToString("x2", CultureInfo.InvariantCulture)); 
    } 

    return stringBuilder.ToString(); 
} 

암호화 된 문자열은 'CurrentUser'범위를 사용하는 ConvertTo-SecureString에서 사용할 수 있습니다.

+2

원래의 질문은'PowerShell'을 사용하기 때문에이 대답을'C#'에서'PowerShell'로 변환 할 수 있습니까? –

14

암호 문자열은 동일한 컴퓨터에서 실행하고 동일한 로그인을 사용하여 만들어야합니다.