You may also be interested in: O'Reilly - SharePoint 2010 at Work
Editor's note: Contributor Craig Pilkenton is a Senior Microsoft Consultant for CDW.com.
In this article we'll continue using the SharePoint management cmdlets, this time to interact with the 'dreaded' User Profile Service.
In my previous article, Using Powershell to... upload documents in SharePoint 2010, I talked about the basics of how to start using Powershell and some best practices.
In this one we'll jump directly to using the object model to talk to the User Profile Service (UPS). You've probably heard the horror stories of UPS making grown men cry or run off to become hermits. While it is an 'experience opportunity' working with the UPS, it is a very powerful storage engine of employee-to-employee discoverability.
Here's the file we'll be working from in this example:
I've designed this script to output the results to the screen in semi-colon separated format so that I can copy it into Excel and give to the customer for review, but as always, you can do anything with the results that Powershell can handle. I've marked up the file to denote what each section does but I'll call out the important areas below by line number.
- Line 1: this is an example for calling the file with parameters and their format. The third parameter, $rqstrUserProps, is where you pass in a semi-colon separated string of which User Profile parameters to pull out.
- If you don't have quick access to Central Admin of your SharePoint Farm, just uncomment Line 28 and all the Out-of-Box properties will show along with any custom ones as well.
- Lines 2-6: receive passed-in parameters make this script reusable so it can be called by other master scripts and/or be called as a Scheduled Task--
- SharePoint MySite URL
- Logon Domain of the users you wish to pull-in. This is only important if you have users in the UPS store that logon with different domains
- Semi-colon separated list of UPS properties to output
- Line 12: takes the input parameter of properties and split's it into an enumerable array of values
- Lines 18-19: this section uses the new Get-SPSite cmdlet
- Store a reference to the MySite in the $site variable
- Store a reference to Site Context using the new Get-SPServiceContext cmdlet in the $context variable
- Lines 20-21: this section obtains a reference to the UserProfileManager using the SharePoint Object Model (no special cmdlet here), then split's all the profiles into an enumerable array
- Line 23: here we start looping through all the individual profiles to get the target properties
- Lines 24-25: (optional) only needed as mentioned above
- Lines 30-32: while we are inside our Profiles loop, we fire another looping using the array of properties from Line 12 above to query that users profile for the property’s value and push it into a string variable created earlier
- Line 37: finally after all the looping is done we output the content to the screen to copy-paste into Excel
- This section could also output the content into a .csv file, send an email with it as an attachment, inject into a database, or any other need we have.
With the pieces documented out, let's open a Powershell command to navigate to the directory where your scripts will live (e.g. "CD E:\Powershell\" + Enter, etc.), enter the name of the script, and the parameters to start running the script:
.\Get_UserProfile_Properties.ps1 "http://MySite_Url" "mydomain\" "LogonID;DisplayName;Title;Department;Office;Manager"
So that's it. Powershell takes our parameters and outputs the data we told it to, giving us user data that can be fed to other user-based systems as needed. As usual, Powershell makes a tough task easy and repeatable.
A copy of the Powershell code, along with many others, is available at my SkyDrive location and the file is called Get_UserProfile_Properties.ps1.