Link to home
Start Free TrialLog in
Avatar of tamray_tech
tamray_tech

asked on

Assistance updating user attributes in AD

I have about 1500 users i need to modify attributes for, so we can merge them with Office 365. I want to use the powershell to do this. I need assistance creating the proper csv file and help with commands to run in order to update the following:


MailNickName = user@domain.com
proxyAddresses = user@domain.com
UserPrincipalName= user@domain.com


I am not sure I have the proper field names, so feel free to correct.

I would like to experiment with a single sub OU before applying it to all users.
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

I have created a script below to do what you are looking for see below...
Note: Construct the CSV with 3 headings NickName, ProxyAddress and UPN. Have all of your entries below these headings. Also, if you are running this from the EMS you can remove the Import-module and Add-PSSnapin lines.
Import-Module activedirectory
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin
$UserSet = Import-Csv "c:\youCSVhere.csv"
foreach ($User in $UserSet) {
$User.NickName
$User.ProxyAddress
$User.UPN
Get-Mailbox -OrganizationalUnit "OU=test,DC=YourDomain,DC=com" | Set-Mailbox -Alias $User.NickName -EmailAddresses $User.ProxyAddress -UserPrincipalName $User.UPN
}

Open in new window


Thanks

Will.
Avatar of tamray_tech
tamray_tech

ASKER

Do I name the script with a specific extension, or run line by line?
Is this how the csv file should look:

NickName, ProxyAddress, UPN
user1
user2
user3
copy and paste the script into a txt file. From there change the file extension from txt to .ps1. That is correct CSV file should have the 3 headings you have illustrated with all of the values you want to update.

The last line of code needs get-mailbox -OrganizationalUnit you will need to specify the path to your testing OU. so you will also need to replace that info with yours as well.
I get the following error when running the script:

The Windows PowerShell snap-in 'Microsoft.Exchange.Management.Powershell.Admin' is not installed on this machine.
Are you running this command on the Exchange Server using the Exchange Management Shell? If you are you do not need this. Based on the error it appears you are running this from a server does not not have Exchange installed or does not have the Exchange Tools installed either?

You need to have the Exchange Management Tools installed to run this script on a server/workstation other than the actual Exchange server.
I am running it from a 2010 Exchange server powershell
If you are running it from the Exchange Server you can remove that line from the script. But based on the error message it looks the same as if you were running this command from a workstation/server without Exchange Tools installed.

So it should look like this...
$UserSet = Import-Csv "c:\youCSVhere.csv"
foreach ($User in $UserSet) {
$User.NickName
$User.ProxyAddress
$User.UPN
Get-Mailbox -OrganizationalUnit "OU=test,DC=YourDomain,DC=com" | Set-Mailbox -Alias $User.NickName -EmailAddresses $User.ProxyAddress -UserPrincipalName $User.UPN
} 

Open in new window


Do the following...
- Save the command to a txt file
- rename the file extension from txt to ps1
- Open the EMS
- type: .\scriptname.ps1 (type your script name in replace of what i have got)
I found the answer:

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

However, my needs are different than I first thought. The script errors out with the following:
PS C:\Users\administrator.domain> C:\updateuser.ps1
AWright95
Couldn't find organizational unit "OU=2013,OU=Students,DC=domain,DC=k12,dc=mn, dc=us". Make sure you have typed the name c
orrectly.
At C:\updateuser.ps1:6 char:1
+  <<<< Get-Mailbox -OrganizationalUnit "OU=2013,OU=Students,DC=domain,DC=k12,dc=mn, dc=us" | Set-Mailbox -Alias $User.Nic
kName -EmailAddresses $User.ProxyAddress -UserPrincipalName $User.UPN
    + CategoryInfo          : NotSpecified: (:) [], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : 784C9312

The script info is correct. Properties from the OU are as follows:
OU=2013,OU=Students,DC=domain,DC=k12,DC=mn,DC=us

Another issue is that the logon name field is blank. However the pre 2000 logon name is there. That is the info I need to show up in the user logon field, along with the other 2 updates.
The Logon name should be the UPN (Universal Principal Name user@domain.com). This is part of the CSV file that you need to fill in, to update the entries for each user in Exchange/ActiveDirectory. As for the OU you have the correct syntax OU=test,DC=domain,DC=com. Make sure that you have the correct path for the OU in question.
OK... It must have been a space in the script that caused the problem. The script now runs without errors, with the user file as is:

NickName,ProxyAddress,UPN
AWright95

However, none of the attributes are updated.
Did you add all of the info in the CSV file? You need to have all of the info in the CSV file. The script reads the CSV file and update each user in Active Directory with the data. If you dont put anything in there nothing will update.
Maybe I misunderstood your instructions.  I posted what I had in my csv file.

Here it is again:

NickName,ProxyAddress,UPN
AWright95 (single user)
You need to add all of the information to the CSV file that you want updated in Active Directory. See below example...

NickName      ProxyAddress                UPN
User1                 User1@domain.com      User1@domain.com
User2                 User2@domain.com      User2@domain.com
User3                 User3@domain.com      User3@domain.com

ProxyAddress is a secondary address that the user can receive mail on. This does not need to be user1@domain.com it can be alias1@domain.com as long as this address is not in use you can make it whatever you want.
I just opened a new question, thinking I would need the username  in order to update the necessary attributes. I am attaching the screen shot here, so you can see what I need to accomplish.

I did misunderstand your solution. Thank you for clarifying. I will be able to complete the task, once I am able to export the logon names.
Screenshot-7.png
To export the logon names is pretty easy. User the below powershell command to get the logon names (sAMAccountName)...

This will get all of the sAMAccountName's for all users in a specific OU.
Get-Aduser -Filter * -Searchbase "OU=test,DC=domain,DC=com" -Properties * | select samaccountname | export-csv "c:\samaccountexport.csv"
I successfully created the csv file, but attributes are not updated when running updateuser.ps1.

Here is what I have:

2012Fsamaccountexport.csv:

 NickName      ProxyAddress      UPN                  
RHanson93      RHanson93@domain.com      RHanson93@domain.com                  
HHawkenson93      HHawkenson93@domain.com      HHawkenson93@domain.com                  
CHedman94      CHedman94@domain.com      CHedman94@domain.com                  
JHendrickso94      JHendrickso94@domain.com      JHendrickso94@domain.com      

updateuser.ps1:
$UserSet = Import-Csv "c:\2012Fsamaccountexport.csv"
foreach ($User in $UserSet) {
$User.NickName
$User.ProxyAddress
$User.UPN
Get-Mailbox -OrganizationalUnit "OU=2012,OU=Students,DC=domain,DC=k12,DC=mn,DC=us" | Set-Mailbox -Alias $User.NickName -EmailAddresses $User.ProxyAddress -UserPrincipalName $User.UPN
}
The script is pointing to a specific OU. If those users listed in the CSV file are not in the OU you are searching then nothing will be updated. Does this make sense?

If you do not want to specify a specific OU just remove the OU switch entirely.

$UserSet = Import-Csv "c:\2012Fsamaccountexport.csv"
foreach ($User in $UserSet) {
$User.NickName
$User.ProxyAddress
$User.UPN
Get-Mailbox | Set-Mailbox -Alias $User.NickName -EmailAddresses $User.ProxyAddress -UserPrincipalName $User.UPN
}
I went with your most recent script.

The csv file contains a single user:

NickName ProxyAddress UPN                  
RHanson93 RHanson93@domain.com RHanson93@domain.com      


I started with a new powershell instance:

PS C:\Users\administrator.NLS> Import-Module activedirectory
PS C:\Users\administrator.NLS> add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
PS C:\Users\administrator.NLS> C:\updateuser.ps1


Results: I get many instances of this error:

Set-Mailbox : The property can't be empty. Property Name: Alias
At C:\updateuser.ps1:6 char:26
+ Get-Mailbox | Set-Mailbox <<<<  -Alias $User.NickName -EmailAddresses $User.ProxyAddress -UserPrincipalName $User.U
    + CategoryInfo          : NotSpecified: (0:Int32) [Set-Mailbox], DataValidationException
    + FullyQualifiedErrorId : 5941D35C,Microsoft.Exchange.Management.RecipientTasks.SetMailbox
Try this...

$UserSet = Import-Csv "c:\2012Fsamaccountexport.csv"
foreach ($User in $UserSet) {
$User.NickName
$User.ProxyAddress
$User.UPN
Get-Mailbox -idendity $User.UPN | Set-Mailbox -Alias $User.NickName -EmailAddresses $User.ProxyAddress -UserPrincipalName $User.UPN
}

