Link to home
Start Free TrialLog in
Avatar of thegordo
thegordoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ADSI script to move computer accounts from default Computers OU to custom OUs

Hi Experts

I would like a vbscript which I can run once a month which will move the computer accounts in AD from the Computers OU to our custom OUs depending on what is in the PC's name.

When our PCs are built their computer accounts is automatically created in the Computers OU. I need to move these into custom OUs... At the moment I do this manually.

Our custom OU structure looks similar to the following:

Group Office
      > Network
             >Desktops
             >Laptops

Our computers are named like this:

Desktops HODesk00x
Laptops  HOLap00x

Obviously the script would be able to look for all PCs with Desk in the name and move them from the default OU to the Desktops OU and so on....

Thanks

G
Avatar of David Lee
David Lee
Flag of United States of America image

Hi thegordo,

This should get the job done.  I tested the concept in my domain and it worked properly.

Dim colComputers, objComputer, objDestOU
'Edit the LDAP path as needed
Set colComputers = GetObject("LDAP://server.company.com/cn=computers,dc=company,dc=com")
For Each objComputer In colComputers
    strSystemName = Trim(Mid(objComputer.Name, 4))
    Set objDestOU = Nothing
    If InStr(1, LCase(strSystemName), "hodesk") Then
        'Edit the LDAP path as needed
        Set objDestOU = GetObject("LDAP://ou=Desktops,ou=Network,ou=Group Office,dc=company,dc=com")
    Else
        If InStr(1, LCase(strSystemName), "holap") Then
            'Edit the LDAP path as needed
            Set objDestOU = GetObject("LDAP://ou=Laptops,ou=Network,ou=Group Office,dc=company,dc=com")
        End If
    End If
    If Not IsNothing(objDestOU) Then
        objDestOU.Movehere objComputer.ADsPath, vbNullString
    End If
Next
Set objComputer = Nothing
Set colComputers = Nothing

Function IsNothing(Obj)
  If TypeName(Obj) = "Nothing" Then
    IsNothing = True
  Else
    IsNothing = False
  End If
End Function

Cheers!
Avatar of thegordo

ASKER

Hi BlueDevilFan

Thanks for the quick response...

I have tried this script but to no avail - I am running the script with a domain admin account so I have adequate privilege...

The script runs without error but the computers remain...

Here is my script...

'*** script start ***

Dim colComputers, objComputer, objDestOU
'Edit the LDAP path as needed
Set colComputers = GetObject("LDAP://cn=computers,dc=uk,dc=x-y-z,dc=com")
For Each objComputer In colComputers
    strSystemName = Trim(Mid(objComputer.Name, 4))
   ' WScript.Echo strsystemname
    Set objDestOU = Nothing
    If InStr(1, LCase(strSystemName), "HODESK") Then
        'Edit the LDAP path as needed
        Set objDestOU = GetObject("LDAP://cn=Desktops,cn=Network Services,cn=Head Office,dc=uk,dc=x-y-z,dc=com")
    Else
        If InStr(1, LCase(strSystemName), "HOLAP") Then
            'Edit the LDAP path as needed
            Set objDestOU = GetObject("LDAP://ou=Laptops,ou=Network Services,ou=Head Office,dc=uk,dc=x-y-z,dc=com")
        End If
    End If
    If Not IsNothing(objDestOU) Then
        objDestOU.Movehere objComputer.ADsPath, vbNullString
    End If
Next
Set objComputer = Nothing
Set colComputers = Nothing

Function IsNothing(Obj)
  If TypeName(Obj) = "Nothing" Then
    IsNothing = True
  Else
    IsNothing = False
  End If
End Function

'*** script end ***

Any ideas?

G

ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi BDFan,

That's great! Works fine! I have also managed to modify it for pc names with other naming formats....

Just one last thing - I am trying to add another couple of lines -

 If InStr(1, UCase(strSystemName), "APP") Then
           Set objDestOU = GetObject("LDAP://ou=Member Servers,ou=Network Services,ou=Central,ou=Head Office,dc=uk,dc=x-y-z,dc=com")

But it does not seem to be able to understand the space in Member Servers?

Can you help?

Thanks

G
I don't think that's it.  I have scripts that access OUs with a space in their name.  Are you sure about the LDAP path?
Hi there

The canonical name of the object is:

uk.x-y-z.com/Head Office/Central/Network Services/Member Servers

The script does work for the other OUs - Desktops and Laptops...

Their paths are:

uk.x-y-z.com/Head Office/Central/Network Services/Laptops etc etc...



G.
Hi, thegordo.

I can't reproduce the problem in my domain.  I have OUs in the same position as your "Member Servers" OU that have a space in their name and GetObject works fine with them.  Are you certain the LDAP path is correct?
Hi BDF,

It seems to just be a problem with that OU...

It has nothing to do with spaces as I have tested it with another OU with a space in the name and it works fine...

Might just have to leave it....

G
Strange, but at least you've identified the problem.
Thanks for all of your help - much appreciated!

G.
You're welcome!