home *** CD-ROM | disk | FTP | other *** search
/ The Games Machine 29 / XENIATGM29.iso / ie / ie40pp1 / ie4 / wsh.cab / addusers.vbs < prev    next >
Text File  |  1996-10-21  |  6KB  |  171 lines

  1. ' Windows Script Host Sample Script
  2. '
  3. ' ------------------------------------------------------------------------
  4. '               Copyright (C) 1996 Microsoft Corporation
  5. '
  6. ' You have a royalty-free right to use, modify, reproduce and distribute
  7. ' the Sample Application Files (and/or any modified version) in any way
  8. ' you find useful, provided that you agree that Microsoft has no warranty,
  9. ' obligations or liability for any Sample Application Files.
  10. ' ------------------------------------------------------------------------
  11. '
  12. 'This script adds and deletes users from the Windows NT DS
  13. 'via OLE DS. The script reads an EXCEL workbook that contains a page 
  14. 'of users to add and a page of users to delete.
  15. '
  16. 'The sample uses the directory root "LDAP://C=US/O=ArcadiaBay"
  17. 'Change the dirctory path in the EXCEL spreadsheet to match your DS
  18. 'before running this sample.
  19. '
  20. 'We have provided a VB SCript (makeod.ds) that creates an OU.
  21. 'To bootstrap the demo, use makeou.vbs to create an ou.
  22. '
  23. 'To add users, run ADD-USERS.VBS with %windir%\system32\wsamples\AddUsers.XLS.
  24. 'To Delete users, run ADD-USERS.VBS with %windir%\system32\wsamples\DelUsers.XLS.
  25.  
  26.  
  27.     Dim oXL
  28.     Dim u
  29.     Dim c
  30.     Dim root
  31.     Dim ou
  32.     Dim TextXL
  33.     Dim CRLF
  34.     dim oArgs
  35.  
  36.     
  37.     'Get the command line args
  38.     set oArgs=wscript.arguments
  39.  
  40.     CRLF = Chr(13) & Chr(10)
  41.   
  42.     'If no command line arguments provided, prompt for file containing users to add/delete
  43.     If oArgs.Count = 0 Then
  44.        TextXL = InputBox("This scripts reads an Excel spread sheet and adds/deletes " & _
  45.        "users from the Windows NT DS via OLE DS." & CRLF & CRLF & _
  46.        "Before starting, change the DS root in the EXCEL spreadsheet to match " & _
  47.        "your DS and run the MAKEOU.VBS script to create an OU." & CRLF & CRLF & _
  48.        "Type in the path of a file containing users to add or delete" & CRLF & CRLF & _
  49.        "Sample Add User file: ADDUSERS.XLS" & CRLF & _
  50.        "Sample Delete User file: DELUSERS.XLS" & CRLF)
  51.     'Else file containing users is the first argument
  52.     Else
  53.       TextXL = oArgs.item(0)
  54.     End If
  55.  
  56.     If TextXL = "" Then
  57.        WScript.Echo "No input file provided.  stopping script now."
  58.        WScript.Quit(1)
  59.     End If
  60.  
  61.     'We will use ou to control loop, so set initial value to null
  62.     ou = ""
  63.    
  64.     'Start EXCEL and display it to the user
  65.     Set oXL = WScript.CreateObject("EXCEL.application")
  66.     oXL.Visible = True
  67.  
  68.     'Open the workbook passed in the command line
  69.     oXL.workbooks.open TextXL
  70.  
  71.     'Activate the Add page 
  72.     oXL.sheets("Add").Activate
  73.  
  74.     'Put the cursor in the starting cell and read the DS root
  75.     oXL.ActiveSheet.range("A2").Activate ' this cell has the DS root in it
  76.  
  77.     'Show it to the user
  78.     'WScript.Echo  oXL.activecell.Value
  79.   
  80.     'This is the starting point in the ds
  81.     root = oXL.activecell.Value
  82.  
  83.     'Step to the next row
  84.     oXL.activecell.offset(1, 0).Activate
  85.  
  86.     'Until we run out of rows
  87.     Do While oXL.activecell.Value <> ""
  88.       
  89.         'if the requested OU is a new one...
  90.         If oXL.activecell.Value <> ou Then
  91.             'Pick up the OU name...
  92.             ou = oXL.activecell.Value
  93.  
  94.             'Compose the OLE DS path...
  95.             s = "LDAP://" + root + "/" + ou
  96.  
  97.             'Show it to the user...
  98.         WScript.Echo s
  99.  
  100.             'And get the object
  101.             Set c = WScript.GetObject(s)
  102.         End If
  103.  
  104.         'Compose the user common name name from first and last names...
  105.         uname = "CN=" + oXL.activecell.offset(0, 1).Value + " " + oXL.activecell.offset(0, 2).Value
  106.  
  107.         'Create the new user object...
  108.         Set u = c.Create("user", uname)
  109.  
  110.         'Set the properties of the new user
  111.         u.Put "givenName", oXL.activecell.offset(0, 1).Value 'givenName
  112.         u.Put "sn", oXL.activecell.offset(0, 2).Value 'sn
  113.         u.Put "mail", oXL.activecell.offset(0, 3).Value 'Email
  114.         u.Put "sAMAccountName", oXL.activecell.offset(0, 4).Value  'Sam Acct
  115.         u.Put "telephoneNumber", oXL.activecell.offset(0, 5).Value  'Phone
  116.  
  117.         'Enable the account, must change pw @ logon
  118.         u.Put "User-Account-Control",16 
  119.  
  120.         '...and update the DS
  121.         u.SetInfo
  122.  
  123.         'Done with this object, discard it
  124.         Set u = Nothing
  125.  
  126.         'Step to the next user...
  127.         oXL.activecell.offset(1, 0).Activate   'Next row
  128.     Loop
  129.  
  130.  
  131.     'Now do deletes
  132.     '
  133.     'Activate the Delete page
  134.     oXL.sheets("Delete").Activate
  135.  
  136.     'Set the cell cursor to the DS root
  137.     oXL.ActiveSheet.range("A2").Activate ' this cell has the DS root in it
  138.  
  139.     'Show it to the user
  140.     'WScript.Echo oXL.activecell.Value
  141.  
  142.     root = oXL.activecell.Value
  143.     oXL.activecell.offset(1, 0).Activate
  144.  
  145.     'Until we run out of rows...
  146.     Do While oXL.activecell.Value <> ""
  147.  
  148.     'If the requested OU is different
  149.         If oXL.activecell.Value <> ou Then
  150.             ou = oXL.activecell.Value
  151.  
  152.         'Compose the new ou path...
  153.             s = "LDAP://" + root + "/" + ou
  154.  
  155.             'Show it to the user
  156.         'WScript.Echo s
  157.  
  158.             'Get the new container
  159.             Set c = WScript.GetObject(s)
  160.         End If
  161.  
  162.         'Compose the user name
  163.         uname = "CN=" + oXL.activecell.offset(0, 1).Value + " " + oXL.activecell.offset(0, 2).Value
  164.     'Delete the user
  165.         Call c.Delete("user", uname)
  166.         oXL.activecell.offset(1, 0).Activate ' next row
  167.     Loop
  168.  
  169.     'Done.  close excel spreadsheet
  170.     oXL.application.quit
  171.