Monday, April 15, 2013

Move user between domain's

Move user between domain's in the same forest PowerShell


This week I try to write PowerShell script for moving users between domain's in the same forest.At first I try to use ADMT 3.2 ,in my test environment the ADMT install on a member server.After several testing I try the RTFM approach (read the faking Manuel).From this approach I learn that if you want to script ADMT it must be install on a Domain Controller!

So I start google to find alternative then I found a Book from  O'Reilly
"Active Directory CookBook" Written byLaura E. Hunter‏,Robbie Allen‏
in the book was explain in VB for moving users between domain in the same forest.



 

 

 

 

 








Below is the Code in PowerShell that do the work enjoy


100 function GetDomainnamefromobjectDN([string]$objDN){
101      $str = $objDN.Split(",")
102      for ($i=$str.length-1 ;$i -gt -1 ;$i--)
103         {if ($str[$i].substring(0,2) -like "dc")
104            {if($strDC)
105               {$strDC = $str[$i].Split("=")[1] + "." + $strDC}
106            else
107               {$strDC = $str[$i].Split("=")[1]}
108            }
109         }
110         return $strDC
111      }
112
113 $Tou = "CN=Users,DC=London,DC=England,DC=CO,DC=UK"
114 $usrDN = "CN=Doron Zilber,CN=Users,DC=England,DC=CO,DC=UK"
115 $TargetRID = $null
116 $SourceRID = $null
117 $objForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
118 $DomainList = @($objForest.Domains | Select-Object Name,RidRoleOwner)
119
120 foreach ($dom in $DomainList)
121      {if ($dom.name -eq (GetDomainnamefromobjectDN($Tou)))
122         {$TargetRID = $dom.RidRoleOwner}
123      if ($dom.name -eq (GetDomainnamefromobjectDN($usrDN)))
124         {$SourceRID = $dom.RidRoleOwner}
125      if ($TargetRID -and $SourceRID)
127         {break}
129      }
130
131 $objUsr = [ADSI]"LDAP://$SourceRID/$usrDN"
132 $MoveToOU = [ADSI]"LDAP://$TargetRID/$Tou"
133
134 # ## Command to Do the actual move
135 $objUsr.PSBase.moveto($MoveToOU)
136





No comments:

Post a Comment