When the User Profile Service Application is created during SharePoint installation with default settings, the My Sites URL changes to online and that is cumbersome, so delete the default created User Profile service application and create a new one.
When creating the User profile service application, provide your site Host URL to the online URL host that would automatically change the My Sites URL at the Active Directory.
If the User Profile service application was created outside of the installation, then go to the User Profile Service Application, then My Sites settings and Setup My Sites. Then change the My Sites Host Location.
Once updated, the My Sites Host URL is updated in the Active Directory and is not given at SharePoint Central Administration.
Copy the following code and save it with the file name SetMySiteHostURLinAD.ps1.
- function PrintUsage {@”
- NAME: SetMySiteHostURLInAD.ps1
- SYNOPSIS: The purpose of this script is to set My Site Host URL in Active Directory.
- This URL will be returned through Exchange Autodiscover.
- MySiteHostURL–URL of My Site Host to set in Active Directory.
- Or use - get to get My Site Host URL from Active Directory.
- Or use - remove to remove My Site Host URL from Active Directory.
- SYNTAX: SetMySiteHostURLInAD.ps1“MySiteHostURL” | -get | -remove
- EXAMPLES: SetMySiteHostURLInAD.ps1“http:
- SetMySiteHostURLInAD.ps1 - get
- SetMySiteHostURLInAD.ps1 - remove“@
- }
-
- function GetConfigurationNamingContextPath {
- return GetEntryProperty“LDAP:
- }
-
- function GetExchangePath {
- param([string] $configurationNamingContextPath)
- return“LDAP:
- }
-
- function GetOrganizationContainerPath {
- param([string] $exchangePath)[string] $organizationContainerPath = “” ([ADSI] $exchangePath).Children | foreach {
- if (!$organizationContainerPath - and $_.SchemaClassName - eq“msExchOrganizationContainer”) {
- $organizationContainerPath = $_.Path
- }
- }
- return $organizationContainerPath
- }
-
- function GetEntryProperty {
- param([string] $entryPath, [string] $propertyName)
- $entry = [ADSI] $entryPath[string] $value = “”
- trap {
- continue
- }
- $value = $entry.Get($propertyName)
- return $value
- }
-
- function SetEntryProperty {
- param([string] $entryPath, [string] $propertyName, [string] $propertyValue)
- $entry = [ADSI] $entryPath
- if (!$propertyValue) {
- $entry.PutEx(1, $propertyName, $null)
- } else {
- $entry.Put($propertyName, $propertyValue)
- }
- trap {
- Write - Host“`nError setting property” - ForegroundColor Red
- continue
- }
- $entry.SetInfo()
- }
-
- function AddOrReplaceOrRemoveMySiteHostURL {
- param([string] $old, [string] $url)[string] $separator = “;” [string] $label = “SPMySiteHostURL” + $separator
- if (!$old) {
- if (!$url) {
- return“”
- } else {
- return $label + $url
- }
- }
- [int] $labelPosition = $old.IndexOf($label)
- if ($labelPosition - eq - 1) {
- if (!$url) {
- return $old
- } else {
- if ($old[$old.Length–1] - eq $separator) {
- return $old + $label + $url
- } else {
- return $old + $separator + $label + $url
- }
- }
- }
- [int] $valuePosition = $labelPosition + $label.Length[int] $nextLabelPosition = $old.IndexOf($separator, $valuePosition)
- if ($nextLabelPosition - eq - 1) {
- if (!$url) {
- if ($labelPosition - eq 0) {
- return“”
- } else {
- return $old.Substring(0, $labelPosition–1)
- }
- } else {
- return $old.Substring(0, $valuePosition) + $url
- }
- }
- if (!$url) {
- return $old.Substring(0, $labelPosition) + $old.Substring($nextLabelPosition + 1)
- } else {
- return $old.Substring(0, $valuePosition) + $url + $old.Substring($nextLabelPosition)
- }
- }
- if ($args.Count - ne 1) {
- Write - Host“`nError: Required argument missing or too many arguments” - ForegroundColor Red
- PrintUsage
- exit
- }
- if ($args[0] - eq“ - ? ” - or $args[0] - eq“ - h” - or $args[0] - eq“ - help”) {
- PrintUsage
- exit
- }
- [string] $url = “”
- if ($args[0] - ne“ - r” - and $args[0] - ne“ - remove”) {
- $url = $args[0]
- }
- Write - Host“`nSetting My Site Host URL in Active Directory…” [string] $configurationNamingContextPath = GetConfigurationNamingContextPath
- Write - Host“`nConfiguration Naming Context path: $configurationNamingContextPath” [string] $exchangePath = GetExchangePath $configurationNamingContextPath
- Write - Host“`nExchange path: $exchangePath” [string] $organizationContainerPath = GetOrganizationContainerPath $exchangePath
- Write - Host“`nOrganization Container path: $organizationContainerPath” [string] $propertyName = “msExchServiceEndPointURL”
- Write - Host“`nProperty name: $propertyName” [string] $old = GetEntryProperty $organizationContainerPath $propertyName
- Write - Host“`nOld value: $old”
- if (!$url) {
- Write - Host“`nRemoving value”
- }
- elseif($url - eq“ - g” - or $url - eq“ - get”) {
- Write - Host“”
- exit
- } else {
- Write - Host“`nAdding or replacing value: $url”
- }
- [string] $new = AddOrReplaceOrRemoveMySiteHostURL $old $url
- Write - Host“`nNew value: $new”
- SetEntryProperty $organizationContainerPath $propertyName $new
- Write - Host“”
Note: This script would only work in Exchange Server 2013 and will not work with Exchange Server 2010 because the script tries to update the property “msExchServiceEndPointURL” that is new in Exchange Server 2013.
- Go to Exchange Server and open Exchange Management Shell.
- Navigate the URL to the location where the script file was saved.
- Run the script with new mysite URL.
For example if the file is saved in the C drive and the new site URL is: http://NewMySiteURLOnline/sites/my, then
[PS] C:/> C:/SetMySiteHostURLInAD.ps1 http://NewMySiteURLOnline/sites/my
The preceding execution will update the My Sites Host URL at AD.
To verify, run the following script to check:
[PS] C:/> C:/SetMySiteHostURLInAD.ps -get.