'Below script to create number of computers in AD--for testing
'Below script to create number of computers in AD--for testing
'==============================================================================
'
' Description: This script creates multiple sequential computer accounts
' in an AD OU. It appends a 3 digit number to the base name starting with
' the number entered at the prompt.
' ==============================================================================
Option Explicit
'Define Constants
Const ADS_SCOPE_ONELEVEL = 1
'Declare Variables
Dim DQ
Dim strAdmin
Dim intRecord
Dim objShell
Dim objNetwork
Dim intWarn
Dim objRootDSE
Dim strADsPath
Dim objConnection
Dim objCommand
Dim strOUPath
Dim objRecordSet
Dim strBaseName
Dim intRecordMax
Dim bEnabled
Dim objOU
Dim strNewComputerName
Dim objNewComputer
Dim strDomainDN
Dim strDomainFQDN
Dim intOULevel
Dim strSearchADsPath
Dim intStartNumber
'Set variables
DQ = Chr(34)
'Create Objects
Set objShell = CreateObject("Wscript.Shell")
Set objNetwork = CreateObject("WScript.NetWork")
'Verifies script was run using Cscript, and if not relauches it using Cscript
If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
objShell.Popup "Relaunching script with Cscript in 5 seconds...", 5, _
"Script Host Message", 48
objShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & _
DQ & WScript.scriptFullName & DQ, 1, False
Script.Quit 0
End If
'Warn User
intWarn = MsgBox("This will make changes to AD." & VbCr & _
"Are you sure you want to do this?", 308, "ID 10 T Check")
'308 = Yes/No (4) + 'Exclaimation (48) + Default Button 2 (256)
If intWarn = vbNo Then
WScript.Quit 0
End If
'Construct an ADsPath to the Current Domain with rootDSE
Set objRootDSE = GetObject("LDAP://rootDSE")
strADsPath = "LDAP://" & objRootDSE.Get("defaultNamingContext")
'Convert domain Distinguished Name to FQDN format
strDomainDN = objRootDSE.Get("defaultNamingContext")
strDomainFQDN = Replace(Replace(strDomainDN, "DC=", ""), ",", ".")
'Connect to Active Directory
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_ONELEVEL
'Prompt for Path to OU
Do
strOUPath = _
InputBox("Please enter the path to the OU where the computer accounts " & _
" will be created - Seperate OUs With a \", "OU Path Input", "TopOU\SubOU")
If strOUPath = False Then
WScript.Quit
End If
Loop Until strOUPath <> ""
'Split OU path by OU
strOUPath = UCase(strOUPath)
strOUPath = Split(strOUPath, "\")
'Prepare variables for search
intOULevel = 0
strSearchADsPath = strADsPath
'Search through each OU level in path provided
For intOULevel = 0 To UBound(strOUPath)
objCommand.CommandText = "SELECT ADsPath FROM '" & strSearchADsPath & _
"'" & " WHERE objectCategory='organizationalUnit' AND Name = '" & _
strOUPath(intOULevel) & "'"
Set objRecordSet = objCommand.Execute
'Verify OU was found
If objRecordSet.EOF Then
WScript.echo "OU named " & strOUPath(intOULevel) & _
" not found, Exiting script."
WScript.quit
Else
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strSearchADsPath = objRecordSet.Fields("ADsPath").Value
objRecordSet.MoveNext
Loop
End If
Next
'Get current username to use in description field
strAdmin = objNetwork.UserName
'Prompt for the base computer name
Do
strBaseName = _
InputBox("Please enter the base computer name to use for new accounts:", _
"Base Computer Name", "TestPC")
If strBaseName = False Then
WScript.Quit
End If
Loop Until strBaseName <> ""
strBaseName = UCase(strBaseName)
'Prompt for starting computer number
Do
intStartNumber = _
InputBox("Please enter the beginning number to use in computer names:", _
"Starting Computer Number", "001")
If intStartNumber = False Then
WScript.Quit
End If
Loop Until intStartNumber <> ""
intStartNumber = CInt(intStartNumber)
intRecord = intStartNumber
'Prompt for number of accounts to be created
Do
intRecordMax = _
InputBox("Please enter the number of accounts to be created", _
"Count Input", "10")
If intRecordMax = False Then
WScript.Quit
End If
Loop Until intRecordMax <> ""
intRecordMax = CInt(intRecordMax)
'Bind to OU that computers will be created in
Set objOU = GetObject(strSearchADsPath)
'Create the user accounts
Do Until intRecord = intRecordMax + intStartNumber
intRecord = Right("000" & intRecord, 3)
strNewComputerName = strBaseName & intRecord
WScript.Echo "Creating " & strNewComputerName
Set objNewComputer = objOU.Create("Computer", "cn= " & strNewComputerName)
objNewComputer.Put "samAccountName", strNewComputerName & "$"
objNewComputer.Put "userAccountControl", 4096
objNewComputer.Put "description", "Account created: " & Date() & " by: " _
& strAdmin
objNewComputer.SetInfo 'Writes settings to AD
intRecord = intRecord + 1
Loop
WScript.Echo
WScript.echo "Finished creating computer accounts."
'==============================================================================
'
' Description: This script creates multiple sequential computer accounts
' in an AD OU. It appends a 3 digit number to the base name starting with
' the number entered at the prompt.
' ==============================================================================
Option Explicit
'Define Constants
Const ADS_SCOPE_ONELEVEL = 1
'Declare Variables
Dim DQ
Dim strAdmin
Dim intRecord
Dim objShell
Dim objNetwork
Dim intWarn
Dim objRootDSE
Dim strADsPath
Dim objConnection
Dim objCommand
Dim strOUPath
Dim objRecordSet
Dim strBaseName
Dim intRecordMax
Dim bEnabled
Dim objOU
Dim strNewComputerName
Dim objNewComputer
Dim strDomainDN
Dim strDomainFQDN
Dim intOULevel
Dim strSearchADsPath
Dim intStartNumber
'Set variables
DQ = Chr(34)
'Create Objects
Set objShell = CreateObject("Wscript.Shell")
Set objNetwork = CreateObject("WScript.NetWork")
'Verifies script was run using Cscript, and if not relauches it using Cscript
If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
objShell.Popup "Relaunching script with Cscript in 5 seconds...", 5, _
"Script Host Message", 48
objShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & _
DQ & WScript.scriptFullName & DQ, 1, False
Script.Quit 0
End If
'Warn User
intWarn = MsgBox("This will make changes to AD." & VbCr & _
"Are you sure you want to do this?", 308, "ID 10 T Check")
'308 = Yes/No (4) + 'Exclaimation (48) + Default Button 2 (256)
If intWarn = vbNo Then
WScript.Quit 0
End If
'Construct an ADsPath to the Current Domain with rootDSE
Set objRootDSE = GetObject("LDAP://rootDSE")
strADsPath = "LDAP://" & objRootDSE.Get("defaultNamingContext")
'Convert domain Distinguished Name to FQDN format
strDomainDN = objRootDSE.Get("defaultNamingContext")
strDomainFQDN = Replace(Replace(strDomainDN, "DC=", ""), ",", ".")
'Connect to Active Directory
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_ONELEVEL
'Prompt for Path to OU
Do
strOUPath = _
InputBox("Please enter the path to the OU where the computer accounts " & _
" will be created - Seperate OUs With a \", "OU Path Input", "TopOU\SubOU")
If strOUPath = False Then
WScript.Quit
End If
Loop Until strOUPath <> ""
'Split OU path by OU
strOUPath = UCase(strOUPath)
strOUPath = Split(strOUPath, "\")
'Prepare variables for search
intOULevel = 0
strSearchADsPath = strADsPath
'Search through each OU level in path provided
For intOULevel = 0 To UBound(strOUPath)
objCommand.CommandText = "SELECT ADsPath FROM '" & strSearchADsPath & _
"'" & " WHERE objectCategory='organizationalUnit' AND Name = '" & _
strOUPath(intOULevel) & "'"
Set objRecordSet = objCommand.Execute
'Verify OU was found
If objRecordSet.EOF Then
WScript.echo "OU named " & strOUPath(intOULevel) & _
" not found, Exiting script."
WScript.quit
Else
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strSearchADsPath = objRecordSet.Fields("ADsPath").Value
objRecordSet.MoveNext
Loop
End If
Next
'Get current username to use in description field
strAdmin = objNetwork.UserName
'Prompt for the base computer name
Do
strBaseName = _
InputBox("Please enter the base computer name to use for new accounts:", _
"Base Computer Name", "TestPC")
If strBaseName = False Then
WScript.Quit
End If
Loop Until strBaseName <> ""
strBaseName = UCase(strBaseName)
'Prompt for starting computer number
Do
intStartNumber = _
InputBox("Please enter the beginning number to use in computer names:", _
"Starting Computer Number", "001")
If intStartNumber = False Then
WScript.Quit
End If
Loop Until intStartNumber <> ""
intStartNumber = CInt(intStartNumber)
intRecord = intStartNumber
'Prompt for number of accounts to be created
Do
intRecordMax = _
InputBox("Please enter the number of accounts to be created", _
"Count Input", "10")
If intRecordMax = False Then
WScript.Quit
End If
Loop Until intRecordMax <> ""
intRecordMax = CInt(intRecordMax)
'Bind to OU that computers will be created in
Set objOU = GetObject(strSearchADsPath)
'Create the user accounts
Do Until intRecord = intRecordMax + intStartNumber
intRecord = Right("000" & intRecord, 3)
strNewComputerName = strBaseName & intRecord
WScript.Echo "Creating " & strNewComputerName
Set objNewComputer = objOU.Create("Computer", "cn= " & strNewComputerName)
objNewComputer.Put "samAccountName", strNewComputerName & "$"
objNewComputer.Put "userAccountControl", 4096
objNewComputer.Put "description", "Account created: " & Date() & " by: " _
& strAdmin
objNewComputer.SetInfo 'Writes settings to AD
intRecord = intRecord + 1
Loop
WScript.Echo
WScript.echo "Finished creating computer accounts."
0 comments:
Post a Comment