错误:" New-ADUser:对象名称语法错误"

时间:2017-11-20 14:28:46

标签: powershell active-directory powershell-v5.0

我正在创建一个PowerShell脚本来复制用户帐户。该脚本在我的测试服务器2016 VM上完美运行。它也可以在我们的同事Windows 10 PC的工作环境中工作,但我无法在本地计算机上运行它。它返回以下错误:

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 '^.*?,', ''

1 个答案:

答案 0 :(得分:0)

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

 SET VDATEI=(SELECT A.DATEI FROM STAFF_CONVENIOS A WHERE 
   A.CONV_ID=CONVE AND A.STAFF_ID=MED LIMIT 1);