none
UTF-8 encoding in powershell script RRS feed

  • السؤال

  • Hello,

    I am using the following PowerShell script (many lines are removed to make it short)  and I m facing issues in querying users with their distinguishedname as some of the users has non-English characters in the DN. How can I use UTF-8 encoding inside my script to query my users correctly? Any help would be highly appreciated.

    #region User
    $Properties = @("objectGuid","sAMAccountName","isDeleted")
    $Credentials = New-Object System.Net.NetworkCredential($username,$password)
    $RootDSE = [ADSI]"LDAP://RootDSE"
    $LDAPDirectory = New-Object System.DirectoryServices.Protocols.LdapDirectoryIdentifier($RootDSE.dnsHostName)
    $LDAPConnection = New-Object System.DirectoryServices.Protocols.LDAPConnection($LDAPDirectory, $Credentials)
    $Request = New-Object System.DirectoryServices.Protocols.SearchRequest($RootDSE.defaultNamingContext, "(&(objectClass=user)(sAMAccountName=T*))", "Subtree", $Properties)
    [System.DirectoryServices.Protocols.SearchResultEntry]$entry = $null
        $Response = $LDAPConnection.SendRequest($Request)
        ForEach($entry in $Response.Entries){
     $DirEntry = New-Object System.DirectoryServices.DirectoryEntry "LDAP://$($entry.distinguishedName)"

    …..

    15/ذو القعدة/1440 01:54 ص

الإجابات

  • Nothing needs to be changed. You do not need to use those commands with AD. Just use ADSI and ADSISearcher.

    $searcher = [adsisearcher]'(objectClass=user)'
    $searcher.FindAll()

    You can set the properties on the search to do all of the other things.  The DN will take care of itself.


    \_(ツ)_/


    • تم التحرير بواسطة jrv 15/ذو القعدة/1440 02:30 ص
    • تم وضع علامة كإجابة بواسطة Abu Ameen 22/ذو القعدة/1440 02:59 م
    15/ذو القعدة/1440 02:29 ص

جميع الردود

  • Nothing needs to be changed. You do not need to use those commands with AD. Just use ADSI and ADSISearcher.

    $searcher = [adsisearcher]'(objectClass=user)'
    $searcher.FindAll()

    You can set the properties on the search to do all of the other things.  The DN will take care of itself.


    \_(ツ)_/


    • تم التحرير بواسطة jrv 15/ذو القعدة/1440 02:30 ص
    • تم وضع علامة كإجابة بواسطة Abu Ameen 22/ذو القعدة/1440 02:59 م
    15/ذو القعدة/1440 02:29 ص
  • Thank you, ADSISearcher worked perfectly.
    22/ذو القعدة/1440 03:01 م