What in the User Profile Service Application is going on here?

While working with PowerShell to build out Service Applications, I managed to get my test farm into a state where I no longer had a User Profile Service Application, but the user profile timer jobs were still around. 

Trying to create a new User Profile Service Application would result in the following :

New-SPProfileServiceApplication : An object of the type Microsoft.Office.Server.ActivityFeed.ActivityFeedUPAJob named "User Profiles_ActivityFeedJob" already exists under the parent Microsoft.Office.Server.Administration.UserProfileService named "". Rename your object or delete the existing object.
At line:1 char:32

+ New-SPProfileServiceApplication <<<< -Name "User Profiles" -ApplicationPool (Get-SPServiceApplicationPool "SharePoint Services Default") -ProfileDBName "UserProfile_Profiles" -ProfileDBServer "Jorman-SQL-02" -ProfileSyncDBName "UserProfile_Sync" -ProfileSyncDBServer "Jorman-SQL-02" -SocialDBName "UserProfile_Social" -SocialDBServer "Jorman-SQL-02"

+ CategoryInfo : InvalidData: (Microsoft.Offic...viceApplication:SPCmdletNewProfileServiceApplication) [New-SPProfileServiceApplication], SPDuplicateObjectException

+ FullyQualifiedErrorId : Microsoft.Office.Server.UserProfiles.PowerShell.SPCmdletNewProfileServiceApplication

 

Trying to get to the Timer Job Definition page in Central Admin would return the following error.  Entry is from the SharePoint trace logs, but the message was displayed in the popup dialog on the page.  Notice in the stack, that we’re getting the UserProfileApplicationJob when the exception is thrown.

2/08/2011 19:36:41.02     w3wp.exe (0x1AE0)                           0x0E18    SharePoint Foundation             Runtime                           tkau    Unexpected   

System.InvalidOperationException: Operation is not valid due to the current state of the object.    

at Microsoft.Office.Server.Administration.UserProfileApplicationJob.get_UserProfileApplication() at Microsoft.Office.Server.Administration.UserProfileApplicationJob.get_DisplayName()
    

at Microsoft.SharePoint.ApplicationPages.JobDefinitionsDataSourceView.AddRow(SPJobDefinition jd, DataTable table, DataSourceSelectArguments selectArguments)    

at Microsoft.SharePoint.ApplicationPages.JobDefinitionsDataSourceView.AddService(SPService service, DataTable table, DataSourceSelectArguments selectArguments)    

at Microsoft.SharePoint.ApplicationPages.JobDefinitionsDataSourceView.AddFarm(SPFarm farm, DataTable table, DataSourceSelectArguments selectArguments)    

at Microsoft.SharePoint.WebControls.TimerJobDataSourceView.FillDataTable(DataTable table, DataSourceSelectArguments selectArguments)    

at Microsoft.SharePoint.WebControls.DataTableDataSourceView.Select(DataSourceSelectArguments selectArguments)    

at Microsoft.SharePoint.WebControls.AdministrationDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)    

at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)    

at System.Web.UI.WebControls.DataBoundControl.PerformSelect()     at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()    

at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()     at System.Web.UI.Control.EnsureChildControls()   
 

At this point, I need to delete some Timer Job Definitions, but I can’t get to Job Definition page in Central Admin to see what jobs are still in the configuration.  This left me going back to PowerShell to do the cleanup.  I started with running Get-SPTimerJob.  This dumps out all the job definitions in the configuration.  I could see the “User Profiles_ActivityFeedJob” definition mentioned in my PowerShell error.  I also noticed there were 9 other Timer Jobs that started with “User Profiles”.  In order to delete them all, I ran the following :

get-sptimerjob | where {$_.Name -match "User Profile"} | %{$_.Delete()}

This grabs all the Timer Jobs that have a name like “User Profile”, then calls Delete on the definition.  Once I ran the above, I was able to provision my User Profile Service Application as well as get back into the Review Job Definitions page in Central Admin.