Getting and Setting the Manager Field in SCSM

I thought I’d share this bit of code as it took me a while to work out so hopefully it will save someone a bit of time.

Getting a users manager can be very useful for setting reviewers on a service request for example either via PowerShell or more likely Orchestrator.

The manager is nothing more than a relationship (what isn’t in Service Manager!) and we can obviously get this easily with SMLets and the class ‘System.UserManagesUser’.

Import-Module SMLets
$Class = Get-SCSMClass -Name “Microsoft.AD.User$”
$FindUser = Get-SCSMObject -Class $Class -Filter “Username -eq GH3 ”
$usermanagerclass = Get-SCSMRelationshipClass -name System.UserManagesUser
$ManagerID = Get-SCSMRelationshipObject -ByTarget $FindUser | Where {$_.RelationshipID -eq $usermanagerclass.id}

$manager = get-SCSMObject -id $ManagerID.SourceObject.id
$ManagerUsername = $Manager.UserName
We can also get the users the user manages by swapping -ByTarget with -BySource

Finally,we can also set the manager by creating a relationship using the code below:

$Class = Get-SCSMClass -Name “Microsoft.AD.User$”
$FindUser = Get-SCSMObject -Class $Class -Filter “Username -eq daviddo”
$findManager = Get-SCSMObject -Class $Class -Filter “Username -eq gh3”
$usermanagerclass = Get-SCSMRelationshipClass -name System.UserManagesUser
New-SCSMRelationshipObject -Relationship $usermanagerclass -Source $findManager -Target $FindUser -Bulk

Hope that helps 🙂

About the author