Smlets Script help

Hello,

I have been trying to write a script using PowerShell and SMlets to get incidents in the system that are using a specific CI value and have status set to "closed". I can get the incidents that are using "Temporary CI" as the affected CI but I can't seem to figure out how to filter out by only "Closed" status incidents. Here is my script:

# Script to get tickets created with 'Temporary CI' used
# Un-comment the #.Count to simply count the number of tickets with this relationship

$WIAffectsCIClass = Get-SCSMRelationshipClass System.WorkItemAboutConfigItem$

$TempCIClass = Get-scsmClass COMPANY.CI.Class
$TempCI = Get-scsmObject -class $TempCIClass -Filter "DisplayName -like %Temporary CI%"

(Get-scsmRelationshipObject -ByTarget $TempCI | ? {$_.RelationshipID -eq $WIAffectsCIClass.ID} )#.count

What am I missing to filter out only the ones with "status=closed"?

August 19th, 2015 2:25pm

I am not sure I understand your requirement.

You want all closed Incidents with Affected Config Item with Name like *Temporary CI*?

If so you need to get the Incidents in your script (filtered by status = closed) and from there you need to work with the relationship WorkItemAboutConfigItem and a get CI with Displayname -like *Temporary CI*.

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 3:08pm

Hello Andreas,

Thanks for your reply. I replied back the same day but just realized my post did not submit for some reason.... sorry for the delay.

Yes I want to pull all the closed incidents that have the affected CI as "Temporary CI" which is a CI I have created.

How do you go about doing this Andreas? I had the same idea but could not figure it out. As you can see above in my script I can pull the objects from WorkItemAboutConfigItem and list all the incidents using that relationship successfully.

I also know how to pull a list of incidents with a closed status... but how do I feed that list into the second part of my script so the relationship search is only looking inside the list of incidents i pulled vs all tickets?

September 2nd, 2015 9:25am

Hi,

You cant do that kind of filtering in one query, so first pull all IRs that have the desired relationship, then get the Closed IRs from that result. Kinda like this:

$IRClass = Get-SCSMClass System.Workitem.Incident$
$Active = Get-SCSMEnumeration IncidentStatusEnum.Active$
$IR1 = Get-SCSMObject -Class $IRClass -Filter "Title -like '*test*'"
$IR2 = $IR1 | ?{$_.Status -eq $Active}

$IR1.Count
$IR2.Count

Regards
//Anders

Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 3:21pm

Hello AndersAsp,

I had the same idea. Problem is that when I pull the list of incidents that use this CI targeted in their relationship the list returns all the incidents but no status is listed... therefore I cannot filter the list by status resolved as it won't find any status.

$WIAffectsCIClass = Get-SCSMRelationshipClass System.WorkItemAboutConfigItem$

$TempCIClass = Get-scsmClass -name Flexity.CI.Class
$TempCI = Get-scsmObject -class $TempCIClass -Filter "DisplayName -like %Temporary CI%"

$IR1 = (Get-scsmRelationshipObject -ByTarget $TempCI | ? {$_.RelationshipID -eq $WIAffectsCIClass.ID} ) #| Where {$_.Status -like '*Closed*'}

$Resolved = Get-SCSMEnumeration IncidentStatusEnum.Resolved$

$IR2 = $IR1 | ?{$_.Status -ne $Resolved}

$IR1.Count
$IR2.Count

IR1 will return a list in this format:

SourceObject      : IR39631 - APRDESXPH047 is not responding
TargetObject      : Temporary CI
RelationshipId    : b73a6094-c64c-b0ff-9706-1822df5c2e82
IsDeleted         : False
Values            : {}
LastModified      : 8/19/2015 12:13:51 PM
IsNew             : False
HasChanges        : False
Id                : 067aaa95-f3aa-1353-18b9-654ab226fdac
ManagementGroup   : SMAdmins
ManagementGroupId : 4c53ee2b-7703-6945-5363-c4d83d61c7e0

As you can see there is no Status property returned... therefore cannot sort by status. Stuck!

September 8th, 2015 10:14am

Hi SirLearnAlot.

What you are actually looking at there is the actual relationship object, which related the Incident (SourceObject) to the CI (TargetObject)

You have two options really.

1. Using what you have there, specify a property of the SourceObject to filter on, so "$IR1.SourceObject.Status -ne $Resolved"

2. Use an ObjectProjection to return all you values.

HTH

Cheers

Shaun

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 12:06pm

Hello Shaun,

Thanks for your reply. I tried your first option, however I'm not sure where to implement it. I tried sticking $IR1.SourceObject.Status -ne $Resolved before my $IR1 = (Get-scsmRelationshipObject -ByTarget $TempCI | ? {$_.RelationshipID -eq $WIAffectsCIClass.ID} ) as well as after, but still same result. The line by itself only returns a value of "True", which does not make sense to me.

My thoughts are I have to integrate it into the same line where it is getting the relationship objects, but where can i put it? specifying the SourceObject.Status is not a property/parameter under the Get-SCSMRelationshipObject cmdlet...

Thanks again.

September 8th, 2015 2:11pm

Why not turning the order of the criteria?

First filter incidents on status

Second filter incidents by affected CI

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 3:06pm

@Andreas

I have also tried this approach. The problem is the second part... I can filter incidents on status and save to variable $IR1

But to pull the incidents that have the specific CI i have to look through the relationship class (Get-SCSMRelationshipClass System.WorkItemAboutConfigItem$)

I don't know how else to pull the incidents that have this relationship... if there is a way to feed the closed status incidents from $IR1 into the second part I do not know of it... feel free to suggest :)

September 8th, 2015 3:11pm

Something like this?

Import-Module smlets
cls

$CIdisplayname = "DC1"


$IncidentClass = Get-SCSMClass -Name System.WorkItem.Incident$
$CIclass = Get-SCSMclass -Name System.ConfigItem$
$RelWIaboutCI = Get-SCSMRelationshipClass -Name System.WorkItemAboutConfigItem$
$IncidentStatus = Get-SCSMEnumeration -Name IncidentStatusEnum.Active$

$Incidents = Get-SCSMObject -Class $IncidentClass | Where {($_.Status -eq $IncidentStatus)}
$Counter = 0

foreach ($Incident in $Incidents)
{
$AffectedCI = Get-SCSMRelatedObject -SMObject $Incident -Relationship $RelWIaboutCI | Where {($_.Displayname -eq $CIdisplayname)}
If ($AffectedCI.DisplayName -eq $CIdisplayname)
    {
    Write-Host "Incident found:" $Incident.ID
    $Counter++
    }
}
Write-Host "Count of Incidents with Affected CI" "'$CIdisplayname':" $Counter

The result looks like this:

Hope this helps.

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 6:54pm

Thanks Andreas - Greatly appreciate the help
September 9th, 2015 9:49am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics