While working on my most recent project I have spent a lot of time using the user profile service and while it offers some real benefits I have found it can be difficult to troubleshoot, especially when it comes to synchronizing with external sources like active directory (AD).
After configuring the user profile service on the client’s live environment I noticed that the email and manager fields were missing for some users. I wanted to try and get an idea of the extent of the issue so I went to the manage user profiles screen in central admin (CA) and while its useful for finding individual users its doesn’t really help view certain properties for all users.
Thinking about the options available I knew from previous experience I could write some C# code to get this information but I have been making an effort to brush up on my PowerShell (PS) skills so I decided this would be a perfect opportunity.
As with all PS scripts its essential these are first tested in a development environment before run on live. I began by creating my PS script on my development environment using PowerGUI as I think this is an excellent tool which makes creating PS scripts much easier.
Before starting I worked out what the rough steps would be to get the user profile information and i came up with the following:
- Get the site the user profile service is associated with
- Get the user profile service
- Get all user profiles
- Loop through all user profiles outputting the required information
- Collate steps and parameterise into a function
I then started working on each step and finally put it all together. Each of the steps is detailed below.
From previous experience I knew the easiest way to get the user profile service was to get the service context for the site the user profile service is associated with. The first line gets the site passing a variable holding the site URL, next I check we have the site. I then got the service context associated with the site by calling the Get-SPServiceContext method passing the site
Once I had the service context the next step is to get the user profile manager which is done by creating a new UserProfileManager instance passing in the associated service context.
With the instance of the user profile manager I then got all users by calling the GetEnumerator method.
In my situation there were hundreds of users so I decided outputting the information to the PS console would be difficult to read of I decided to output the details to a text file instead.
I looped though each user profile outputting the users preferred name. In addition so I could get additional user profile property values I looped through an array containing the names of user profile properties I wanted to get and output a string containing the property name and the property value for the current user profile
Putting all the above steps together adding additional logging, variable checks and ensuring all settings can be passed in I ended up with the function below.
I have already used this function several times without any issues and I have found it a very easy and flexible way to pull information from the user profile store.
I hope this is helpful for others too.