question

JBezza avatar image
0 Votes"
JBezza asked ·

SMLets - Filtering SourceObject in Powershell

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.

windows-server-powershellmsc-service-manager-general
image.png (7.0 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
1 Vote"
AndreasBaumgarten answered ·

@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

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered ·

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

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JBezza avatar image
0 Votes"
JBezza answered ·

Hey @AndreasBaumgarten,

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

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered ·

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


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JBezza avatar image
0 Votes"
JBezza answered ·

Hey @AndreasBaumgarten,

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


image.png (7.1 KiB)
image.png (3.2 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JBezza avatar image
0 Votes"
JBezza answered ·

Hey @AndreasBaumgarten,

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

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JBezza ,

If you have any question about the code in the new approach please let me know.

If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A


Regards
Andreas Baumgarten

0 Votes 0 ·