I'm trying to copy all users of OU "A" to the OU "B". My PowerShell shot at this is
$sourceEntry = [ADSI]"LDAP://OU=A,DC=demo,DC=com"
$targetEntry = [ADSI]"LDAP://OU=B,DC=demo,DC=com"
$searcher = New-Object DirectoryServices.DirectorySearcher($sourceEntry)
$searcher.Filter = "(objectClass=user)"
$results = $searcher.FindAll()
foreach($result in $results) {
$user = $result.GetDirectoryEntry()
$user.CopyTo($targetEntry)
}
My problem is, that $user appears to lack the CopyTo method I try to call. As far as I understand PowerShell, $user is an .NET object of the type System.DirectoryServices.DirectoryEntry ... in Visual Studio I find the method CopyTo ... in PowerShell I find none of it's methods, just properties.
I'm just starting with PowerShell, so please help!
-
You can't copy AD users.
You can move them from one place to another, or you can create new users based on existing ones... but in the latter case, you have to supply new user names, passwords and a few other things; it's not as simple as a "copy & paste" operation.
Users are security principals, they must be unique in a given domain; you can't have two "identical" user objects in different OUs.
mfinni : Correct and Yup. In NDS (Novell Directory Services) you can have the same username in multiple OUs. Not in AD.Hinek : Ok, but the MoveTo method isn't accessible either ...From Massimo -
According to this StackOverflow question, you should use the
PSBasemember of aDirectoryEntryobject in order to access all of its methods. Try this:foreach($result in $results) { $user = $result.GetDirectoryEntry() $user.PSBase.MoveTo($targetEntry) }Hinek : Ok, thanks, this works. Together with your first answer (You can't copy AD users) this is the answer I searched for.From Massimo
0 comments:
Post a Comment