Updating Service Manager Work Items Ownership (In Bulk!)

[Last Reviewed 2018-07-12]

Let’s say you recently changed your username. You’ve already gone through the process of getting Windows/Office/other apps to recognize you by your new details. Then you open up Service Manager for your ticket queue and… it’s empty!

One of two things have happened here: either another member of your team worked on all your tasks (huzzah!), or Service Manager still knows of all your work items but still has them assigned to your old persona. Assuming the former didn’t happen, here’s a quick-and-dirty PowerShell script to help automate the otherwise-tedious process of updating all your work items.

(Note: this script requires SMLets)

# What type of work items are you looking for?
# Incident = "System.WorkItem.Incident$"
# Problem = "System.WorkItem.Problem$"
# Service Request = "System.WorkItem.ServiceRequest$"
# Manual Activity = "System.WorkItem.Activity.ManualActivity$"

$class = Get-SCSMClass -Name System.WorkItem.Activity.ManualActivity$

# Gets the SCSM object(s) of you (old and new)
$assignedToUserRelClass = Get-SCSMRelationshipClass -Name System.WorkItemAssignedToUser$
$userClass = Get-SCSMClass -Name System.Domain.User$
$oldAssignedTo = Get-SCSMObject -Class $userClass -Filter "Username -eq YOUR_OLD_USERNAME"
$newAssignedTo = Get-SCSMObject -Class $userClass -Filter "Username -eq YOUR_NEW_USERNAME"

# Let's show a progress bar while we're at it
$i = 0

Get-SCSMObject -Class $class | ForEach-Object {
$id = $_.Id
Write-Host "Looking at $id..." -ForegroundColor Cyan
$getCurrentlyAssignedTo = Get-SCSMRelatedObject -SMObject $_ -Relationship $assignedToUserRelClass
if ($getCurrentlyAssignedTo -eq $oldAssignedTo)
{
New-SCSMRelationshipObject -Relationship $assignedToUserRelClass -Source $($_) -Target $newAssignedTo -Bulk
Write-Host "$id assignment has been updated to $newAssignedTo" -Verbose -ForegroundColor Yellow
$i++
}
}

Write-Host "Operation completed. $newAssignedTo has been assigned $i $($class.DisplayName)s." -ForegroundColor Green

 

Note: There is a grammatical error when running this script with manual activities. As seen in the last line, it will show “activitys.” If this bothers you as much as it bothers me, replace the last line with:

if ($class.DisplayName -eq "Manual Activity")
{
Write-Host "Operation completed. $newAssignedTo has been assigned $i Manual Activities." -ForegroundColor Green
}
else ($class.DisplayName -neq "Manual Activity")
{
Write-Host "Operation completed. $newAssignedTo has been assigned $i $($class.DisplayName)s." -ForegroundColor Green
}

(Ahh, much better)

 

Just like that, your work items have returned! Yay? 🙂

 

Jacob

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.