scripted “re creation” of User Profile Service app from powershell as spfarm account after deleting the existing UPSA.

Thanks to Brian Lala and AutospInstaller for the inspiration and start-process syntax.

  • Ensure UAC is OFF
  • Run a PowerShell window as administrator
  • Paste the script below into¬†your.ps1 file and run it
$ErrorActionPreference="stop"
 
$script = {
Add-PSSnapin microsoft.sharepoint.powershell -ea SilentlyContinue
$ErrorActionPreference="stop"
$setNetbiosNames=$true
$prefix="hostname"
$tld="domain.dev"
$mshPort="80"
 
#
 
$upServiceAppName="User Profile Service Application"
$upAppPool="SharePoint Hosted Services"
$upDbServer="UPS"
 
$upProfileDBServer=$upDbServer
$upProfileDB=("$prefix" + "_Profile")
 
$upSyncDBServer=$upDbServer
$upSyncDB=("$prefix" + "_Sync")
 
$upSocialDBServer=$upDbServer
$upSocialDB=("$prefix" + "_Social")
 
$upSyncInstance="$prefix"
 
$upMySiteHostUrl=("http://$prefix" + "-mysites." + "$tld" + ":" + "$mshport" + "/")
$upManagedPath="Personal"
 
 
 
$upsapp = Get-SPServiceApplication | ? {$_.TypeName -like "User Profile Service Application"}
 
if($upsapp -eq $null) {
    #write-host "Delete pre existing User Profile timer jobs"
 
    #Get-SPTimerJob | where {$_.name -match "User Profile Service.*"} |  % {
    #    write-host "deleting timer job:" $_.Name
    #    $_.Delete()
    # }
 
    write-host "Create UPSA"
 
    try {
        $upsapp = New-SPProfileServiceApplication `
                    -ApplicationPool $upAppPool `
                    -MySiteHostLocation $upMySiteHostUrl `
                    -Name $upServiceAppName `
                    -ProfileDbName $upProfileDB `
                    -ProfileDbServer $upProfileDbServer `
                    -ProfileSyncDbServer $upSyncDbServer `
                    -ProfileSyncDbName $upSyncDB `
                    -SocialDbName $upSocialDb `
                    -SocialDbServer $upSocialDbServer
 
 
        if($setNetbiosName) {
            write-host "Enable NETBIOS domain names"
 
            $upsapp.NetBIOSDomainNamesEnabled=1
            $upsapp.update()
        }
 
        $upsapp
    }
    catch {
        write-host $_
    }
 
}
else {
    write-host "Pre existing User Profile Service Application"
}
 
if($upsapp -ne $null) {
    write-host "Create UPSA Proxy"
 
    $upsAppProxy = Get-SPServiceApplicationProxy | ? {$_.TypeName -like "User Profile Service Application Proxy"}
 
    if($upsAppProxy -eq $null) {
 
        try {
 
            $upsAppProxy = New-SPProfileServiceApplicationProxy `
                            -ServiceApplication $upsapp.Id `
                            -Name $upServiceAppName
 
            $upsAppProxy
        }
        catch {
            write-host $_
        }
    }
}
else {
    write-host "Pre existing UPSA Proxy"
}
 
 
}
 
 
 
 
 
# this runs the script defined above under spfarm user account
# orginally sourced from Brian Lala autoSP-Installer for the "Start-process" syntax, Thanks Brian :-)
# See Brian T if it does not work for you
#
$secpasswd = ConvertTo-SecureString "p@55w0rd" -AsPlainText -Force
$farmCredential = New-Object System.Management.Automation.PSCredential ("domain\spfarm", $secpasswd)
 
$scriptFile = "$env:TEMP\UPCreate-Script.ps1"
write-output $script | out-file $scriptFile
 
Start-Process  -WorkingDirectory $PSHOME -FilePath "powershell.exe" -Credential $farmCredential -ArgumentList "-Command Start-Process -WorkingDirectory `"'$PSHOME'`" -FilePath `"'powershell.exe'`" -ArgumentList `"'$scriptFile'`" -Verb Runas " -Wait
 
 
 
 
$msg = "UP SA Creation done`n`n"
 
$msg += "You need to start the UP Sync service in `"Services on a server`"`n"
$msg += "Create a Sync connection, if you get error in create, try to use a new name for connection`n"
$msg += "Ensure UP Service account has Admin and full control of UP SA`n"
$msg += "Ensure msh app pool account has Admin and full control of UP SA`n"
$msg += "Ensure app pool account has Admin and full control of UP SA`n"
$msg += "Ensure sp content account has Admin = read people data in UP SA`n"
$msg += "Ensure sp farm account has Admin and full control of UP SA`n"
$msg += "Ensure setup (you) account has Admin and full control of UP SA`n"
$msg += "Configure service application associations and ensure UP SA is associated"
 
$msg += "Run a full sync`n"
 
write-host $msg