Because the OU has been removed we need to specify the users you want to modify. I have added the -identity switch in the command above try that.


CSV file should still represent what you see below...
NickName                  ProxyAddress                        UPN                  
RHanson93             RHanson93@domain.com      RHanson93@domain.com                  
HHawkenson93     HHawkenson93@domain.com HHawkenson93@domain.com                  
CHedman94            CHedman94@domain.com      CHedman94@domain.com                  
JHendrickso94      JHendrickso94@domain.com      JHendrickso94@domain.com
I ran it exactly as you have listed, but corrected idendity to identity.

Here are the results:


Set-Mailbox : The property can't be empty. Property Name: Alias
At C:\updateuser.ps1:6 char:46
+ Get-Mailbox -identity $User.UPN | Set-Mailbox <<<<  -Alias $User.NickName -EmailAddresses $User.ProxyAddress -UserPri
ncipalName $User.UPN
    + CategoryInfo          : NotSpecified: (310:Int32) [Set-Mailbox], DataValidationException
    + FullyQualifiedErrorId : 5941D35C,Microsoft.Exchange.Management.RecipientTasks.SetMailbox
Ok lets try doing a 1 command to ensure that it works. Once that is confirmed we can check out the script. Use the command below for 1 user.

Get-mailbox -identity RHanson93@domain.com | set-mailbox -Alisa RHanson93 -EmailAddreses RHanson93@emaildomain.com -UserPrincipalName RHanson93@domain.com

Let me know what the results are.
Object cannot be found....

I think the issue is that the user does not actually have a current mailbox, so that is why things are not working.

The school originally did not want to provide email accounts to students, so that info is empty in their account settings.
Rofl, that might be why!!!! I was starting to wonder why it was not working when I have had it working on my end. The attributes you want to update are exchange attributes so unless the users have a mailbox the above commands will not work as you have experienced.


Cheers
Correct.. The attributes needed are for Exchange, specifically for Office 365, as I covered when I opened the question. Is there another variable we can use to query the user, so these attributes can be added. I need each account to be configured with these variables in order to merge them with existing Office 365 accounts. Without this, the accounts will be added, rather than merged.
I am assuming that you want to use AD attributes for this considering the accounts do not have an Exchange mailbox currently. I am not sure what attributes you require for this but if you do the following command below it will provide a list of AD attributes for a user without a mailbox...

get-aduser -identity "sAMAccountName" -Properties * | get-member

This will populate a list of attributes you can update/modify with a script. Only select the attribute MemberType that are "Property" value.

Once you know that information I can correct the script above to achieve this.
All of the users that need to be modified are part of the "Students" group. Would it work to use that membership against specific OUs to get the needed updates?
It does not matter the location (OU) or Group they are part of. I only need to know what attributes need to be modified to existing users in your environment. The location of the users can be done later. If we need to modify attributes to match the office 365 mailboxes this is more important.
The attributes I opened the question with are the same I still need created for each account, per Microsoft.

Maybe it would be just as simple to bulk create the missing email accounts and run your script again??
Well are the requirements Per Microsoft to have the mailboxes created prior to Office 365 migration? If this is the case then you need to create them. ProxyAddresses and MailNickName are Exchange attributes, so if you are looking to update those field i would assume that the mailboxes should already be present.
I think, like you, they just assume the mailboxes would be there. I only know I have to have those attributes populated in order for the merge to work
Then the only thing you can do is create new mailboxes for the accounts that do not already have one. If you can confirm this I can assist with the script.
That is  what I think we need to do.
Script to enabled mailboxes of Currently created AD users.

CSV file needed for this looks like below...

UserPrincipalName         Database
user1@domain.com        ServerName\StorageGroup\DatabaseName
user2@domain.com        ServerName\StorageGroup\DatabaseName
So on....

Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin
$UserList = Import-Csv "c:\yourCSVhere.csv"
$UserList = foreach ($Mailbox in $UserList) {
$Mailbox.UserPrincipalName
$Mailbox.Database
Enable-mailbox -Identity $Mailbox.UserPrincipalName -Database $Mailbox.Database
}

Open in new window


