Thursday 10 November 2016

How to create a User Profile service application in SharePoint Server 2016 Using PowerShell

Today we are writing about how to create a User Profile service application in SharePoint 2016 using PowerShell ISE.
Below are the steps to create a User Profile service application
  • We need to verify that the user account that is performing this procedure is a member of the Farm Administrators group for the farm for which you want to create the service application.
  • Click on User Profile button on the server and enter PowerShell ISE and run as administrator.
  • It will open with blank page, if written space is unavailable we need to click on CTRL+N and copy below script with required modification.

#Adds support for My Sites, Profiles pages, Social Tagging, and other social computing features.
#Read more: http://technet.microsoft.com/en-us/library/ee662538.aspx
$ServiceApplicationName = "User Profile Service Application"
$ServiceName1 = "User Profile Service"
$ServiceName2 = "User Profile Synchronization Service"
$ServerName = "SP2016"
$FarmAccount = "dOMAIN\Farm User"
$FarmAccountPwd = "!@#DER#@##"
$MySiteHostLocation = "http://Your site.com/my/mysite"
$DatabaseNameProfile = 'spServiceApp-UPProfile'
$DatabaseNameSocial = 'spServiceApp-UPSocial'
$DatabaseNameSync = 'spServiceApp-UPSync'
$spAppPoolName = "UserProfile Service Application Pool Account"
$spAppPoolAcc = "dOMAIN\Farm User"

Write-Host "SharePoint 2013 - '$ServiceApplicationName'..."
Write-Host " - You must be logged on with the Farm Account which is in the local Administrators Group."
Write-Host " - You must restart the server once you put the Farm Account in the local Administrators Group due to security token."
Write-Host " - You must open a PowerShell console using 'Run as administrator' to run the script."
#Allows to use SharePoint cmdlets from inside the Windows PowerShell command window
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
Start-SPAssignment -Global | Out-Null

#Get default SQL server
$DatabaseServer = (Get-SPDatabase | ? { $_.Type -eq "Configuration Database" }).NormalizedDataSource

try
{
   #Check for existing service application and proxy
   $ExistingServiceApp = Get-SPServiceApplication | where-object {$_.Name -eq $ServiceApplicationName}

if ($ExistingServiceApp -eq $null)
   {
       Write-Host " - Restarting SPTimerV4"
       #restart-service SPTimerV4

Write-Host " - Creating '$ServiceApplicationName'"

       #Check if application pool already exist, if not create it
       $spManagedAccount = Get-SPManagedAccount -Identity $spAppPoolAcc
       $ApplicationPool = Get-SPServiceApplicationPool -Identity $spAppPoolName -ErrorAction SilentlyContinue
       if ($ApplicationPool -eq $null)
       {
        New-SPServiceApplicationPool -Name $spAppPoolName -Account $spManagedAccount | Out-Null
       }
       else
       {
           Set-SPServiceApplicationPool $ApplicationPool -Account $spManagedAccount | Out-Null
       }  

       $userProfileService = New-SPProfileServiceApplication -Name $ServiceApplicationName -ApplicationPool $spAppPoolName -ProfileDBServer $DatabaseServer -ProfileDBName $DatabaseNameProfile -SocialDBServer $DatabaseServer -SocialDBName $DatabaseNameSocial -ProfileSyncDBServer $DatabaseServer -ProfileSyncDBName $DatabaseNameSync -MySiteHostLocation $MySiteHostLocation

       #Create Service Application Proxy
       Write-Host " - Creating '$ServiceApplicationName' proxy"
       New-SPProfileServiceApplicationProxy -Name "$ServiceApplicationName Proxy" -DefaultProxyGroup -ServiceApplication $userProfileService | Out-Null

       #Start service instances
       Write-Host " - Starting service instances"
       #Gets the service to determine its status
       $service1 = $(Get-SPServiceInstance | where {$_.TypeName -match $ServiceName1} | where {$_.Server -match "SPServer Name="+$ServerName})
       $service2 = $(Get-SPServiceInstance | where {$_.TypeName -match $ServiceName2} | where {$_.Server -match "SPServer Name="+$ServerName})
         
       If ($service1.Status -eq "Disabled")
       {
          Write-Host " - Starting" $service1.ID
           Start-SPServiceInstance -Identity $service1.ID | Out-Null
       }
       If ($service2.Status -eq "Disabled")
       {
          Write-Host " - Starting" $service2.ID
           $userProfileService.SetSynchronizationMachine($ServerName, $service2.ID, $FarmAccount, $FarmAccountPwd)
           Start-SPServiceInstance -Identity $service2.ID | Out-Null
       }
       else
       {
           write-host "Profile Synchronization Service is" $service2.Status
       }
            
Write-Host " - Done creating '$ServiceApplicationName'.`n"
}
else
   {
       #Write-Host " - '$ServiceApplicationName' already exists."
       write-host " - Removing '$ServiceApplicationName'..."
       Remove-SPServiceApplication $ExistingServiceApp -removedata -Confirm:$false

       #Proxy is NOT automatically deleted
       $ExistingServiceAppProxy = Get-SPServiceApplicationProxy | where-object {$_.Name -eq "$ServiceApplicationName Proxy"}
       if ($ExistingServiceAppProxy -ne $null)
       {
           write-host " - Removing '$ServiceApplicationName proxy'..."
           Remove-SPServiceApplicationProxy $ExistingServiceAppProxy -Confirm:$false
       }
       write-host " - Stopping service instances..."
       Get-SPServiceInstance | where-object {$_.TypeName -eq $ServiceName1} | where {$_.Server -match "SPServer Name="+$ServerName} | Stop-SPServiceInstance -Confirm:$false | Out-Null
       write-host " - Stopping service instances..."
       Get-SPServiceInstance | where-object {$_.TypeName -eq $ServiceName2} | where {$_.Server -match "SPServer Name="+$ServerName} | Stop-SPServiceInstance -Confirm:$false | Out-Null
   }
}
catch { write-Output $_ }

Stop-SPAssignment -Global | Out-Null
cmd.exe /c pause


  • Changes are required on highlighted line.
  • Once all changes done, click on run button or press F5 on the key board, scripting will start running. Wait for some time till we are getting successful message on the PowerShell script page.
  • Once run successfully, Open CA and verify from app management service is created.
  • We can verify by clicking on Database server and verify below Database has been created.


Thanks ☺

2 comments:

  1. The writing skills and better way to get more and implementing tips I ever seen here after reading this company profile writer.

    ReplyDelete
  2. Thank You, this is very detailed and informative script.

    ReplyDelete