0
votes

I want to create a new AD user but it is showing error message like:

New-ADUser : The object name has bad syntax At C:\Users\sa\Desktop\AD User Script.ps1:22 char:1 + New-ADUser -Name "$displayName" -UserPrincipalName "($initials) ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (CN=fbfb regbgfn...IT,DC=,DC=it:String) [New-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.NewADUser

$firstName = Read-Host "Indtast dit fornavn"            
$middlename = Read-Host "Indtast dit mellemnavn (Hvis du ikke har et tryk Enter)"            
$surname = Read-Host "Indtast dit efternavn"      
$PlainPassword = "Admin100"      
$SecurePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force              
$group = Read-Host "Hvilken Gruppe? [1 - Help] [2 - Sof] [3 - In]"            
$firstletter1 = $firstname.Substring(0, 1) 
$secondletter = $firstname.Substring(0, 2)          
$firstletter2 = $middlename.Substring(0, 1)            
$firstletter3 = $surname.Substring(0, 1)
$displayName = "$firstName $middlename$surname"
if ($middlename -eq $Null) {
    $initials = "$firstletter1$secondletter$firstletter3".ToLower() 
    Write-Host "$initials"  
}
else {
    $initials = "$firstletter1$firstletter2$firstletter3".ToLower()
    Write-Host "$initials"  
}        
$Searcher = [ADSISearcher]"(sAMAccountName=$initials)"            
$Results = $Searcher.FindOne()            
If ($Results -eq $Null) {        
    If ($group -eq 1) {  
        New-ADUser -Name "$displayName" -UserPrincipalName "($initials)" -Path "OU=,OU=,OU-,OU=,DC=,DC=" -Enabled $true -AccountPassword $SecurePassword -ChangePasswordAtLogon $True -DisplayName "$initials" -GivenName "$firstname" -HomeDrive "P: \\fileshare\Privat\%$initials%" -Initials "$initials" -SamAccountName "$firstletter1" -Surname "$surname"   

    }
1
Use Write-Host to print the command and double-check that the arguments are parsed properly.vonPryz

1 Answers

0
votes

Can you check the UserPrincipalName and sAMAccountName formats. An example would be:

Name: John Smith

UPN: [email protected]

sAMAccountName : smithj

UPN Format

A UPN consists of a UPN prefix (the user account name) and a UPN suffix (a DNS domain name). The prefix is joined with the suffix using the "@" symbol. For example, "someone@ example.com". A UPN must be unique among all security principal objects within a directory forest. This means the prefix of a UPN can be reused, just not with the same suffix.