none
UTF-8 encoding in powershell script RRS feed

  • Pregunta

  • 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)"

    …..

    jueves, 18 de julio de 2019 1:54

Respuestas

  • 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.


    \_(ツ)_/


    • Editado jrv jueves, 18 de julio de 2019 2:30
    • Marcado como respuesta Abu Ameen jueves, 25 de julio de 2019 14:59
    jueves, 18 de julio de 2019 2:29

Todas las respuestas

  • 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.


    \_(ツ)_/


    • Editado jrv jueves, 18 de julio de 2019 2:30
    • Marcado como respuesta Abu Ameen jueves, 25 de julio de 2019 14:59
    jueves, 18 de julio de 2019 2:29
  • Thank you, ADSISearcher worked perfectly.
    jueves, 25 de julio de 2019 15:01