thegordo
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
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
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=compu ters,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=Deskt ops,cn=Net work Services,cn=Head Office,dc=uk,dc=x-y-z,dc=c om")
Else
If InStr(1, LCase(strSystemName), "HOLAP") Then
'Edit the LDAP path as needed
Set objDestOU = GetObject("LDAP://ou=Lapto ps,ou=Netw ork Services,ou=Head Office,dc=uk,dc=x-y-z,dc=c om")
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
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=compu
For Each objComputer In colComputers
strSystemName = Trim(Mid(objComputer.Name,
' WScript.Echo strsystemname
Set objDestOU = Nothing
If InStr(1, LCase(strSystemName), "HODESK") Then
'Edit the LDAP path as needed
Set objDestOU = GetObject("LDAP://cn=Deskt
Else
If InStr(1, LCase(strSystemName), "HOLAP") Then
'Edit the LDAP path as needed
Set objDestOU = GetObject("LDAP://ou=Lapto
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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=Membe r Servers,ou=Network Services,ou=Central,ou=Hea d Office,dc=uk,dc=x-y-z,dc=c om")
But it does not seem to be able to understand the space in Member Servers?
Can you help?
Thanks
G
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=Membe
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?
ASKER
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.
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?
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?
ASKER
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
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.
ASKER
Thanks for all of your help - much appreciated!
G.
G.
You're welcome!
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.c
For Each objComputer In colComputers
strSystemName = Trim(Mid(objComputer.Name,
Set objDestOU = Nothing
If InStr(1, LCase(strSystemName), "hodesk") Then
'Edit the LDAP path as needed
Set objDestOU = GetObject("LDAP://ou=Deskt
Else
If InStr(1, LCase(strSystemName), "holap") Then
'Edit the LDAP path as needed
Set objDestOU = GetObject("LDAP://ou=Lapto
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!