Character encoding in powershell

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Ritmo2k Ritmo2k 2 years, 2 months ago.

  • Author
    Posts
  • #18553
    Profile photo of Ritmo2k
    Ritmo2k
    Participant

    Anyone know the technique to correctly encode a string value in the format required by the Active Directory unicodePwd attribute? In other languages, this is pretty simple but I am missing something when using PowerShell?

  • #18554
    Profile photo of Don Jones
    Don Jones
    Keymaster

    That attribute is supposed to be a one-way hash (OWF) of the password, usable by downlevel (NT) systems, meaning it isn't a clear-text password. You're able to generate the correct OWF hash?

    If so, http://stackoverflow.com/questions/1056692/how-to-encode-unicode-character-codes-in-a-powershell-string-literal may be helpful.

  • #18560
    Profile photo of Ritmo2k
    Ritmo2k
    Participant

    Well, not one-way, http://msdn.microsoft.com/en-ca/library/cc223248.aspx but none the less I am still unable to create a string that the DSA accepts:(

    Thanks!

  • #18715
    Profile photo of Ritmo2k
    Ritmo2k
    Participant

    Hey Dave,

    Problem was related to code elsewhere, so the process is (as expected) like other languages:

    # Surround password in quotes (I needed a random password).
    $pwdString = '"{0}"' -f [guid]::NewGuid()

    # Obtain byte array from UTF-16LE encoded string.
    $encoding = [System.Text.Encoding]::GetEncoding('utf-16')
    [byte[]]$pwdBin = $encoding.GetBytes($pwdString)

    $stringEncoded = [Convert]::ToBase64String($pwdBin)

    That encoded value can be applied directly to the field.

You must be logged in to reply to this topic.