大家好我正在使用以下代码将用户添加到活动目录但我收到错误 找不到目录对象
$NewUser = Read-Host "New Username"
$firstname = Read-Host "First Name"
$Lastname = Read-Host "Last Name"
$NewName = "$firstname $lastname"
New-ADUser -SamAccountName $NewUser -Name $NewName -GivenName $firstname -Surname
$lastname -Path "ou=Users,DC=mydomain,DC=local" -AccountPassword (Read-Host "New Password"
-AsSecureString)
答案 0 :(得分:0)
我觉得你没有真正查看Get-Help New-ADUser -Full
,因为它指定了(请注意第二个要点):
-Path <string> Specifies the X.500 path of the Organizational Unit (OU) or container where the new object is created. In many cases, a default value will be used for the Path parameter if no value is specified. The rules for determining the default value are given below. Note that rules listed first are evaluated first and once a default value can be determined, no further rules will be evaluated. In AD DS environments, a default value for Path will be set in the following cases: - If the cmdlet is run from an Active Directory PowerShell provider drive, the parameter is set to the current path of the provider drive. - If the cmdlet has a default path, this will be used. For example: in New-ADUser, the Path parameter would default to the Users container. - If none of the previous cases apply, the default value of Path will be set to the default partition or naming context of the target domain.
因此它应默认为您的Users OU,而您实际上并不需要指定它。此外,我认为您在使用它的上下文中需要-DisplayName而不是-Name,并且可能在该上下文中使用-Name而不是-SamAccountName。试试这个:
New-ADUser -Name $NewUser -DisplayName $NewName -GivenName $firstname -Surname $lastname -AccountPassword (Read-Host "New Password" -AsSecureString)
请注意,默认情况下,此帐户将被创建为已禁用。将-Enabled $true
添加到您的命令中,以使其创建启用的帐户。这确实需要提供有效的密码。如果密码无效,仍会创建该帐户,但密码没有密码,也不会启用。