Error: "New-ADUser : The object name has bad syntax"

Error: "New-ADUser : The object name has bad syntax"

我正在创建一个 PowerShell 脚本来复制用户帐户。该脚本在我的测试服务器 2016 VM 上完美运行。它也适用于我们同事 Windows 10 台 PC 上的工作环境,但我不能 运行 在我的本地计算机上使用它。它 returns 出现以下错误:

New-ADUser : The object name has bad syntax
At line:155 char:1
+ New-ADUser -Name $New_DisplayName @params
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (CN=cnelson test...ctions,DC=local:String) [New-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.NewADUser

Add-ADGroupMember : Cannot find an object with identity: 'cnelsontest1' under:
'DC=,DC=local'.
At line:159 char:29
+ Add-ADGroupMember -Members $Username.Text
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (cnelsontest1:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
    + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
$params = @{'SamAccountName' = $Username.Text;
            'Instance' = $AD_Account_To_Copy;
            'DisplayName' = $New_DisplayName;
            'GivenName' = $FirstName.Text;
            'Path' = $New_Path;
            'SurName' = $LastName.Text;
            'ChangePasswordAtLogon' = $true;
            'Enabled' = $true;
            'UserPrincipalName' = $Username.Text;
            'AccountPassword' = $New_Pass;
            'EmailAddress' = $Username.Text + '@azcorrections.gov';
            'HomePage' = $HomePage.HomePage;
            'Description' = $NewDescription.Description;
            'Office' = $NewOffice.Office;
            'StreetAddress' = $NewStreet.StreetAddress;
            'City' = $NewCity.City;
            'State' = $NewState.State;
            'PostalCode' = $NewPostalCode.PostalCode;
            'Title' = $NewTitle.Title;
            'Department' = $NewDepartment.Department;
            'Company' = $NewCompany.Company;
            'ScriptPath' = $NewScript.ScriptPath;
            'OfficePhone' = $PhoneNumber.text;
            }

New-ADUser -Name $New_DisplayName @params

Full Script link

我运行正在使用 PSVersion 5.1.150

关于我遗漏了什么以及为什么我会遇到此错误的任何想法?我不知道它指的是什么,也不知道为什么它可以在一台同事的电脑上运行,但不能在我自己的电脑上运行。


编辑: $params 错误时的值:

Name                  Value
----                  -----
AccountPassword       System.Security.SecureString
Description           Chris Nelson Test Account
UserPrincipalName     cnelsontest1
HomePage              http://...
DisplayName           cnelson test1
SamAccountName        cnelsontest1
ScriptPath
EmailAddress          cnelsontest1@example.com
Office                test
GivenName             cnelson
Title                 SYSTEMS/LAN ADMR
Company
OfficePhone           555-1234
StreetAddress         Sesame Street
PostalCode            54321
SurName               test1
State                 candid
Department            IT
ChangePasswordAtLogon True
Path                   cnelson,OU=IT_TECHSRVS,OU=Information Technology,OU=ADMIN,OU=CENT_OFF,DC=example,DC=com
City                  
Enabled               True
Instance              CN=test\, cnelson,OU=IT_TECHSRVS,OU=Information Technology,OU=ADMIN,OU=CENT_OFF,DC=example,DC=com

我正在这样计算 $New_Path

$New_Path = (Get-ADUser ($UsernameCopy.Text)).DistinguishedName -replace '^.*?,', ''

您从 $AD_Account_To_Copy 的值中删除通用名称部分的方式存在缺陷。 -replace '^.*?,', '' 将删除字符串中第一个逗号之前的所有内容。如果公用名本身包含逗号(如 CN=test\, cnelson,OU=...),则替换不会删除 cnelson,。用积极的先行断言修改您的正则表达式,以便删除第一个 OU= 之前的所有内容:

$New_Path = $AD_Account_To_Copy -replace '^.*?,\s*(?=ou=)', ''