?
Solved

80070005 error on vbscript to move Computers to new OU

Posted on 2011-03-21
5
Medium Priority
?
913 Views
Last Modified: 2012-05-11
I wanted to create a script that a standard user could run via the login script which will move the user's PC to their corresponding computers container in active directory.

The script is rough (as I am no vb expert), but works as an admin user. How can I make it work as a standard user. I get an access denied 80070005 error.  I tried playing with the impersonate command but with no joy. Any ideas ?

Option Explicit
Dim objSysInfo
DIM OU, OU1, OU2, COMPPATH, strADsPath, strADsPath1
DIM objNetwork
DIM objConnection
DIM objCommand
DIM WshShell
DIM WshNetwork
DIM colUserEnvVariables
DIM currentuser
DIM objNewOU
DIm objMoveComputer
DIm objRootDSE
DIM strDNSDomain
DIM test1
Dim strComputer
DIM objWMIService


'======If an error is encountered, keep going======
'On Error Resume Next


Set objSysInfo = CreateObject("ADSystemInfo")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set WshShell = CreateObject("Wscript.Shell")
Set CurrentUser = GetObject("LDAP://" & objSysInfo.UserName)

'Retrieve the collection of system variables
Set colUserEnvVariables = WshShell.Environment("System")


'------------------------------------------------------------------------------------
'-                  Get current OU of the user and comp
'------------------------------------------------------------------------------------
OU = objSysInfo.username
comppath= objSysInfo.computername

'------------------------------------------------------------------------------------
'-         Format the OU string for the query to provide location of new computers container
'------------------------------------------------------------------------------------
OU1= replace(ou, "USERS", "COMPUTERS")
OU2= instr (ou1, "OU=COMPUTERS")
strADsPath= mid (OU1,OU2)
strADsPath1= "OU=" & mid (OU1,OU2)
'------------------------------------------------------------------------------------
'-   TESTING
'------------------------------------------------------------------------------------
'msgbox ou
'msgbox ou1
msgbox ou2
msgbox comppath
msgbox strADsPath

'------------------------------------------------------------------------------------
'move PC to new OU
'------------------------------------------------------------------------------------

strComputer=WshNetwork.ComputerName
msgbox strComputer

CONST ForReading = 1
Const ForAppending = 8
 
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
Set objNewOU = GetObject("LDAP://" & strADsPath)
Set objMoveComputer = objNewOU.MoveHere ("LDAP://" & comppath, "CN=" & strComputer)

0
Comment
Question by:BlueDelta5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 56

Expert Comment

by:McKnife
ID: 35184899
Hi.
Could you provide the goal behind this? Why should this be solved at the client side? What changed to make it necessary?
0
 
LVL 1

Author Comment

by:BlueDelta5
ID: 35184943
At the moment all PC's are in the standard computer  container. We have a user and computer container per dept. As machines are brought online and the user logs in for the first time, we want to move the PC to the right computer container.

The script works. I just need to know how to insert higher credentials so to allow the script to move an AD object,
0
 
LVL 56

Expert Comment

by:McKnife
ID: 35184989
0
 
LVL 21

Expert Comment

by:snusgubben
ID: 35185024
A domain user has only read access to the Computer object and OUs by default. If you want a domain user to be able to move objects, with a script, you'll need to grant them permissions. Not that I think that is a good idea.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 1000 total points
ID: 35186731
Hi, if you really want to put your username and password in the script, this will allow the current computer object to be moved to the target OU.

Regards,

Rob.
sADDomain = "yourdomain"
sADUser = "adminuser"
sADPassword = "adminpassword"

Const ADS_SECURE_AUTHENTICATION = 1
sDestOU = "LDAP://OU=targetOU,OU=sites,DC=domain,DC=com"
Set objRootDSE = GetObject("LDAP:")
Set objSysInfo = CreateObject("ADSystemInfo")
Set objDestOU = objRootDSE.OpenDSObject(sDestOU, sADDomain & "\" & sADUser, sADPassword, ADS_SECURE_AUTHENTICATION)
Set objComputer = objRootDSE.OpenDSObject("LDAP://" & objSysInfo.ComputerName, sADDomain & "\" & sADUser, sADPassword, ADS_SECURE_AUTHENTICATION)
On Error Resume Next
objDestOU.MoveHere "LDAP://" & objComputer.distinguishedName, vbNullString
If Err.Number = 0 Then
	MsgBox "Computer moved successfully."
Else
	MsgBox "Error " & Err.Number & ": " & Err.Description
	Err.Clear
End If

Open in new window

0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question