Share via

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.

System Center Service Manager
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Andreas Baumgarten 132.1K Reputation points MVP Volunteer Moderator
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

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

5 additional answers

Sort by: Most helpful
  1. 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

    Was this answer helpful?

    0 comments No comments

  2. Andreas Baumgarten 132.1K Reputation points MVP Volunteer Moderator
    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

    Was this answer helpful?

    0 comments No comments

  3. 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.

    Was this answer helpful?

    0 comments No comments

  4. Andreas Baumgarten 132.1K Reputation points MVP Volunteer Moderator
    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

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.