none
why this error? RRS feed

  • Question

  • $users=Get-Content H:\powershell\erp\NOERP.txt 
    $out = 0
    foreach($user in $users) {
        $out += (Get-ADUser -Filter * -SearchBase  $user ).count   
        }
    $out

    txt like this

    i will get this error,But I can execute by one line 。。。。。Get aduser: the distinguished name provided must belong to one of the following partitions

    

    Tuesday, December 3, 2019 10:13 AM

Answers

  • Use Import-Csv to remove the quotes:

    $users = Import-Csv H:\powershell\erp\NOERP.txt  -Header DistinguishedName | Select -Expand DistinguishedName


    \_(ツ)_/


    • Edited by jrvModerator Tuesday, December 3, 2019 7:06 PM
    • Marked as answer by Klay.T Thursday, December 5, 2019 11:34 PM
    Tuesday, December 3, 2019 7:06 PM
    Moderator

All replies

  • The text file should contain the full distinguished names of OU's or containers in the domain. The error message seems to indicate it does not.

    Also, the default -SearchScope is subTree, which means that the Get-ADUser query will include users in the SearchBase, plus all users in any sub (child) OU's. You can specify -SearchScope oneLevel to restrict the query to just the specified SearchBase (not including any sub OU's). Or, you can take advantage of the subTree scope to just specify a parent OU. Then the query will include all sub OU's. And, if you want to count all users in the domain, simply specify the distinguishedName of the domain (no need for a text file), and the query should count the total number of users in the domain (since subTree is the default scope).

    Another possibility is that the text file is not strictly text. For testing, have the script simply output the distinguished names in the file, to verify that each line looks correct. In your image the curly quote characters could be the problem. The fix may be to replace with regular quote characters, such as ".

    Edit:

    Reference for Get-ADUser:

    https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/ee617241(v=technet.10)


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Tuesday, December 3, 2019 12:57 PM
  • Use Import-Csv to remove the quotes:

    $users = Import-Csv H:\powershell\erp\NOERP.txt  -Header DistinguishedName | Select -Expand DistinguishedName


    \_(ツ)_/


    • Edited by jrvModerator Tuesday, December 3, 2019 7:06 PM
    • Marked as answer by Klay.T Thursday, December 5, 2019 11:34 PM
    Tuesday, December 3, 2019 7:06 PM
    Moderator
  • That's great this works,But they are also string objects. Why do I have problems?
    Wednesday, December 4, 2019 12:57 AM
  • Unfortunately, it's not caused by quote characters
    Wednesday, December 4, 2019 1:01 AM
  • The likely cause is that the file is Unicode but lacks a BOM. Open the file in notepad and save it as ASCII then try again

    It also may be that you character set or locale is wrong for he text file.  This an happen with Chinese and Japanese systems.


    \_(ツ)_/

    Wednesday, December 4, 2019 1:06 AM
    Moderator
  • Unfortunately,after I change the encoding format,it not works
    Wednesday, December 4, 2019 1:35 AM
  • Well you will have to do some serious debugging to find out what is wrong. There is something wrong with your file or your system. We cannot fix this for you since we cannot see your system.

    Have someone in your organization with technical Windows training help you track down your problem.


    \_(ツ)_/

    Wednesday, December 4, 2019 1:53 AM
    Moderator
  • What do you see when you just do the following:

    Import-Csv H:\powershell\erp\NOERP.txt -Header DistinguishedName | select *


    \_(ツ)_/

    Wednesday, December 4, 2019 1:58 AM
    Moderator
  • like this

    Wednesday, December 4, 2019 2:17 AM
  • like this

    Like what? Posting images is of no help and your images are mostly unreadable and the last one doesn't even display.


    \_(ツ)_/

    Wednesday, December 4, 2019 2:24 AM
    Moderator