Link to home
Start Free TrialLog in
Avatar of Mohammed Hamada
Mohammed HamadaFlag for Portugal

asked on

Create a script to move users to their pertaining AD group based on their province

Hello All,

I am trying to create a script that will add users to their pertaining group based on their province Attribute. so if the user's province attribute says "London" the script will move the user to their pertaining Organization unit.

I did the script and it worked but I want to do a generic one that will work

Get-ADuser -SearchBase "DC=Opera,DC=com" -properties * -Filter * | Where-Object {$_.st -like "London"}  | Move-ADObject -TargetPath "OU=London,OU=UK,OU=DOB USER,DC=Opera,DC=com"

Open in new window


I would like to amend the script so that it would automatically search all the users on AD and look for the province and once it reads that it'll move the user to his City Organization Unit.

I'd appreciate any thoughts.

Thanks
Avatar of Bradley Fox
Bradley Fox
Flag of United States of America image

This assumes you have an OU for each providence that matches the AD attribute of all users in your search base.

Get-ADuser -SearchBase "DC=Opera,DC=com" -properties * -Filter * | ForEach-Object {Move-ADObject -TargetPath "OU=$($_.st),OU=UK,OU=DOB USER,DC=Opera,DC=com"}

Open in new window

Avatar of Mohammed Hamada

ASKER

Hi Mcsween, that didn't work it asked for the identity. I was trying to use where-object instead for-each but that didn't work either however it didn't give me any error .

Get-ADuser -SearchBase "DC=opera,DC=com" -properties * -Filter * | Where-Object "$OU={$_.st}" | ForEach-Object {Move-ADObject -TargetPath "OU=$OU,DC=opera,DC=com"}

Open in new window

I am sure I can do this with If statement but my problem is that I am not good yet with scripting.. I would say If user's State is Bavaria and province is Munich then move user to OU=Munich,OU=Bavaria..etc

Else if user's state is London and his province is Greater Manchester then move user to OU=Greater Manchester, OU=London"
ASKER CERTIFIED SOLUTION
Avatar of Bradley Fox
Bradley Fox
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
Thanks a lot