VBScript to create computer accounts
Note1 : I have a new script to create a computer.
Note 2: I have a VBScript to create a computer from an Excel Spreadsheet
♦
VBScript Create Computer Accounts
What the script does is to connect to Active Directory, then create a XPSimple in the ‘Computer’ container.
GetObject connects to ("LDAP://rootDSE") that is why the script will work on any domain. Then it extracts the domain name ("defaultNamingContext") and builds the path to the "Computer" container.
‘ VBScript to create one computer
‘ Guy Thomas January 2003
strComputer = "XPSimple"
Set objRootDSE = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://cn=Computers," & _
objRootDSE.Get("defaultNamingContext"))
Set objLeaf = objContainer.Create("Computer", "cn=" & strComputer)
objLeaf.Put "sAMAccountName", strComputer & "$"
objLeaf.Put "userAccountControl", 4096
objLeaf.SetInfo
Learning Points
- UserAccountControl 512 is Enabled, 514 is disabled.
- strComputer is a string variable for the name of the computer. Feel free to change strComputer = "XPSimple" to a different computer name.
- Even though this is a computer object, it still needs the sAMAccountName.
And then the script developed into this monster!
Goals for the ‘Monster Script’
I decided I wanted the script to create machine accounts, not in the ‘Computer’ container, but an OU of my choice.
One computer was not enough, I wanted to make 10 (or more).
‘VBScript
‘ Created by Guy Thomas January 2003
‘ Script to create ten computers in strOU
Option Explicit
Dim objLeaf, objRootDSE, objDomain, objOU, objComputer
Dim strXPStation, strOU, strSam, strDomain
Dim intComputerNum
strXPStation = "XPMachine"
strOU = "Accounts"
strSam = "sAMAccountName"
‘ Get Domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")
‘ Create OU = strOU
Set objDomain = GetObject("LDAP://" &strDomain )
Set objOU = objDomain.Create("organizationalUnit", "ou=" & strOU)
objOU.SetInfo
‘ Create Computers
For intComputerNum = 1 To 10
Set objLeaf = objOU.Create("Computer", "cn=" & StrXPStation & intComputerNum)
objLeaf.Put strSam, strXPStation & intComputerNum & "$"
‘ 512 means Enable the Account
objLeaf.Put "userAccountControl",512
objLeaf.SetInfo
objLeaf.SetInfo
Next
intComputerNum = intComputerNum -1
WScript.Echo intComputerNum & " Machines created in " & strOU & " OU"
WScript.Quit
Guy Recommends: SolarWinds’ Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the users with their fields in the top row, and save as .csv file. Then launch this FREE utility and match your fields with AD’s attributes, click and import the users.
Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.
If you need more comprehensive software, download a free trial of SAM (Server & Application Monitor)
Note1 : I have a new script to create a computer.
Note 2: I have a VBScript to create a computer from an Excel Spreadsheet
Learning Points
- This script creates an OU that you specify with the strOU. So feel free to alter strOU = "Accounts" to another name.
- The ‘For …… Next’ loop is responsible for making multiple machines, each with a different digit at the end of the XPMachine name.
Download your eBook: How to create users with a VBScript- only $6.25
Save hours of frustration and buy Guy’s eBook. The features include: detailed examples on how to import LDAP properties from a spreadsheet, Do…Loop Until explained, connect to your domain with RootDSE.
You get a printer friendly version with copy enabled, and no expiry date.