我的目标是复制一个现有的用户,包括AD树中的OU位置。
到目前为止,我有这样的:
$SName = Read-Host "Please Enter the login initials of the source user " $U = Get-ADUser -Identity $SName -Properties streetaddress,homepage,POBox,postalcode,city,country,company,department,office,fax $FirstName = read-host -Prompt "Enter first name of new user: " $LastName = read-host -Prompt "Enter last name of new user: " $DisplayName = "$Firstname $LastName" $DName = Read-Host "Please Enter logon initials of the new user " $UPN = $DName+"@scangl.com" $TempPassword = read-host -AsSecureString -Prompt "Enter temporary password (min. 8 characters)" $Path = Get-User -Identity $SName|Select "OrganizationalUnit" New-ADUser -Instance $u -Name $DisplayName -SamAccountName $DName -Path $Path -UserPrincipalName $UPN -DisplayName $DisplayName -givenname $FirstName -surname $LastName -AccountPassword $TempPassword -ChangePasswordAtLogon $True
您可能会注意到,我试图使用Get-UserselectOrganizationalUnit,但它给了我错误:没有上级引用已经configuration为目录服务。 目录服务因此无法发布引用到此目录林以外的对象。
任何build议如何复制OU的位置,或者可能是它是根本无法复制此用户属性?
对象所在的组织单位(或容器)不是对象的属性,所以不能轻易读取; 但是可以通过parsing对象的可分辨名称(以第一个逗号分割)来检索:
$DN = $U.DistinguishedName $Path = $DN.Substring($dn.IndexOf(',') + 1) New-ADUser [...] -Path $Path
Get-User cmdlet不是活动目录,而是来自Exchange命令行pipe理程序; 其OrganizationalUnit属性不是有效的LDAPpath,所以不能在New-ADUser命令中使用。
如果对象的DN是CN=User Name,OU=SomeOU,DC=domain,DC=com ,我的代码将返回OU=SomeOU,DC=domain,DC=com (这是OU的实际LDAPpath)而Get-User.OrganizationalUnit会返回一个完全不同的东西domain.com/SomeOU 。
复制用户,然后从创build副本的默认OU移到所需的副本。 就那么简单。 像Massimo指出的那样,一个对象所在的OU /容器不是对象的一部分。
如果你认为这是一个文件操作,你可能会变得更好。 用户就像一个文件,OU或一个容器就像一个文件夹 – 你不会将文件夹添加到文件中,而是将文件复制到你想要驻留的位置。 与AD一样,但在Windows域中,所有对象types都有一个默认的容器/ OU,其中自动创build了该types的新对象。