none
Counting every 1000 ADUser or computer objects and parse to csv file RRS feed

  • Question

  • Hi Team

    How to make a powershell script to count every 1000 adcomputer or user objects and parse it to csv file  every 1000 and continue counting the next loop until the end or reach the last computer or user objects. 

    Thanks

    Sunday, January 26, 2020 3:21 PM

Answers

  • $a=@(Get-ADObject -LDAPFilter "(ObjectClass=user)")
    
    for ($i=0;$i*1000 -lt $a.length; $i++) {$b=$i*1000; if (($c=$b+1000-1) -ge ($a.length-1)) {$c=$a.length-1} ;$b..$c|%{$a[$_]}|Export-Csv ".\file$i.csv"}


    my blog: http://shserg.ru/


    • Edited by s.h.s. _ Monday, January 27, 2020 10:00 AM
    • Marked as answer by green_tree22 Friday, January 31, 2020 9:56 AM
    Monday, January 27, 2020 10:00 AM

All replies

  • Hint:

    [collections.arraylist]$computerList = Get-AdConmputer -Filter *
    While($computerList){
        $computerList.GetRange(0,1000) | Export-Csv file.csv
        $computerList.RemoveRange(0,1000)
    }
    


    \_(ツ)_/

    Sunday, January 26, 2020 3:42 PM
  • Hi,

    its getting error below.

    Exception calling "RemoveRange" with "2" argument(s): "Offset and length were out of bounds for the array or count is greater than the number of elements from index to 
    the end of the source collection."

    is that possible to display the first 1000 counts then after that its display again the next 1000?

    Thanks

    Monday, January 27, 2020 12:23 AM
  • Yes.  First you have to take time out to learn PowerShell.  After learning basic PowerShell all of these things will be obvious.

    Learning to script properly with PowerShell


    \_(ツ)_/

    Monday, January 27, 2020 12:36 AM
  • $a=@(Get-ADObject -LDAPFilter "(ObjectClass=user)")
    
    for ($i=0;$i*1000 -lt $a.length; $i++) {$b=$i*1000; if (($c=$b+1000-1) -ge ($a.length-1)) {$c=$a.length-1} ;$b..$c|%{$a[$_]}|Export-Csv ".\file$i.csv"}


    my blog: http://shserg.ru/


    • Edited by s.h.s. _ Monday, January 27, 2020 10:00 AM
    • Marked as answer by green_tree22 Friday, January 31, 2020 9:56 AM
    Monday, January 27, 2020 10:00 AM
  • Thanks jvr for the sample command.
    Friday, January 31, 2020 9:56 AM
  • Hi Jvr,

    when i run the script, i notice that the objectclass for computer is also appear on the csv file results?

    how can i exclude the objectclass computer and on how to query the disabled users

    Thanks

    Friday, January 31, 2020 10:24 AM
  • hi

    how not to create a file for every loop. or only a single csv file only.

    Thanks

    Saturday, February 1, 2020 3:45 AM
  • hi

    how not to create a file for every loop. or only a single csv file only.

    Thanks

    Without your code it is not possible to understand your question.  Your question makes no sense.

    What code are you running that is a problem?


    \_(ツ)_/

    Saturday, February 1, 2020 3:48 AM
  • Hi s.h.s_

    how can to make parse every result in a single csv file?

    Saturday, February 1, 2020 3:59 AM
  • Hi s.h.s_

    how can to make parse every result in a single csv file?

    Sorry but your question doesn't make any sense.

    Read the following:


    \_(ツ)_/

    Saturday, February 1, 2020 5:44 AM
  • Think of this - what is the definition of "parse"?

    It basically means "inspect" so what do you want to inspect?

    See: https://www.merriam-webster.com/dictionary/parse


    \_(ツ)_/

    Saturday, February 1, 2020 5:50 AM
  • This is the code that i run from S.h.s

    $a=@(Get-ADObject -LDAPFilter "(ObjectClass=user)") for ($i=0;$i*1000 -lt $a.length; $i++) {$b=$i*1000; if (($c=$b+1000-1) -ge ($a.length-1)) {$c=$a.length-1} ;$b..$c|%{$a[$_]}|Export-Csv "C:\temp\file$i.csv"}

    $con= Get-ChildItem c:\temp\na\*.csv 
        $import=import-csv $con 

        foreach($file in $import){
        $file|Export-Csv C:\temp\na\raw.csv -Append 
        }
        
     $data = import-csv C:\temp\na\raw.csv
     $data | Sort-Object DistinguishedName |
    Group-Object Name | 
    Foreach-Object {$_.group| Select-Object -Last 1} |
    export-csv c:\temp\na\final.csv -NoTypeInformation
    $ap=Import-Csv  c:\temp\na\final.csv

    its create 1000 per csv file.

    and my question is, every 1000 objects would be parse in a single csv file.

    then i added a scritp to parse all csv in a single file and delete duplicate values but its takes too long or an hour

    im extracting a morethan 700 000 user objects. :( how can i achieve on that and make it a little faster?

    and as of now, my script is running for parsing all the csv file into one csv which the raw and takes 5 hours or more to finish then after that, the raw csv file will export to final csv file to check and remove the duplicate entry.

    Thanks.

    Sunday, February 2, 2020 5:58 AM
  • thanks for correction, i thought parse is pasting the results in csv. anyway. i just want to paste the results in a single csv file. Thanks
    Sunday, February 2, 2020 8:46 AM
  • thanks for correction, i thought parse is pasting the results in csv. anyway. i just want to paste the results in a single csv file. Thanks

    "paste"?  What is that.  How can you paste something into a file.

    Open file in notepad and type Control-V to paste into a file.



    \_(ツ)_/

    Sunday, February 2, 2020 11:16 AM
  • This is the code that i run from S.h.s

    $a=@(Get-ADObject -LDAPFilter "(ObjectClass=user)") for ($i=0;$i*1000 -lt $a.length; $i++) {$b=$i*1000; if (($c=$b+1000-1) -ge ($a.length-1)) {$c=$a.length-1} ;$b..$c|%{$a[$_]}|Export-Csv "C:\temp\file$i.csv"}

    $con= Get-ChildItem c:\temp\na\*.csv 
        $import=import-csv $con 

        foreach($file in $import){
        $file|Export-Csv C:\temp\na\raw.csv -Append 
        }
        
     $data = import-csv C:\temp\na\raw.csv
     $data | Sort-Object DistinguishedName |
    Group-Object Name | 
    Foreach-Object {$_.group| Select-Object -Last 1} |
    export-csv c:\temp\na\final.csv -NoTypeInformation
    $ap=Import-Csv  c:\temp\na\final.csv

    its create 1000 per csv file.

    and my question is, every 1000 objects would be parse in a single csv file.

    then i added a scritp to parse all csv in a single file and delete duplicate values but its takes too long or an hour

    im extracting a morethan 700 000 user objects. :( how can i achieve on that and make it a little faster?

    and as of now, my script is running for parsing all the csv file into one csv which the raw and takes 5 hours or more to finish then after that, the raw csv file will export to final csv file to check and remove the duplicate entry.

    Thanks.

    This code makes absolutely no sense.  Why are you doing this.  Th3re can be no duplicates in AD.  All objects must be unique.  There is no way to have duplicates.

    There is no point to exporting into multiple files if you are just going to recombine those files.

    "grouping" and "sorting" are really the same thing.  They both require multiple passes over a file to accomplish.  There is no point to doing both.

    I think you need to try to determine what the purpose of all of the is before continuing.  Part of the issue is that you do not have a good understanding of English and also you have no training in computer programming or data.  All of this is making your task much harder than it should be.  Without a clear statement of what you are trying to do you wil get very bad answers form others whoa are also not trained in programming and computer science.


    \_(ツ)_/

    Sunday, February 2, 2020 11:40 AM
  • The following does the same thing your code is trying to do but uses far fewer steps.  It will still be slow because of the sort which will be limited by memory and the number of processors on your PC.

    # get all users into a variable and into a CSV file and save to variable
    Get-AdUser -Filter * |
        Tee-Object -Variable $users |
        Export-Csv AllUsers.csv
    
    # split users into files of 1000
    for ($i=0;$i -lt $a.length; $i+=1000) {
        $users[$i..($i+1000)] | Export-Csv "C:\temp\file$i.csv"
    }
    
    # find duplicate CNs and remove - export and save in variable
    $users | 
        Sort-Object Name -Unique |
        Tee-Object -Variable $ap |
        Export-Csv c:\temp\na\final.csv -NoTypeInformation

    Learning basic programming would help you to not choose bad approaches to big problems.


    \_(ツ)_/



    • Edited by jrv Sunday, February 2, 2020 11:55 AM
    Sunday, February 2, 2020 11:54 AM