Automate PC's, Laptop to correct OU

Kelly Garcia
Kelly Garcia used Ask the Experts™
on
Hi,

We have recently installed Quest Active Acroles and there is a way of automatically placing laptops and PCS to the correct OU's. Example we have laptops with the naming convention LP-LON-01, LP means it is a laptop and LON means it is based in London therefore this machine should be placed in the London laptop OU. How do we achieve this. Is there a script any one have in order to do this so that  we may then import this script as a module on active roles.

Thank you in advance.

kelly
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Joseph MoodyBlogger and wearer of all hats.

Commented:
You can use PowerShell to do this. Here is a guide that I put together a few years ago. It uses the Quest AD cmdlets but you can also use the native AD cmdlets if you wish:

https://deployhappiness.com/move-computer-to-ou-based-on-name-automatically/

Let me know if you have any issues.
Kelly GarciaSenior Systems Administrator

Author

Commented:
there is an onpostcreate script module on active roles, when I import that module it gives me

function onPostCreate($Request)
{



}

Open in new window


so my thoughts right now are:

function onPostCreate($Request)
{

if (Get-QADComputer $request| ? {$_.name -like 'lp-lon*'})
{
#move to London OU

}

}

Open in new window


not sure if this will work.

regards,
kay onpostcreate_1.JPGonpostcreate.JPG
PowerShell Developer
Top Expert 2010
Commented:
The $Request variable that's being passed is likely to contain some useful information. Having onPostCreate trigger a global move operation is a bad idea. To use onPostCreate you really need $Request to have a bit of information about the computer account. For example, if it had a "Name" property this might be used.
function onPostCreate($Request) {
    $adComputer = Get-QADComputer -Name $Request.Name
    if ($adComputer.DN -notlike '*London*') {
        Move-QADObject -Identity $adComputer.DN -NewParentContainer 'OU=London,OU=something,DC=domain,DC=com'
    }
}

Open in new window

The advantage of this is that it targets the one computer you're interested it and sorts it out. You'll likely need a bit more logic to deal with accounts that aren't in London of course.

Outside of the creation process, you can of course move things around too. This example searches for all computers using your name wildcard which are not already in an OU named London.
Get-QADComputer -Name 'lp-lon*' | Where-Object { $_.DN -notmatch 'OU=London' } | ForEach-Object {
    Move-QADObject -Identity $_.DN -NewParentContainer 'OU=London,OU=parent,DC=domain,DC=com'
}

Open in new window

If the original OU is not in the same OU tree as "London" the snippet can be made more efficient as it would be feasible to limit the search before hitting Where-Object.

Another approach is to only move computers created in the last x days. For example, this finds accounts created in the last 7 days.
Get-QADComputer -Name 'lp-lon*' -CreatedAfter (Get-Date).AddDays(-7) | Where-Object { $_.DN -notmatch 'OU=London' } | ForEach-Object {
    Move-QADObject -Identity $_.DN -NewParentContainer 'OU=London,OU=parent,DC=domain,DC=com'
}

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
Fantastic!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial