Home Powershell

Use -filter with Get-SCSMRelationshipObject

Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭

Hello guys,

does anyone know, how to use the -filter switch in the Get-scsmrelationshipobject function correctly?

I have a request with multiple SW Assets for installation and for every Asset I check if it is allowed. If it is not, the relationship to that certain Service Request shall be deleted.

I tried it with -Filter "SourceObject.Name -eq $($SR.Id)", where $SR is the Service Request, which I receive via another Relationship Object by an Activity, but with that I receive all SRs where this SW asset is related.

I know I could accomplish that via pipelining all SR <-> SW Relationships for this SW Asset, but I'd like to avoid that.

@Adam_Dzyacky do you know how that works? :)

Best Answer

  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭
    Answer ✓

    I've only used said parameter in a limited capacity, but I think/hope this is what you're after:

    #classes
    $irClass = Get-SCSMClass -Name "System.WorkItem.Incident$"
    $assignedToUserRelClass = Get-SCSMRelationshipClass -Name "System.WorkItemAssignedToUser$"
    
    #an incident
    $incident = Get-SCSMObject -Class $irClass -Filter "name -eq 'IR2030'"
    
    #filter
    Get-SCSMRelationshipObject -BySource $incident -Filter "RelationshipID -eq '$($assignedToUserRelClass.Id)'"
    


    In this example, I want to retrieve the Assigned To User from a single Incident but I do not want to use PowerShell pipeline into a Where-Object to filter down by Relationship.

Answers

  • Adam_DzyackyAdam_Dzyacky Product Owner Contributor Monkey ✭✭✭✭✭
    Answer ✓

    I've only used said parameter in a limited capacity, but I think/hope this is what you're after:

    #classes
    $irClass = Get-SCSMClass -Name "System.WorkItem.Incident$"
    $assignedToUserRelClass = Get-SCSMRelationshipClass -Name "System.WorkItemAssignedToUser$"
    
    #an incident
    $incident = Get-SCSMObject -Class $irClass -Filter "name -eq 'IR2030'"
    
    #filter
    Get-SCSMRelationshipObject -BySource $incident -Filter "RelationshipID -eq '$($assignedToUserRelClass.Id)'"
    


    In this example, I want to retrieve the Assigned To User from a single Incident but I do not want to use PowerShell pipeline into a Where-Object to filter down by Relationship.

  • Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited February 2023

    Hello @Adam_Dzyacky ,

    thank you for the answer, I really appreciate that :)

    I guess I forgot to mention that I already use the -Filter in other scripts for receiving RelObjects by Source, where I only want certain Relationship Types ( I learned the hard way, that it's not possible to use the -Relationship switch when using -bySource whereas it works with -ByTarget)

    But as you can use -Relationship when you get a RelObject when you use -ByTarget, I thought maybe I can also combine it with a -Filter , so I could e.g. get all Service Requests where a certain SW Asset is the Target, and with the filter I could limit it to the Relationship to a certain Work Item ;) But I guess I have to use pipelining then :)

Sign In or Register to comment.