Hopefully this is what you are looking for...
I believe so.. I will work on this today. Thank you for your help and patience. Sorry for the confusion.
I'm getting the following error:

Enable-Mailbox : Cannot bind argument to parameter 'Identity' because it is null.
At C:\updateuser2.ps1:6 char:25
+ Enable-mailbox -Identity <<<<  $Mailbox.UserPrincipalName -Database $Mailbox.Database
    + CategoryInfo          : InvalidData: (:) [Enable-Mailbox], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Exchange.Management.RecipientTa
   sks.EnableMailbox





Here is what I have for the ps1 and csv (I could not find any storage groups on 2010 and believe they are not used )

ps1:

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
$UserList = Import-Csv "c:\nls2012OUUPNwithmail.csv"
$UserList = foreach ($Mailbox in $UserList) {
$Mailbox.UserPrincipalName
$Mailbox.Database
Enable-mailbox -Identity $Mailbox.UserPrincipalName -Database $Mailbox.Database
}


csv:

UserPrincipalName       Database            
RHanson93@domain.com      Exchange.domain.com\021213            
HHawkenson93@domain.com      Exchange.domain.com\021213            
CHedman94@domain.com      Exchange.domain.com\021213
Your CSV file should be servername\database. The UserPrincipalName needs to be one that actually exists in AD and it has to be exact.

Also using the script below. The one i originally gave you works for me but I have modified this one slightly.

$UserList = Import-Csv "c:\nls2012OUUPNwithmail.csv"
foreach ($Mailbox in $UserList) {
$Mailbox.UserPrincipalName
$Mailbox.Database
Enable-mailbox -Identity $Mailbox.UserPrincipalName -Database $Mailbox.Database
}
Here are my latest findings.

As stated before, most of the accounts do not have anything populating the "User Logon name" field, but do have the UPN suffix populated. I ran the following command to be sure I indeed had the UPN correct. Specifically, I am using the Rhanson93 user for testing:

PS C:\Users\administrator.NLS> dsquery user -name Rebecca* | dsget user -display -upn
  upn                         display
  carlsonraATnls.k12.mn.us     Rebecca A. Carlson
  @nlswildcats.com

  carlsonrmATnls.k12.mn.us     Rebecca M. Carlson
  RSchmidt00@nls.k12.mn.us    Rebecca Schmidt
dsget succeeded

As you can see, I end up with an entry of @nlswildcats.com. I then modified the account by adding the correct information to the User Logon field and ran the command again. This time the users upn was there.

PS C:\Users\administrator.NLS> dsquery user -name Rebecca* | dsget user -display -upn
  upn                          display
  carlsonraATnls.k12.mn.us      Rebecca A. Carlson
  Rhanson93ATnlswildcats.com

  carlsonrmATnls.k12.mn.us      Rebecca M. Carlson
  RSchmidt00ATnls.k12.mn.us     Rebecca Schmidt
dsget succeeded

Now, I made only a single entry in the csv file, as follows:

UserPrincipalName       Database            
RHanson93@nlswildcats.com      Exchange\021213      
      
(also changed the sever name to its fqdn for a 2nd test)

I am using the latest script you provided, but end up with the following error:

PS C:\Users\administrator.NLS> C:\updateuser2.ps1


Enable-Mailbox : Cannot bind argument to parameter 'Identity' because it is null.
At C:\updateuser2.ps1:5 char:26
+  Enable-mailbox -Identity <<<<  $Mailbox.UserPrincipalName -Database $Mailbox.Database
    + CategoryInfo          : InvalidData: (:) [Enable-Mailbox], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Exchange.Management.RecipientTa
   sks.EnableMailbox
I have used this script in my environment for many AD/Exchange modificaitons, and it works with no issues. At this point i am not sure what is happening on your side. You might want to close this question and re-open a new one.

Will.
I'm wondering if your .CSV is actually in the correct format.  All the posts show pretty much what what you would see when opening the file in Excel, but in Notepad it would look like below.
NickName,ProxyAddress,UPN
User1,User1@domain.com,User1@domain.com
User2,User2@domain.com,User2@domain.com
User3,User3@domain.com,User3@domain.com

Open in new window

It's a bit basic, but it's good to verify...
ASKER CERTIFIED SOLUTION
Avatar of tamray_tech
tamray_tech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worked, as needed