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

    …..

    יום חמישי 18 יולי 2019 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 יום חמישי 18 יולי 2019 02:30
    • סומן כתשובה על-ידי Abu Ameen יום חמישי 25 יולי 2019 14:59
    יום חמישי 18 יולי 2019 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 יום חמישי 18 יולי 2019 02:30
    • סומן כתשובה על-ידי Abu Ameen יום חמישי 25 יולי 2019 14:59
    יום חמישי 18 יולי 2019 02:29
  • Thank you, ADSISearcher worked perfectly.
    יום חמישי 25 יולי 2019 15:01