0

i've a problem with a script in Powershell...

I want to transfer users from an OU (parent OU : "IT" for exemple) to a "child OU" => "Users". ("IT">"Users").

Problem :

$name = "CN=Michael Allen,OU=IT,DC=Adatum,DC=com" 
Move-ADObject "CN=Michael Allen,OU=IT,DC=Adatum,DC=com" -TargetPath "OU=IT,OU=Users,DC=Adatum,DC=com"
Move-ADObject : The operation could not be performed because the object's parent is either 
uninstantiated or deleted
At line:5 char:1
+ Move-ADObject "CN=Michael Allen,OU=IT,DC=Adatum,DC=com" -TargetPath "OU=IT,OU=Us ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (CN=Michael Allen,OU=IT,DC=Adatum,DC=com:ADObject) [Move-AD 
   Object], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8329,Microsoft.ActiveDirectory.Management.Commands. 
   MoveADObject

My code :

$root = [ADSI]"LDAP://RootDSE"
$DOMAIN = $root.Get("rootDomainNamingContext")

#Informations serveur & domaine (Ex. : SERVER='LON-DC' / DOMAIN='DC=Adatum,DC=com')"
$SERVER = (Get-WmiObject Win32_ComputerSystem).Name
$root = [ADSI]"LDAP://RootDSE"
$DOMAIN = $root.Get("rootDomainNamingContext")

#Sous OU des OU parentes
$CHILD_OU1 = "Users"
$CHILD_OU2 = "Computers"
$CHILD_OU3 = "Groups"

#Pour chaque sous-OUs "Users", on récupère les utilisateurs des OU parent du fichier texte défini et on les transferts
foreach ($PARENT_OU in $OU_FILE) {
    #Récupération de la liste des utilisateurs de l'OU parente
    $users = Get-ADUser -Filter * -SearchBase "OU=$PARENT_OU,$DOMAIN"
    #Write-Host "Liste d'utilisateurs pour : $PARENT_OU : [$users]"

    #Pour chaque utilisateurs de la liste dans l'OU parente, on le transfert dans l'OU enfante "Users"
    foreach ($user in $users) {
    Write-Host "Utilisateur pour le transfert : $user"
    Write-Host "OU parente : $PARENT_OU"
    Write-Host "OU enfante : $CHILD_OU1"
    Move-ADObject -Identity "$user" -Server $SERVER -TargetPath "OU=$PARENT_OU,OU=$CHILD_OU1,$DOMAIN"
    }

}

I'm french and I try lot of time to do this script but I can't understand... Can somone help could help me please ?

Thanks.

2 Answers2

0

Normally this error would appear when the destination OU path can not be validated, does the 'Users' OU exists within the 'IT' OU already? Another debugging method is to create a temp user in your destination OU and compare its OU location to your destination to ensure they match exactly.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Klaseek
  • 11
  • 2
0

Thanks all for your answers ! But I found the problem who was so "really dumb of me"...

My Move-AdObject was :
Move-ADObject -Identity "$user" -Server $SERVER -TargetPath "OU=$PARENT_OU,OU=$CHILD_OU1,$DOMAIN"

And it was :

My Move-AdObject was : Move-ADObject -Identity "$user" -Server $SERVER -TargetPath "OU=$CHILD_OU1,OU=$PARENT_OU,$DOMAIN".

Thanks for your answers, I notice all details and it will help me.

Good continuation :)