0
votes

I have to change all UPN in my Active Directory. Some UPN are completely empty an some are filled.

Now I want to fill them all with their sAMAccountName.

My script looks like this:

# Import AD Module
Import-Module ActiveDirectory            

# Import CSV into variable $userscsv            
#$userscsv = import-csv C:\temp\rename\usernames.csv            
$users = Import-Csv -Path C:\temp\rename\usernames.csv           
# Loop through CSV and update users if the exist in CVS file
foreach ($user in $users) {
    #Search in specified OU and Update existing attributes
    Get-ADUser -Filter "sAMAccountName -eq '$($user.samaccountname)'"
    Set-ADUser -userPrincipalName $($users.samaccountname)           
}

I get an errorcode:

Get-ADUser : Der Suchfilter wurde nicht erkannt
Bei C:\TEMP\RENAME\User-Rename.ps1:13 Zeichen:12
+  Get-ADUser <<<<  -Filter "sAMAccountName -eq '$($user.samaccountname)'"     
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : Der Suchfilter wurde nicht erkannt,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Does anyone have an idea?

1
how your csv file looks? add some lines of it... - Avshalom
Name,Benutzeranmeldename,E Mail-Adresse,Typ,Beschreibung,Anmeldename für Prä-Windows 2000 Amanu2 ext. Schreibdienst,[email protected], ,Benutzer,,amanu2 Ambulanz Leitung, , ,Benutzer,Ambulanz Leitung,Ambulanz Leitung - user2942606
EEG, ,[email protected],Benutzer,EEG,eeg - user2942606
where is your 'samaccountname' column? - Avshalom

1 Answers

0
votes

I would not recommend setting the UPN to the SamAccountName. That's not how it's supposed to be.

If you must do it anyway, you can't use subexpressions in a filter. Assign the value to a variable and use that in the filter expression. You also need to pipe the user object into Set-ADUser to give the cmdlet something to work with:

foreach ($user in $users) {
    $acct = $user.SamAccountName
    Get-ADUser -Filter "sAMAccountName -eq '$acct'" |
        Set-ADUser -userPrincipalName $acct
}