共用方式為


Got Orphaned OpsMgr Objects?

Have you ever wondered what would happen if, in Operations Manager, you’d delete a Management Server or Gateway that managed objects (such as network devices) or has agents pointing uniquely to it as their primary server?

The answer is simple, but not very pleasant: you get ORPHANED objects, which will linger in the database but you won’t be able to “see” or re-assign anymore from the GUI.

So the first thing I want to share is a query to determine IF you have any of those orphaned agents. Or even if you know, since you are not able to "see" them from the console, you might have to dig their name out of the database. Here's a query I got from a colleague in our reactive support team:

-- Check for orphaned health services (e.g. agent). declare @DiscoverySourceId uniqueidentifier; SET @DiscoverySourceId = dbo.fn_DiscoverySourceId_User(); SELECT TME.[TypedManagedEntityid], HS.PrincipalName FROM MTV_HealthService HS INNER JOIN dbo.[BaseManagedEntity] BHS WITH(nolock)     ON BHS.[BaseManagedEntityId] = HS.[BaseManagedEntityId] -- get host managed computer instances INNER JOIN dbo.[TypedManagedEntity] TME WITH(nolock)     ON TME.[BaseManagedEntityId] = BHS.[TopLevelHostEntityId]     AND TME.[IsDeleted] = 0 INNER JOIN dbo.[DerivedManagedTypes] DMT WITH(nolock)     ON DMT.[DerivedTypeId] = TME.[ManagedTypeId] INNER JOIN dbo.[ManagedType] BT WITH(nolock)     ON DMT.[BaseTypeId] = BT.[ManagedTypeId]     AND BT.[TypeName] = N'Microsoft.Windows.Computer' -- only with missing primary LEFT OUTER JOIN dbo.Relationship HSC WITH(nolock)     ON HSC.[SourceEntityId] = HS.[BaseManagedEntityId]     AND HSC.[RelationshipTypeId] = dbo.fn_RelationshipTypeId_HealthServiceCommunication()     AND HSC.[IsDeleted] = 0 INNER JOIN DiscoverySourceToTypedManagedEntity DSTME WITH(nolock)     ON DSTME.[TypedManagedEntityId] = TME.[TypedManagedEntityId]     AND DSTME.[DiscoverySourceId] = @DiscoverySourceId WHERE HS.[IsAgent] = 1 AND HSC.[RelationshipId] IS NULL;

Once you have identified the agent you need to re-assign to a new management server, this is doable from the SDK. Below is a powershell script I wrote which will re-assign it to the RMS. It has to run from within the OpsMgr Command Shell.
You still need to change the logic which chooses which agent - this is meant as a starting base... you could easily expand it into accepting parameters and/or consuming an input text file, or using a different Management Server than the RMS... you get the point.

  1. $mg = (get-managementgroupconnection).managementgroup  
  2. $mrc = Get-RelationshipClass | where {$_.name –like "*Microsoft.SystemCenter.HealthServiceCommunication*"}  
  3. $cmro = new-object Microsoft.EnterpriseManagement.Monitoring.CustomMonitoringRelationshipObject($mrc)  
  4. $rms = (get-rootmanagementserver).HostedHealthService  
  5.  
  6. $deviceclass = $mg.getmonitoringclass(“HealthService”)  
  7. $mc = Get-connector | where {$_.Name –like “*MOM Internal Connector*”}  
  8.    
  9. Foreach ($obj in $mg.GetMonitoringObjects($deviceclass))  
  10. {  
  11.     #the next line should be changed to pick the right agent to re-assign
  12.     if ($obj.DisplayName -match 'dsxlab')  
  13.     {  
  14.                 Write-host $obj.displayname  
  15.                 $imdd = new-object Microsoft.EnterpriseManagement.ConnectorFramework.IncrementalMonitoringDiscoveryData  
  16.                 $cmro.SetSource($obj)  
  17.                 $cmro.SetTarget($rms)  
  18.                 $imdd.Add($cmro)  
  19.                 $imdd.Commit($mc)  
  20.     }  

 

Similarly, you might get orphaned network devices. The script below is used to re-assign all Network Devices to the RMS. This script is actually something I have had even before the other one (yes, it has been sitting in my "digital drawer" for a couple of years or more...) and uses the same concept - only you might notice that the relation's source and target are "reversed", since the relationships are different:

  • the Management Server (source) "manages" the Network Device (target)
  • the Agent (source) "talks" to the Management Server (target)

With a bit of added logic it should be easy to have it work for specific devices.

  1. $mg = (get-managementgroupconnection).managementgroup  
  2.  
  3. $mrc = Get-RelationshipClass | where {$_.name –like "*Microsoft.SystemCenter.HealthServiceShouldManageEntity*"}  
  4.  
  5. $cmro = new-object Microsoft.EnterpriseManagement.Monitoring.CustomMonitoringRelationshipObject($mrc)  
  6. $rms = (get-rootmanagementserver).HostedHealthService  
  7.  
  8. $deviceclass = $mg.getmonitoringclass(“NetworkDevice”)  
  9.  
  10. Foreach ($obj in $mg.GetMonitoringObjects($deviceclass))  
  11.  
  12. {  
  13.                 Write-host $obj.displayname  
  14.                 $imdd = new-object Microsoft.EnterpriseManagement.ConnectorFramework.IncrementalMonitoringDiscoveryData  
  15.                 $cmro.SetSource($rms)  
  16.                 $cmro.SetTarget($obj)  
  17.                 $imdd.Add($cmro)  
  18.  
  19.                 $mc = Get-connector | where {$_.Name –like “*MOM Internal Connector*”}  
  20.  
  21.                 $imdd.Commit($mc)  

 

Disclaimer

The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my own personal opinion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.