Moving relationships between Service Manager User Configuration Items with Powershell

There have been a few instances where I needed to move a User CIs relationships to another. Specifically, moving over the items where they are the Affected User, Assigned To User, Config Item Owner, Created By User, Resolved By User and Closed By User.

For example, in the past I have had to delete a user CI where that CI was not appearing in Cireson, or had an issue with relationships in the database. Deleting the user CI would remove that CI from all of the calls and config items they were attached to in the ServiceManager DB. If you had a particular user that was the Affected User on 100s of calls this could be a problem if you are doing any reporting on the ServiceManager DB.

The following Powershell script can be used to move all of the user relationships from one User CI to the other. In the past I used this to move the relationships to a temp user CI, delete the CI that I was having trouble with, sync that CI back in then move the relationships back.

You will need to edit the Service Manager Powershell module location (line 14) and also the very last line, changing Example1 and Example2 to the username of the affected CIs. The Powershell uses a function to make it easier to pipe in variables with Orchestrator or other automation products.

As with any Powershell, this comes “as is” and you should run it at your own risk, if you are happy with what the script does/achieves.

Function Move-Relationships {
[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$oldADUser,
	
   [Parameter(Mandatory=$True)]
   [string]$newADUser
)

if (!(Get-module System.Center.Service.Manager)) 
{
	#TODO: You may need to alter the location of the PowerShell Module:
	import-module 'E:\Program Files\Microsoft System Center 2012 R2\Service Manager\PowerShell\System.Center.Service.Manager.psd1';
}

#Get the old and new user objects from the CMDB
$oldName = Get-SCClassInstance -Class (Get-SCClass -Name "System.Domain.User") -Filter "UserName -eq $oldADUser";
$newName = Get-SCClassInstance -Class (Get-SCClass -Name "System.Domain.User") -Filter "UserName -eq $newADUser";

#Get relationships targeted at the old user object
$relatedItems = Get-SCRelationshipInstance -TargetInstance $oldName;

foreach ($item in $relatedItems)
{
	#Figure out the type of each of those relationships & check against the list
	$relType = Get-SCRelationship -Id ($item.RelationshipId);
	if ($relType.Name -eq "System.WorkItem.TroubleTicketClosedByUser" -or
		$relType.Name -eq "System.WorkItemAffectedUser" -or
		$relType.Name -eq "System.WorkItemCreatedByUser" -or
		$relType.Name -eq "System.WorkItemAssignedToUser" -or
		$relType.Name -eq "System.WorkItem.TroubleTicketResolvedByUser" -or
		$relType.Name -eq "System.ConfigItemOwnedByUser")
	{
		#delete the old relationships & recreate them with the new user object
		Remove-SCSMRelationshipInstance -Instance $item;
		New-SCRelationshipInstance -RelationshipClass (Get-SCRelationship -Name $relType.Name) -Source $item.SourceObject -Target $newName;
	}
}

}

Move-Relationships -oldADUser Example1 -newADUser Example2