locked
Do While loop User existing RRS feed

  • Question

  • Hi,

    I'm trying to run a script to create a new user in Active Directory by using Do While loop.
    I have a var called $alias that its value is the Alias/SamAccountName of the user now i'd like to use a loop that will use the var $alias that i already have to search for an existing samaccountname then eventually to replace the var $alias with a new samaccount name that doesn't exist.

    For example:

    $alias = John
    $filter = "Get-ADUser -Filter {SamAccountName -eq $Alias}"
    
    Do   
    
    {
         $filter
    }
    
    while  (Read-Host "User $alias exist type new alias" -OutVariable alias)


    I know the example is totally wrong but you got the point.

    Thanks in advance.



    • Edited by MendiL Tuesday, January 29, 2019 5:29 PM
    Tuesday, January 29, 2019 5:28 PM

All replies

  • you should not use the do / while. for Performance reasons. 

    look at for-each or for each-objects this is much faster and very eays to use. 

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-6 

    https://docs.microsoft.com/en-us/powershell/scripting/samples/repeating-a-task-for-multiple-objects--foreach-object-?view=powershell-6

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-6

    klaus

    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue


    Klaus

    Tuesday, January 29, 2019 5:32 PM
  • What performance each?  Did you just make that up.  The loop ask for input on each pass. Why does that have to be 2 milliseconds faster?

    In PowerShell strings need to be quoted,

    The better method is this:

    while(1){
        if($alias = Read-Host 'Enter SamAccountName'){
            # test alias
        }else{
            break
        }
    }

    You will have to start you PowerShell experience by learning basic programming which you can get along with PowerShell here:

    Microsoft Virtual Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Tuesday, January 29, 2019 5:47 PM
  • please try this with AD Objects with more as 10.000 and you see the result. 


    Klaus

    Wednesday, January 30, 2019 8:10 PM
  • please try this with AD Objects with more as 10.000 and you see the result. 


    Klaus

    Please understand that there is a "Read-Host" in the loop.  This makes you claim impossible.  You cannot use a ForEach-Object in this place.


    \_(ツ)_/

    Wednesday, January 30, 2019 8:19 PM
  • please try this with AD Objects with more as 10.000 and you see the result. 


    Klaus

    You are really referencing the issue between "foreach()" and "ForEach-Object".  This issue is totally dependent on the code being executed and how much and the kind of I/O that it does.  In general ForEach-Object is faster than foreach() when processing in the pipeline but for memory only operations the foreach() can be slightly faster.

    For processing a read-host in a loop we would not use either construct.  "do" and "while constructs are used to repeat a loop until we have reached a certain set of criteria.  This can seldom be done with either form of foreach, however, I will agree that, when we can use a pipeline with "ForEach-Object" to process a collection, it will likely, but not always, be faster.

    (I couldn't figure out how to use any more commas)


    \_(ツ)_/

    Wednesday, January 30, 2019 9:01 PM
  • no I not say use the difference between "each--whatever"

    the Performance is between each- and the while.. loop.. 

    for-each object makes only sense if you use a object to loop trough. or ?


    Klaus

    Thursday, January 31, 2019 8:52 AM
  • Klaus,

    vielleicht wird das auf Deutsch etwas einfacher und wir können diese fruchtlose Diskussion endlich beenden  ...  Was James meint, ist, dass es keinen Sinn macht, über Performance zu sprechen, so lange im Code ein Read-Host enthalten ist, welches die Code-Ausführung sowieso unterbricht bis der Anwender reagiert.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Thursday, January 31, 2019 9:15 AM
    Thursday, January 31, 2019 9:14 AM
  • ich gebe Dir und James recht Ziel sollt es einfach sein 

    Fragen fachlich neutral zu beantworten mit dem Ziel zu helfen, 

    lg k. 


    Klaus

    Thursday, January 31, 2019 12:40 PM
  • for-each object makes only sense if you use a object to loop trough. or ?


    Klaus

    Correcto mundo! It only make sense when iterating over a collection of objects.

    \_(ツ)_/

    Thursday, January 31, 2019 4:23 PM