SMLets - Filtering SourceObject in Powershell

JBezza 21 Reputation points
2021-02-22T08:24:37.663+00:00

Hello!
I have searched the web for many hours and have returned nothing to resolve my issue.
I am very new to coding in PowerShell and using SMLets so please mind my coding below.
Here is the code I currently have:

# SM ID  
$ID = "SR1089579"  
  
# Change Directory to Service Manager Server  
$SMDefaultComputer = "SC-SM-MS"  
  
# Variables for Service Request ID and Items to filter  
$SMID = "Name -eq $ID"  
$SearchCriteria = Get-SCSMRelationshipClass -Name "System.WorkItemAboutConfigItem$"  
  
# Variables for selecting class and filtering by above variables  
$SRclass = Get-SCSMClass -name "System.WorkItem.ServiceRequest$"  
$Request = Get-SCSMobject -class $SRClass -filter $SMID  
  
# Using all above variables to search and display infomation  
Get-SCSMRelationshipObject -BySource $Request | Where-Object {$_.RelationshipId -eq $SearchCriteria.Id} | FT TargetObject, SourceObject  

The code currently does what I want it to do but with one issue.
Here is the output of the code I have shown above:

70548-image.png

As you can see it pulls through all the details I need and a little bit extra.
I would like to filter out all the Run Book entries and only leave the Service Request entries.

As I stated above I have searched for an answer but to no avail.
Any help would be appreciated but please could you explain any code because I am trying to learn.
Also any tips on my script would be appreciated.

Thank you for taking the time to read my issue.

Service Manager
Service Manager
A family of System Center products for managing incidents and problems.
206 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,322 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 94,196 Reputation points MVP
    2021-02-22T11:19:03.023+00:00

    @JBezza

    Lets try a different approach:

    # SM ID  
    $ID = "SR1089579"  
          
    # Change Directory to Service Manager Server  
    $SMDefaultComputer = "SC-SM-MS"  
          
    # Variables for Service Request ID and Items to filter  
    $relWIaboutCI = Get-SCSMRelationshipClass -Name System.WorkItemAboutConfigItem$  
         
    # Variables for selecting class and filtering by above variables  
    $SRclass = Get-SCSMClass -name System.WorkItem.ServiceRequest$  
    $Request = Get-SCSMobject -class $SRClass -filter "ID -eq $ID"  
    $RequestDP = $Request.DisplayName  
      
    Write-Output "This is the SR object: $RequestDP"  
          
    # Using all above variables to search and display information  
    $relObj = Get-SCSMRelatedObject -SMObject $Request -Relationship $relWIaboutCI  
    $Object = New-Object PSObject -Property @{  
        TargetObject = $relobj.DisplayName  
        SourceObject = $Request.DisplayName  
        }  
    $Object  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 94,196 Reputation points MVP
    2021-02-22T09:57:35.457+00:00

    Hi @JBezza ,

    Could you please try this:

    # SM ID  
    $ID = "SR1089579"  
          
    # Change Directory to Service Manager Server  
    $SMDefaultComputer = "SC-SM-MS"  
          
    # Variables for Service Request ID and Items to filter  
    $SearchCriteria = Get-SCSMRelationshipClass -Name "System.WorkItemAboutConfigItem$"  
          
    # Variables for selecting class and filtering by above variables  
    $SRclass = Get-SCSMClass -name "System.WorkItem.ServiceRequest$"  
    $Request = Get-SCSMobject -class $SRClass -filter "ID -eq $ID"  
          
    # Using all above variables to search and display information  
    Get-SCSMRelationshipObject -BySource $Request | Where-Object {$_.RelationshipId -eq $SearchCriteria.Id} | FT TargetObject, SourceObject  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. JBezza 21 Reputation points
    2021-02-22T10:02:18.477+00:00

    Hey @Andreas Baumgarten ,

    Thank you for the reply.
    It seems that the code you have send is still showing the Run Book entries in the results.

    0 comments No comments

  3. Andreas Baumgarten 94,196 Reputation points MVP
    2021-02-22T11:00:40.533+00:00

    Please try again:

    # SM ID
     $ID = "SR1089579"
    
    
     # Change Directory to Service Manager Server
     $SMDefaultComputer = "SC-SM-MS"
    
     # Variables for Service Request ID and Items to filter
     $SearchCriteria = Get-SCSMRelationshipClass -Name System.WorkItemAboutConfigItem$
     $scID = $SearchCriteria.Id
    
     # Variables for selecting class and filtering by above variables
     $SRclass = Get-SCSMClass -name System.WorkItem.ServiceRequest$
     $Request = Get-SCSMobject -class $SRClass -filter "ID -eq $ID"
    
    
    # Just for test
     Write-Output "This is the SR object: $($Request.DisplayName)"
    
     # Using all above variables to search and display infomation
    Get-SCSMRelationshipObject -BySource $Request | Where-Object {$_.RelationshipId -eq $scID } | FT TargetObject, SourceObject
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  4. JBezza 21 Reputation points
    2021-02-22T11:13:38.527+00:00

    Hey @Andreas Baumgarten ,

    Thank you for replying again.

    As you can see below I have tested the code you sent and I am still seeing the Run Book in the displayed information:
    70588-image.png

    Also the Write-Output test code returned this:

    70613-image.png

    I tried to put $($Request.DisplayName) in and out of the String but still produced the same output.

    Kind Regards

    0 comments No comments