Announcement

Collapse
No announcement yet.

VBS/Batch: creating ou's and computers and users

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBS/Batch: creating ou's and computers and users

    hello,

    i'm looking for a script which creates about 10 OU's with each about 100 computers and users in it per OU... prefferable filtert with new ou's in it:
    so for example: ou sales has an ou computers and a ou users.


    it's for testing purposes, and i really don't like to create it again, and again and again and....

    if someone got a script, please.. may i use it
    Marcel
    Technical Consultant
    Netherlands
    http://www.phetios.com
    http://blog.nessus.nl

    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
    "No matter how secure, there is always the human factor."

    "Enjoy life today, tomorrow may never come."
    "If you're going through hell, keep going. ~Winston Churchill"


  • #2
    What kind of system do you have?
    Windows 2003 AD?
    w2k?
    MCSE w2k
    MCSA w2k - MCSA w2k MESSAGING
    MCDBA SQL2k

    Comment


    • #3
      sorry to tell..
      win2003 AD
      standard edition..
      Marcel
      Technical Consultant
      Netherlands
      http://www.phetios.com
      http://blog.nessus.nl

      MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
      "No matter how secure, there is always the human factor."

      "Enjoy life today, tomorrow may never come."
      "If you're going through hell, keep going. ~Winston Churchill"

      Comment


      • #4
        Example Script to create an OU called Suppliers
        On the surface, the idea of this script is simply to create an OU called suppliers (building on what we did last week). However, underneath the covers, my purpose is to test error correcting code. An all too common scenario is that you run a script to create an object, but that object already exists. We need to anticipate this duplication error in our script.

        One technique is to ignore the error with On Error Resume Next. A better alternative is to create a branch in the VBScript which handles the error gracefully.

        Instructions

        Which OU will create in your test script? My script uses OU=Suppliers.
        Copy and paste the script below into notepad.
        Save the file with .vbs extension e.g. ErrOU.vbs.
        Double click and examine the message boxes.
        Here is one script which I would like you to run for a second time.
        Remember to use Refresh in Active Directory to check what has happened. Sometimes just pressing F5 is not good enough.



        ' ErrOU.vbs
        ' Purpose VBScript to demonstrate Error Handling.
        ' Learning Points: Create OU
        ' Usage this as a Template Script to introduce error handling
        ' Author Guy Thomas http://computerperformance.co.uk/
        ' Version 1.4 - November 28th 2004
        ' --------------------------------------------------------------'
        Option Explicit
        Dim objRoot, objOU, objDomain, objUser
        Dim strDNS, strContainer, strOUDescription, strOURebuild

        ' Set string variables
        strContainer = "OU=Suppliers"
        strOUDescription = "Guy's Contacts OU"
        strOURebuild = "Rebuilt Guy's Contacts OU"

        ' Section to attach to Active Directory
        Set objRoot = GetObject("LDAP://rootDSE")
        strDNS = objRoot.Get("defaultNamingContext")
        Set objDomain = GetObject("LDAP://" & strDNS)

        ' Section to create an OU
        On Error Resume Next
        Set objOU=objDomain.Create("organizationalUnit", strContainer)
        objOU.Put "Description", strOUDescription
        objOU.SetInfo

        ' --------------------------------------------
        ' Main point of the script
        ' Err correcting code
        If Err.Number <> vbEmpty Then
        Wscript.Echo "Error number " & Err.Number
        Err.Clear
        objOU=objDomain.Delete("organizationalUnit", strContainer)
        Set objOU=objDomain.Create("organizationalUnit", strContainer)
        objOU.Put "Description", strOURebuild
        objOU.SetInfo
        On Error GoTo 0
        Else
        WScript.Echo "No Error (" & Err.Number & ") OU Created 1st time"
        End If

        WScript.Echo "Look in " & strDNS & " for (F5) " & strContainer
        WScript.Quit


        ' End of Script

        Learning Points
        Note 1: The script should work when you run it a second time, however you get a different message.

        Tip: To check the script again, either delete the Suppliers OU, or amend strContainer, for example strContainer = Supplier2

        Note 2: Observe where the script ignores the error. (On Error Resume Next). Then see where it reverts to normal with :
        - On Error GoTo 0.

        Note 3: In line 31, vbEmpty is an alternative to 0 (Zero)

        Note 4: err.clear it is good practice to reset the error so that it does not interfere with subsequent lines of code.

        Note 5: Did you spot that I changed the strOUDescription to strOURebuild?
        MCSE w2k
        MCSA w2k - MCSA w2k MESSAGING
        MCDBA SQL2k

        Comment


        • #5
          You can see this site as well:


          http://www.microsoft.com/technet/scr...s/default.mspx

          Have fun
          MCSE w2k
          MCSA w2k - MCSA w2k MESSAGING
          MCDBA SQL2k

          Comment


          • #6
            i'm aware of that site..
            i just hope that someone has such kind of a script..
            naming convention is not important..

            i just want that hé creates about 10 ou's, each filled with about 100 computers and 100 users..

            i just hoped that someone has that kind of script... before i need to program it to myself
            Marcel
            Technical Consultant
            Netherlands
            http://www.phetios.com
            http://blog.nessus.nl

            MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
            "No matter how secure, there is always the human factor."

            "Enjoy life today, tomorrow may never come."
            "If you're going through hell, keep going. ~Winston Churchill"

            Comment


            • #7
              Code:
              Set objDomain = GetObject("LDAP://dc=domain,dc=co,dc=uk")
              
              ' Creates 3 OU's with nothing in them
              '
              ------------------------------------------------------------------------
              
              Set objOU = objDomain.Create("organizationalUnit", "ou=Sales")
              objOU.SetInfo
              Set objOU = objDomain.Create("organizationalUnit", "ou=IT")
              objOU.SetInfo
              Set objOU = objDomain.Create("organizationalUnit", "ou=Admin")
              objOU.SetInfo
              
              ' Creates 100 Users in the Sales OU
              --------------------------------------------------------------------------
              
              For i = 1 To 100
              	Set objAdd = GetObject("LDAP://OU=Sales,dc=domain,dc=co,dc=uk")
                  	Set objLeaf = objAdd.Create("User", "cn=UserNo" & i)
                  	objLeaf.Put "sAMAccountName", "UserNo" & i
              	objLeaf.SetInfo
              
              Set objUser = GetObject("LDAP://cn=UserNo" & i & ",ou=Sales,dc=domain,dc=co,dc=uk")
              
              		objUser.AccountDisabled = FALSE
              		objUser.SetInfo
              Next
              
              
              ' Creates another 100 Users in the IT OU
              --------------------------------------------------------------------------
              
              For i = 101 To 200
              	Set objAdd = GetObject("LDAP://OU=IT,dc=domain,dc=co,dc=uk")
                  	Set objLeaf = objAdd.Create("User", "cn=UserNo" & i)
                  	objLeaf.Put "sAMAccountName", "UserNo" & i
              	objLeaf.SetInfo
              
              Set objUser = GetObject("LDAP://cn=UserNo" & i & ",ou=IT,dc=domain,dc=co,dc=uk")
              
              		objUser.AccountDisabled = FALSE
              		objUser.SetInfo
              Next
              
              
              ' Creates yet another 100 Users in the Admin OU
              --------------------------------------------------------------------------
              
              For i = 201 To 300
              	Set objAdd = GetObject("LDAP://OU=Admin,dc=domain,dc=co,dc=uk")
                  	Set objLeaf = objAdd.Create("User", "cn=UserNo" & i)
                  	objLeaf.Put "sAMAccountName", "UserNo" & i
              	objLeaf.SetInfo
              
              Set objUser = GetObject("LDAP://cn=UserNo" & i & ",ou=Admin,dc=domain,dc=co,dc=uk")
              
              		objUser.AccountDisabled = FALSE
              		objUser.SetInfo
              Next
              I've only done 3 OU's with no child OU's, very simple just to add create and set user in. Just copy the stages and rename to your own domain.

              Give us a shout if you have any probs.

              topper
              * Shamelessly mentioning "Don't forget to add reputation!"

              Comment


              • #8
                You can use the command line tool from MS


                http://www.microsoft.com/technet/pro...c558f82fa.mspx

                Easy!
                MCSE w2k
                MCSA w2k - MCSA w2k MESSAGING
                MCDBA SQL2k

                Comment


                • #9
                  Sorry Marcel, forgot about the computers. Also made it a little easier to understand. Just changes the Domainname and OUname at the top then run the script, then just have 10 scripts. (or you could combine into 1 fairly easily)

                  Code:
                  DomainName = "dc=domain,dc=co,dc=uk"
                  OUName = "Sales"
                  Set objDomain = GetObject("LDAP://" & DomainName)
                  
                  Set objOU = objDomain.Create("organizationalUnit", "ou=" & OUName)
                  objOU.SetInfo
                  
                  Set objDomain = GetObject("LDAP://ou="&OuName &"," & DomainName)
                  
                  Set objOU = objDomain.Create("organizationalUnit", "ou=Users")
                  objOU.SetInfo
                  Set objOU = objDomain.Create("organizationalUnit", "ou=Computers")
                  objOU.SetInfo
                  
                  
                  For i = 1 To 100
                  	Set objAdd = GetObject("LDAP://OU=Users,OU="& OUName &"," & DomainName)
                      	Set objLeaf = objAdd.Create("User", "cn="& OUName &"User" & i)
                      	objLeaf.Put "sAMAccountName", OUName &"User" & i
                  	objLeaf.SetInfo
                  		Set objUser = GetObject("LDAP://cn="& OUName &"User" & i & ",ou=Users,ou="& OUName &"," & DomainName)
                  		objUser.AccountDisabled = FALSE
                  		objUser.SetInfo
                  
                  	Set objAdd = GetObject("LDAP://OU=Computers,OU="& OUName &"," & DomainName)
                  	Set objComputer = objAdd.Create("Computer","cn="& OUName & "Comp" & i)
                  	objComputer.Put "sAMAccountName", OUName &"Comp" & i & "$"
                  	objComputer.Put "userAccountControl", 4096
                  	objComputer.SetInfo 
                  Next
                  topper
                  * Shamelessly mentioning "Don't forget to add reputation!"

                  Comment


                  • #10
                    Thnx topper..
                    that's where i was looking for..

                    i rebuild a little bit to:


                    Code:
                    Set objDomain = GetObject("LDAP://dc=Documentatie,dc=local") 
                    
                    ' Creates 3 OU's with nothing in them 
                    ' 
                    ' ------------------------------------------------------------------------ 
                    
                    Set objOU = objDomain.Create("organizationalUnit", "ou=Sales") 
                    objOU.SetInfo 
                    Set objOU = objDomain.Create("organizationalUnit", "ou=IT") 
                    objOU.SetInfo 
                    Set objOU = objDomain.Create("organizationalUnit", "ou=Admin") 
                    objOU.SetInfo 
                    
                    ' Creating child OU's
                    '---------------------------------------------------------------------------
                    Set ObjChildOU=GetObject("LDAP://OU=Sales,dc=Documentatie,dc=local") 
                    Set objOU = objChildOU.Create("organizationalUnit", "ou=Computers") 
                    objOU.SetInfo 
                    Set objOU = objChildOU.Create("organizationalUnit", "ou=Users") 
                    objOU.SetInfo 
                    
                    Set ObjChildOU=GetObject("LDAP://OU=IT,dc=Documentatie,dc=local") 
                    Set objOU = objChildOU.Create("organizationalUnit", "ou=Computers") 
                    objOU.SetInfo 
                    Set objOU = objChildOU.Create("organizationalUnit", "ou=Users") 
                    objOU.SetInfo 
                    
                    Set ObjChildOU=GetObject("LDAP://OU=Admin,dc=Documentatie,dc=local") 
                    Set objOU = objChildOU.Create("organizationalUnit", "ou=Computers") 
                    objOU.SetInfo 
                    Set objOU = objChildOU.Create("organizationalUnit", "ou=Users") 
                    objOU.SetInfo 
                    
                    
                    
                    ' Creates 100 Users in the Sales OU 
                    '-------------------------------------------------------------------------- 
                    
                    For i = 1 To 100 
                       Set objAdd = GetObject("LDAP://OU=Users,OU=Sales,dc=Documentatie,dc=local") 
                           Set objLeaf = objAdd.Create("User", "cn=UserNo" & i) 
                           objLeaf.Put "sAMAccountName", "UserNo" & i 
                       objLeaf.SetInfo 
                    
                    Set objUser = GetObject("LDAP://cn=UserNo" & i & ",OU=Users,ou=Sales,dc=Documentatie,dc=local") 
                    
                          objUser.AccountDisabled = FALSE 
                          objUser.SetInfo 
                    Next 
                    
                    
                    ' Creates another 100 Users in the IT OU 
                    '-------------------------------------------------------------------------- 
                    
                    For i = 101 To 200 
                       Set objAdd = GetObject("LDAP://OU=Users,OU=IT,dc=documentatie,dc=local") 
                           Set objLeaf = objAdd.Create("User", "cn=UserNo" & i) 
                           objLeaf.Put "sAMAccountName", "UserNo" & i 
                       objLeaf.SetInfo 
                    
                    Set objUser = GetObject("LDAP://cn=UserNo" & i & ",OU=Users,ou=IT,dc=documentatie,dc=local") 
                    
                          objUser.AccountDisabled = FALSE 
                          objUser.SetInfo 
                    Next 
                    
                    
                    ' Creates yet another 100 Users in the Admin OU 
                    '-------------------------------------------------------------------------- 
                    
                    For i = 201 To 300 
                    Set objAdd = GetObject("LDAP://OU=Users,OU=Admin,dc=documentatie,dc=local") 
                           Set objLeaf = objAdd.Create("User", "cn=UserNo" & i) 
                           objLeaf.Put "sAMAccountName", "UserNo" & i 
                       objLeaf.SetInfo 
                    
                    Set objUser = GetObject("LDAP://cn=UserNo" & i & ",OU=Users,ou=Admin,dc=documentatie,dc=local") 
                    
                          objUser.AccountDisabled = FALSE 
                          objUser.SetInfo 
                    Next
                    
                    
                    ' Creating 100 Computers in the Sales OU
                    '-------------------------------------------------------------------------------------------------
                    
                    For i = 1 To 100 
                    
                    Set objadd = nothing
                    set objleaf = nothing
                    
                       Set objAdd = GetObject("LDAP://OU=Computers,OU=Sales,dc=Documentatie,dc=local") 
                           Set objLeaf = objAdd.Create("Computer", "cn=ComputerNo" & i) 
                           objLeaf.Put "sAMAccountName", "ComputerNO" & i 
                       objLeaf.SetInfo 
                    
                    Set objComputer = GetObject("LDAP://cn=ComputerNO" & i  & ",OU=Computers,ou=Sales,dc=Documentatie,dc=local") 
                    
                          objComputer.AccountDisabled = FALSE 
                          objComputer.SetInfo 
                    Next 
                    
                    
                    ' Creating 100 Computers in the IT OU
                    '-------------------------------------------------------------------------------------------------
                    
                    For i = 101 To 200 
                    
                    Set objadd = nothing
                    set objleaf = nothing
                    
                       Set objAdd = GetObject("LDAP://OU=Computers,OU=IT,dc=Documentatie,dc=local") 
                           Set objLeaf = objAdd.Create("Computer", "cn=ComputerNo" & i) 
                           objLeaf.Put "sAMAccountName", "ComputerNO" & i 
                       objLeaf.SetInfo 
                    
                    Set objComputer = GetObject("LDAP://cn=ComputerNO" & i  & ",OU=Computers,ou=IT,dc=Documentatie,dc=local") 
                    
                          objComputer.AccountDisabled = FALSE 
                          objComputer.SetInfo 
                    Next 
                    
                    
                    
                    ' Creating 100 Computers in the Admin OU
                    '-------------------------------------------------------------------------------------------------
                    
                    For i = 201 To 300 
                    
                    Set objadd = nothing
                    set objleaf = nothing
                    
                       Set objAdd = GetObject("LDAP://OU=Computers,OU=Admin,dc=Documentatie,dc=local") 
                           Set objLeaf = objAdd.Create("Computer", "cn=ComputerNo" & i) 
                           objLeaf.Put "sAMAccountName", "ComputerNO" & i 
                       objLeaf.SetInfo 
                    
                    Set objComputer = GetObject("LDAP://cn=ComputerNO" & i  & ",OU=Computers,ou=Admin,dc=Documentatie,dc=local") 
                    
                          objComputer.AccountDisabled = FALSE 
                          objComputer.SetInfo 
                    Next
                    more adjustments will come... more users, more computers, more ou's... but this is already a great start..

                    thnx again
                    Marcel
                    Technical Consultant
                    Netherlands
                    http://www.phetios.com
                    http://blog.nessus.nl

                    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                    "No matter how secure, there is always the human factor."

                    "Enjoy life today, tomorrow may never come."
                    "If you're going through hell, keep going. ~Winston Churchill"

                    Comment


                    • #11
                      Thats too much code

                      Real man do it in batch :
                      Code:
                      @echo off
                      
                      SET DOMAIN_NC=ou=test,dc=antid0t,dc=net
                      SET UPN_SUFFIX=antid0t.net
                      SET N_OBJ=3
                      SET N_OUS=3
                      
                      for /l %%i IN (1,1,%N_OUS%) do (
                      	echo ========Start site OU "ou%%i" ===============
                      	dsadd ou "ou=ou%%i,%DOMAIN_NC%" 
                      	dsadd ou "ou=Computers,ou=ou%%i,%DOMAIN_NC%" 
                      	dsadd ou "ou=Users,ou=ou%%i,%DOMAIN_NC%" 
                      	
                      	for /l %%j IN (1,1,%N_OBJ%) do (
                      		dsadd user cn=user_ou%%i_%%j,ou=Users,ou=ou%%i,%DOMAIN_NC% -samid user_ou%%i_%%j -disabled no -pwd PassW0rd -upn user_ou%%i_%%[email protected]%UPN_SUFFIX%
                      		dsadd computer cn=computer_ou%%i_%%j,ou=Computers,ou=ou%%i,%DOMAIN_NC% -samid computer_ou%%i_%%j$
                      	)
                      	echo ========End site OU "ou%%i" ===============
                      	echo.
                      )
                      Guy Teverovsky
                      "Smith & Wesson - the original point and click interface"

                      Comment


                      • #12
                        Too much code just for creating OU's on A.D.

                        You can just use the tool that i said.

                        Example:

                        Run, CMD then copy your script and paste into the command window. Alternatively type it starting with dsadd ou .........

                        dsadd ou "ou=guyds, dc=mydomain, dc=com"


                        Note 1 : dsadd ou. This command tells Active Directory which object to create, in this case an OU (not a user).

                        Note 2 : You only really need speech marks if there is a space in any of your names. So ou=guyds, dc=mydomain, dc=com would work fine, but ou=GUY Space DS, dc=mydomain, dc=com fails because of the spaces in the GUY Space DS, name. In this second example you would type: "ou=GUY Space DS, dc=mydomain, dc=com"
                        MCSE w2k
                        MCSA w2k - MCSA w2k MESSAGING
                        MCDBA SQL2k

                        Comment


                        • #13
                          no, i want 5 - 10 root ou's. with 2 child ou's.
                          one child: computers. one child users.
                          each child must contain 100 user accounts and 100 computer accounts.

                          the name of a computer and user doesn't matter. just like the "root" ou's.

                          it's only for documentation DC (i'm writing documentation and a desgin for ePolicy Orchestrator)
                          Marcel
                          Technical Consultant
                          Netherlands
                          http://www.phetios.com
                          http://blog.nessus.nl

                          MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                          "No matter how secure, there is always the human factor."

                          "Enjoy life today, tomorrow may never come."
                          "If you're going through hell, keep going. ~Winston Churchill"

                          Comment


                          • #14
                            ONE
                            dsadd ou "ou=guyds, dc=mydomain, dc=com"
                            TWO CHILDS
                            dsadd ou "ou=child1,ou=guyds, dc=mydomain, dc=com"
                            dsadd ou "ou=child2,ou=guyds, dc=mydomain, dc=com"
                            TWO
                            dsadd ou "ou=guyds1, dc=mydomain, dc=com"
                            TWO CHILDS
                            dsadd ou "ou=child1,ou=guyds1, dc=mydomain, dc=com"
                            dsadd ou "ou=child2,ou=guyds1, dc=mydomain, dc=com"
                            THREE
                            dsadd ou "ou=guyds2, dc=mydomain, dc=com"
                            TWO CHILDS
                            dsadd ou "ou=child1,ou=guyds2, dc=mydomain, dc=com"
                            dsadd ou "ou=child2,ou=guyds2, dc=mydomain, dc=com"

                            You want this? It's easy to complete.
                            MCSE w2k
                            MCSA w2k - MCSA w2k MESSAGING
                            MCDBA SQL2k

                            Comment


                            • #15
                              ok, and what about the users and computer accounts?
                              Marcel
                              Technical Consultant
                              Netherlands
                              http://www.phetios.com
                              http://blog.nessus.nl

                              MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                              "No matter how secure, there is always the human factor."

                              "Enjoy life today, tomorrow may never come."
                              "If you're going through hell, keep going. ~Winston Churchill"

                              Comment

                              Working...
                              X