1
votes

I am creating a PowerShell script at work to copy user accounts. The script works perfectly on my test Server 2016 VM. It also works in our work environment on a coworkers Windows 10 PC, however I cannot run it on my local machine. It returns the following error:

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

I'm running PSVersion 5.1.150

Any ideas as to what i'm missing and why i'm coming across this error? I have no idea what it is referring to, nor why it works on one coworkers computer but not my own.


Edit: Value of $params at the time of the error:

Name                  Value
----                  -----
AccountPassword       System.Security.SecureString
Description           Chris Nelson Test Account
UserPrincipalName     cnelsontest1
HomePage              http://...
DisplayName           cnelson test1
SamAccountName        cnelsontest1
ScriptPath
EmailAddress          [email protected]
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

I'm calculating $New_Path like this:

$New_Path = (Get-ADUser ($UsernameCopy.Text)).DistinguishedName -replace '^.*?,', ''
1
Try adding the Name to your params 'Name' = $New_DisplayName; and then calling with just splatting: New-ADUser @paramsJames C.
try { New-ADUser ... } catch { $params }. Make sure all parameters have the value you expect them to have.Ansgar Wiechers
@JamesC. That shouldn't make a difference.Ansgar Wiechers
@AnsgarWiechers how exactly do i type that in to test your idea? I apologize if this is a silly question. Do I just surround the New-ADUser part in brackets and add catch { $params } at the end?cnelson
@cnelson Don't forget the try at the beginning.Ansgar Wiechers

1 Answers

0
votes

The way you remove the common name portion from the value of $AD_Account_To_Copy is flawed. -replace '^.*?,', '' will remove everything up to the first comma in the string. If the common name itself contains a comma (like in CN=test\, cnelson,OU=...) the replacement won't remove cnelson,. Amend your regular expression with a positive lookahead assertion, so that everything before the first OU= is removed:

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