Script Move and Create AD Workstation Object to an OU

Does anyone have a script that searches AD for computers in a CSV file and move them to a designated OU and if the computer does not exist, create new workstation object in the OU?

Thanks,
shong1997Asked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
>> It is a text file with one computer per line.


Sure. In that case, the script above should to the job.  Test it out before putting it into production.

Regards,

Rob
0
 
yo_beeDirector of ITCommented:
Are you able to use Powershell?
It might be easier to handle it with that, but if not I think we can work a VBS for you.
0
 
shong1997Author Commented:
I don't have a preference. I would like to see if someone in this forum has already created a similar script either in Powershell or VBscript.

Thanks,
0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
RobSampsonCommented:
Hi, what's in the CSV file?  Is it just a text file with one computer per line?
0
 
RobSampsonCommented:
This script should read from computers.txt and move account found, or create the computer object in the target OU.

Regards,

Rob.

If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

strInputFile = "computers.txt"
strTargetOU = "OU=NewComputers,DC=Domain,DC=com"

Set objContainer = GetObject("LDAP://" & strTargetOU)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInputFile, 1, False)
While Not objInput.AtEndOfStream
	strComputer = Trim(objInput.ReadLine)
	If strComputer <> "" Then
		strADsPath = Get_LDAP_User_Properties("computer", "name", strComputer, "adsPath")
		If strADsPath <> "" Then
			On Error Resume Next
			WScript.Echo "Moving " & strADsPath
			objContainer.MoveHere strADsPath, vbNullString
			If Err.Number = 0 Then
				WScript.Echo strComputer & " successfully moved."
			Else
				WScript.Echo strComputer & " failed to move. Error " & Err.Number & ": " & Err.Description
			End If
			Err.Clear
			On Error GoTo 0
		Else
			WScript.Echo strComputer & " not found. Creating computer account."
			Set objComputer = objContainer.Create("Computer", "cn=" & strComputer)
			objComputer.Put "sAMAccountName", strComputer & "$"
			objComputer.Put "userAccountControl", 4096
			On Error Resume Next
			objComputer.SetInfo
			If Err.Number = 0 Then
				WScript.Echo strComputer & " successfully created."
			Else
				WScript.Echo strComputer & " failed to create. Error " & Err.Number & ": " & Err.Description
			End If
			Err.Clear
			On Error GoTo 0
		End If
	End If
Wend
objInput.Close
WScript.Echo "Finished"

Function Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps)
	
	If InStr(strObjectToGet, "\") > 0 Then
		arrGroupBits = Split(strObjectToGet, "\")
		strDC = arrGroupBits(0)
		strDNSDomain = strDC & "/" & "DC=" & Replace(Mid(strDC, InStr(strDC, ".") + 1), ".", ",DC=")
		strObjectToGet = arrGroupBits(1)
	Else
		Set objRootDSE = GetObject("LDAP://RootDSE")
		strDNSDomain = objRootDSE.Get("defaultNamingContext")
	End If

	strBase = "<LDAP://" & strDNSDomain & ">"
	' Setup ADO objects.
	Set adoCommand = CreateObject("ADODB.Command")
	Set adoConnection = CreateObject("ADODB.Connection")
	adoConnection.Provider = "ADsDSOObject"
	adoConnection.Open "Active Directory Provider"
	adoCommand.ActiveConnection = adoConnection

 
	' Filter on user objects.
	'strFilter = "(&(objectCategory=person)(objectClass=user))"
	strFilter = "(&(objectClass=" & strObjectType & ")(" & strSearchField & "=" & strObjectToGet & "))"

	' Comma delimited list of attribute values to retrieve.
	strAttributes = strCommaDelimProps
	arrProperties = Split(strCommaDelimProps, ",")

	' Construct the LDAP syntax query.
	strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
	adoCommand.CommandText = strQuery
	adoCommand.Properties("Page Size") = 100
	adoCommand.Properties("Timeout") = 30
	adoCommand.Properties("Cache Results") = False

	WScript.Echo ""
	WScript.Echo "Executing " & strQuery
	' Run the query.
	Set adoRecordset = adoCommand.Execute
	' Enumerate the resulting recordset.
	Do Until adoRecordset.EOF
	    ' Retrieve values and display.    
	    For intCount = LBound(arrProperties) To UBound(arrProperties)
	    	If strDetails = "" Then
	    		strDetails = adoRecordset.Fields(intCount).Value
	    	Else
	    		strDetails = strDetails & VbCrLf & adoRecordset.Fields(intCount).Value
	    	End If
	    Next
	    ' Move to the next record in the recordset.
	    adoRecordset.MoveNext
	Loop

	' Clean up.
	adoRecordset.Close
	adoConnection.Close
	Get_LDAP_User_Properties = strDetails

End Function 

Open in new window

0
 
shong1997Author Commented:
It is a text file with one computer per line.

Thanks
0
 
shong1997Author Commented:
I tested this script in the lab, it works flawlessly.
0
 
RobSampsonCommented:
Great. Thanks for the grade.

Rob.
0
All Courses

From novice to tech pro — start learning today.