Getting and Setting the Manager Field in SCSM

By |2017-12-07T11:15:47+00:00April 28th, 2015|PowerShell|0 Comments

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 🙂

Leave A Comment

like what you see? 

Sign-up to our newsletter and never miss out on the latest blogs, events and tech news from the world of risual
SUBSCRIBE!
Give it a try, you can unsubscribe anytime.