VBS Scripting to move computers from one OU to the next

1.5k Views Asked by At

I am working on packaging a script for the company that I work for that will allow field service techs to convert computers in a private workstation OU to a team workstation OU and vice versa.

That is a small part of this script for now and one that has had me puzzled for most of the day. I've tried different variations of this script and landed on one that I believe will get me on the right track.

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Set colItems = objWMIService.ExecQuery("Select Name from Win32_ComputerSystem",,48) 
For Each objItem in colItems 
strPCName = objItem.Name 
Next 
Set objNewOU = GetObject("LDAP://OU=Computers,OU=Corporate,DC=xxxxx,DC=net") 
Set objMoveComputer = objNewOU.MoveHere("LDAP://CN=" & strPCName & ",OU=Computers,OU=Corporate,DC=xxxxx,DC=net",vbnullstring) 

I get an error, There is no such object on the server. When I put the computer manually in the OU in question, I don't get that error message. This is where I'm stuck at the moment.

The scripting is in my personal lab at the moment.

1

There are 1 best solutions below

0
On

I was able to get a solution that did work, by using the following script.

Set objSysInfo = CreateObject("ADSystemInfo") 
strComputerDN = objSysInfo.ComputerName

Set objNewOU = GetObject("LDAP://OU=Private Workstations,OU=xxxx,OU=xxxx,DC=xxxx,DC=xxxx") 
Set objMoveComputer = objNewOU.MoveHere _ ("LDAP://" & strComputerDN, vbNullString)

Judging by the script's behavior, it uses strComputerDN to determine where the computer is, and the objNewOU determines where the computer is going. The objmoveComputer consolidates this information as best as I can determine to move the computer to it's OU.