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
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 '^.*?,', ''
Name
to your params'Name' = $New_DisplayName;
and then calling with just splatting:New-ADUser @params
– James C.try { New-ADUser ... } catch { $params }
. Make sure all parameters have the value you expect them to have. – Ansgar WiechersNew-ADUser
part in brackets and addcatch { $params }
at the end? – cnelsontry
at the beginning. – Ansgar Wiechers