how to write loop (was "no subject")

Dave Angel davea at ieee.org
Tue May 12 09:33:53 EDT 2009


karlos barlos wrote:
> hello to all 
>
> i have been using this script to add users to my active directory structure
>
> i wise to make a loop in order for it to run for A large Number of Users 
>
> can anyone give me some advice on the loop ??
>
>
>
> import win32com,win32com.client
> def add_acct(location,account):
>     
>   ad_obj=win32com.client.GetObject(location)
>   ad_user=ad_obj.Create('user','cn='+user['login'])
>   ad_user.Put('sAMAccountName',user['login'])
>   ad_user.Put('userPrincipalName',user['login']+'@email.address.com')
>   ad_user.Put('DisplayName',user['last']+' '+user['first']) #fullname
>   ad_user.Put('givenName',user['first'])
>   ad_user.Put('sn',user['last'])
>   ad_user.Put('description','regular account')
>   ad_user.Put('physicalDeliveryOfficeName','office 1')
>   ad_user.Put('HomeDirectory',r'\\server1\ '[:-1]+user['login']) 
>   ad_user.Put('HomeDrive','H:')
>   ad_user.SetInfo();ad_user.GetInfo()
>   ad_user.LoginScript='login.bat'
>   ad_user.AccountDisabled=0
>   ad_user.setpassword('the password')
>   ad_user.Put('pwdLastSet',0) #-- force reset of password
>   ad_user.SetInfo()
>
>
> location='LDAP://OU=sales,DC=shay,DC=com'
> user={'first':'shay','last':'smith','login':'shay123'}
> add_acct(location,user)
>
>
> location='LDAP://OU=sales,DC=shay,DC=com'
> user={'first':'ron','last':'smith','login':'ron1267'}
> add_acct(location,user)
>
>
>
>   
Your formal parameter "account" needs to be "user" to match the references.

What could be a loop?  You have two users added at the bottom which 
could be done as a loop, but only if you have some structure to loop 
over.  So before you make it a loop, you have to decide what that 
structure is, and whether creating it is more work than just continuing 
with your present model of an unrolled loop.

Examples of structures might be:
    a list of user information.  But building that list would be as much 
work as you have now.
    a text file.  Good idea.  It might be populated with Excel, or any 
other program that can produce a csv file, or a formatted text file.  Or 
it might be populated with a text editor, with verification.
    an interactive session.  The user types in the information one user 
at  a time, into a form.  You just loop through, presenting the form, 
and processing the information entered.
     an existing user database.  This might be  a loop that transfers 
information from one format to another.

Decide the structure, and you can readily write the loop.

>       
>   



More information about the Python-list mailing list