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.
222 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,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 107.5K 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. JBezza 21 Reputation points
    2021-02-22T11:27:20.293+00:00

    Hey @Andreas Baumgarten ,

    The Run Book entries are now gone with the new code.
    I am just looking through the code you sent to learn the new approach.

    Thank you for taking some time out of your day to help with my query.

    Kind Regards


Your answer

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