Automate PC's, Laptop to correct OU

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
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
0
Kelly GarciaSenior Systems AdministratorAuthor 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
0
Chris DentPowerShell DeveloperCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelly GarciaSenior Systems AdministratorAuthor Commented:
Fantastic!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.