Before we start…
1. It’s not supported to create a User Profile Sync connection by script or code using the server object model. We will NOT do this here!
2. The script in this article is part of my “dev lab” setup scripts. – The script cannot be used “as-is” in real world scenarios. If you want to use this approach in your deployment you MUST customize the script to fit your needs! – Especially for selecting sync entries from the Active Directory treeview selector.
3. The script is a Proof of Concept.
4. It’s tested only on SharePoint Server 2010. I’ll do it for SP 2013 later.
5. The script has almost no error handling !!! Sometimes I get the error message “MOSS MA not found”. In this case I restart the “FIMService” and try again.
The approach…
… is to use Internet Explorer as COM object to navigate to pages of the Central Administration and “act like a user”. This means:
1. Open IE window
2. Navigate to Central Administration -> Manage Service Applications
3. Search of the (first) “User Profile Service Application” in the Service Apps list and navigate to it’s settings page.
4. Open page “Configure Synchronization Connections”.
5. “Click” on link “Create New Connection”
6. Fill in connection parameters such as name, forrest, user profile sync account. Click button “Populate Containers”. Click “Select All”. Click “OK”. Wait until connection is created.
7. On the User Profile Service Application property page: Click on “Start Profile Synchronization”. There select “Start Full Synchronization”. Wait until sync starts. Wait until sync is complete.
The script…
Here it is:
http://gallery.technet.microsoft.com/Create-User-Profile-Sync-98ab7201
(Update) Here is the script for for SharePoint 2013: http://gallery.technet.microsoft.com/Create-User-Profile-Sync-e9ec0cf7 (Article here).
First the script contains some config settings for the new connection.
![clip_image001 clip_image001](https://i0.wp.com/blog.kenaro.com/wp-content/uploads/2013/09/clip_image001_thumb.png?resize=404%2C106&ssl=1)
Appendix
Here are the input boxes related to the config values (line numbers):
![clip_image003 clip_image003](https://i0.wp.com/blog.kenaro.com/wp-content/uploads/2013/09/clip_image003_thumb.jpg?resize=404%2C227&ssl=1)
The script is written in for a English localized CA. However I have created resource variables for other languages.
![clip_image004 clip_image004](https://i0.wp.com/blog.kenaro.com/wp-content/uploads/2013/09/clip_image004_thumb.png?resize=404%2C217&ssl=1)
Usage of resource variables (line numbers)
![clip_image006 clip_image006](https://i0.wp.com/blog.kenaro.com/wp-content/uploads/2013/09/clip_image006_thumb.jpg?resize=404%2C175&ssl=1)
![clip_image008 clip_image008](https://i0.wp.com/blog.kenaro.com/wp-content/uploads/2013/09/clip_image008_thumb.jpg?resize=404%2C104&ssl=1)
![clip_image010 clip_image010](https://i0.wp.com/blog.kenaro.com/wp-content/uploads/2013/09/clip_image010_thumb.jpg?resize=404%2C89&ssl=1)
![clip_image012 clip_image012](https://i0.wp.com/blog.kenaro.com/wp-content/uploads/2013/09/clip_image012_thumb.jpg?resize=404%2C227&ssl=1)
![clip_image014 clip_image014](https://i0.wp.com/blog.kenaro.com/wp-content/uploads/2013/09/clip_image014_thumb.jpg?resize=404%2C75&ssl=1)